Tuesday, August 5, 2008

Play .wav/.mp3(Songs)/Vedio(Movies)

Hello Friends in this article we will see three things

1. How to play “.wav” Files?
2. How to play “.mp3” Files? (i.e. How to play Songs?)
3. How to play Video Files? (i.e. How to play Movies?)

First we will see to play wave files (i.e. “.wav” files)(U guys can found some of the .wav files on your system as well they are mostly in “C:\Windows\Media” in this folder U can found all the sound made by your Windows Operating system while doing diff. things.)

Create a new Project by clicking on FILE --> NEW PROJECT and selecting a new Windows Project.

A new Form will come in front of U. Now we need to add few Controls to it,

1. Two Button control. (btnPlay, btnOpen)
2. One Label Control. (Label1)
3. One Text Box Control. (txtFileNm)
4. One OpenFileDialog Control. (OpenFileDialog1)

Make Your Form Look like mine



Make the Filter Property of OpenFileDialoge to “WAV Files|*.wav”

Now just paste the code on your code window.
Imports System.Media

Public Class Form1

Private Sub btnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnopen.Click

If OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
txtFileNm.Text = OpenFileDialog1.FileName
End If

End Sub

Private Sub btnPlay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPlay.Click

If txtFileNm.Text <> String.Empty Then
Dim wavPlayer As SoundPlayer = New SoundPlayer()
wavPlayer.SoundLocation = txtFileNm.Text
AddHandler wavPlayer.LoadCompleted, AddressOf wavPlayer_LoadCompleted
wavPlayer.LoadAsync()
End If
End Sub

Private Sub wavPlayer_LoadCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs)
CType(sender, System.Media.SoundPlayer).Play()
End Sub
End Class


Now click on to browse button u will see the similar browsing dialog box which we normal see when we browse some file in windows.
Now just select any .wav file u have or u can found some in the folder I have already told U .Select the file and click Ok button and then hit the play button to here the sound.


____________________________________________________________________________

Now here is the thing for which most of U guys are bearing for this long i.e how to Play a song using your application or how to play “.mp3” file.

This one is simpler; create a new project as told earlier

To a new Form we add few Controls,

1. One Button control. (btnOpen/Play)
2. One OpenFileDialog Control. (OpenFileDialog1)
3. One WindowsMediaPlayer Control.

Make Your Form Look like mine




Note : This windowsMediaPlayer u can not find in your tool box for that you have to do the following

1. Right click on to the tool Box and click on to the choose items (As shown below.)




Now after a weight of few seconds new window (as shown below) will pop up.



On to this select the COM Component (Windows Media Player) (as shown above) and hit OK.

Then a new windows media player control is ready to be added to your Project . Now just double click on to the control and it will be added to your project.

Now just paste the code in your project and see(I mean hear) the magic.


Imports System.Media
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Const DATA_FILE_EXTENSION As String = ".mp3"
Dim dlgFileDialog As New OpenFileDialog
With dlgFileDialog
.Filter = DATA_FILE_EXTENSION & _
" files (*" & DATA_FILE_EXTENSION & "|*" & DATA_FILE_EXTENSION
.FilterIndex = 1
.RestoreDirectory = True
If .ShowDialog() = Windows.Forms.DialogResult.OK Then
'Play the sound file
Me.AxWindowsMediaPlayer1.URL = dlgFileDialog.FileName
End If
End With

End Sub


End Class

_____________________________________________________________________________-

In this portion we will see how to play a movie using vb.net

Just create a new project as told earlier

This will be the simplest .

On to the form add a single button hence I am not giving u the visual this time.
Now the new thing to this will be that u need to add the reference

Don’t panic every thing will be told here only have patience for some more time,just follow the steps

1. Click on the Project Menu --> Add Reference
2. New window will be open and now in .net Reference U can found “Microsoft.DirectX.AudioVedioPlayback” (As shown below)





Click OK to add the Reference to your project.

Now that’s it just pass the code and pass the path of your video file where required


Imports Microsoft.DirectX.AudioVideoPlayback
Public Class Form1
Dim WithEvents Vid As New Microsoft.DirectX.AudioVideoPlayback.Video("Your video file Path")


Public Sub Play()

If Vid Is Nothing Then Return
Vid.Play()

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Call Play()
End Sub
End Class

---------------------------------------------------------------------------------------------------------------------
Thanx for your patience ,in the end I would like to say that all these three projects can be done in one project but for the sake of simplicity I make it in three projects

With some more code and and some more user control one can have your very own media player.well this is not easy as I am saying but it can be done and in fact I am working on it as soon as it is done it will be on my Blog .

Till then HAPPY CODING and God Bless.

1 comment:

Anonymous said...

Dear sir,

It work well, but kindly educate me on playing all wave files. Eg., I tried playing some other wav files but i was not able to do other than c:/windows/music files.. since i am new to SW kindly help me. I would like to know more...