Showing posts with label sound. Show all posts
Showing posts with label sound. Show all posts

Monday, April 7, 2008

mp3 Files in VB .NET


You can play mp3 files in VB .NET also. The link above shows how to add a Windows Media Player to your project and to hide if you like. Otherwise you can show the player like shown at left.

You only have to change one line of code if you want it to allow the user to search for files (shown in red):

Private Sub 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

Extras - Sound

Below is the code for the sound. You will have to replace the red text below with the sound file's name (remember that if you put the sound files in your debug folder you can access it with only the name and not the entire path). Note: This only works with .wav files:


'Alex Maureau, Queens College
'Credits:
'http://www.codeproject.com/vb/net/SoundClass.asp
'by Angelo Cresta


Imports System
Imports System.Runtime.InteropServices
Imports System.Resources
Imports System.IO

Public Class Sound


Declare Auto Function PlaySound Lib "winmm.dll" (ByVal name _
As String, ByVal hmod As Integer, _
ByVal flags As Integer) As Integer

Public Const SND_SYNC = &H0 'play synchronously
Public Const SND_ASYNC = &H1 'play asynchronously
Public Const SND_LOOP = &H8 'play with a loop

Public Shared Sub PlayWaveFile( _
ByVal fileWaveFullPath As String)
Try
PlaySound(fileWaveFullPath, 0, SND_ASYNC)
Catch
End Try
End Sub
End Class