|
Introduction
This is a simple class to play MIDI and Wave files with your VB.NET application.
Using the code
Add a new class file into your project and copy the code into it. Make a reference to DirectX7 COM-Object. Imports System
Imports DxVBLib
Public Class SoundPlayer
Private Declare Function mciSendString Lib "Winmm.dll" Alias "mciSendStringA" _
(ByVal lpstrCommand As String, ByVal lpstrReturnString As String, _
ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long
Private File As String
Private m_dx As New DirectX7
Private m_ds As DirectSound
Public Sub New(ByVal strFileName As String)
Me.File = strFileName
End Sub
Public ReadOnly Property FileName() As String
Get
Return File
End Get
End Property
Public Function PlaySound() As Boolean
If InitAudio() = True Then
If PlayFile() = True Then
Return True
Else
Return False
End If
Else
Return False
End If
End Function
Public Function StopSound() As Boolean
StopFile()
End Function
Private Function PlayFile() As Boolean
Dim lRet As Long
Try
StopFile()
lRet = mciSendString("open " & Me.File & " alias track", "", 0, 0)
lRet = mciSendString("play track", "", 0, 0)
PlayFile = (lRet = 0)
Catch ex As Exception
Return False
End Try
Return True
End Function
Public Function CloseAudio()
mciSendString("close all", 0, 0, 0)
End Function
Private Function StopFile() As Boolean
Dim lRet As Long
Try
lRet = mciSendString("stop track", "", 0, 0)
lRet = mciSendString("close track", "", 0, 0)
Return True
Catch ex As Exception
Return False
End Try
End Function
Private Function InitAudio() As Boolean
m_ds = m_dx.DirectSoundCreate("")
If Err.Number <> 0 Then
Return False
Else
Return True
End If
End Function
End Class
In your application, play a sound like this: Dim Sound As New SoundPlayer(".\sound\testmidi.mid")
Sound.PlaySound()
or Sound.StopSound()
to stop playing.
Sound.FileName shows the filename given to the class on declare.
I didn't test it but I'm sure that it works with DX8 or DX9 too. And always remember: this code is for the beginners to show how to play MIDI and Wave files. The class plays each file once. No loop...
| You must Sign In to use this message board. |
|
| | Msgs 1 to 14 of 14 (Total in Forum: 14) (Refresh) | FirstPrevNext |
|
 |
|
|
hi all, I have reached to a step that the player can play and stop music that I have selected. But how may I play files that have chinese characters for their file name?
Please advise.
Thanks.
serene
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
I need to play a files at the same time, for different devices, In vb6 i can do this, but how can i do in vb net??
hjhgfhj
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Hi i am doing a mci music player in vb.net. I have done everything except a progress bar to skip song and a duration bar. How would I do this and what MCI strings would I use heres what i have done far
Private Paused As Boolean = False 'this is a class level variable
'Private close As Long
Private Declare Function mciGetErrorString Lib "winmm.dll" Alias "mciGetErrorStringA" (ByVal dwError As Long, ByVal lpstrBuffer As String, ByVal uLength As Long) As Long
_ Public Shared Function mciSendString(ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Integer, ByVal hwndCallback As Integer) As Integer 'Used to call MCI
End Function
Public Function PlayMCI(ByVal filename As String, Optional ByVal ShowMsg As Boolean = False) As Boolean
' close any previous commands that have opened the media mciSendString("Close all", 0, 0, 0) ' and create the string which will open the new one
mciSendString("Open " & Chr(34) & filename & Chr(34) & " Type " & SoundFormat(filename) & _ " Alias Med", 0, 0, 0)
mciSendString("Close", 0, 0, 0) mciSendString("Pause Med ", 0, 0, 0)
mciSendString("Open " & Chr(34) & filename & Chr(34) & " Alias Med style child parent " + PictureBox1.Handle.ToString, 0, 0, 0)
mciSendString("Play Med", 0, 0, 0)
' End If End Function ' Play MCI
Private Function SoundFormat(ByVal FullPath As String) As String Dim Sound As New FileInfo(FullPath) Dim FileExt As String = Sound.Extension.ToLower
Select Case FileExt Case ".wav" Return "Waveaudio" Case ".mid" Return "Mpeg3" Case ".mp3" Return "MPEGVideo" Case ".mpg" Return "CDAudio" Case ".wma" Return "MPEGVideo" Case ".mpeg" Return "Windows Media Player" Case Else Return "NotSoundFile" End Select End Function ' SoundFormat #Region " Windows Form Designer generated code "
Public Sub New() MyBase.New()
'This call is required by the Windows Form Designer. InitializeComponent()
#End Region
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load mciSendString("close Med", 0&, 0, 0)
End Sub
Private Sub play_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim OpenFile As New OpenFileDialog
With OpenFile .InitialDirectory = "G:\" .Filter = "MP3 Files (*.mp3)/*.mp3/Wma files(*.wma)/*.wma/Wav Files(*.wav)/*.wav/Mpg Files (*.mpg) /*.mpg/flash Files (*.flvplayer) /*.flvplayer/All Files(*.*)/*.*" '.Filter = "Mpg Files (*.mpg) /*.mpg" .CheckFileExists = True
' Open the dialog If .ShowDialog = DialogResult.OK Then
PlayMCI(.FileName) 'mul.wait = False Label2.Text = .FileName
Timer1.Enabled = True
End If End With End Sub
Private Sub Stop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click mciSendString("Close all", 0, 0, 0) End Sub
Private Sub TrackBar1_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar1.Scroll mciSendString("seek Med to " & TrackBar1.Value, 0&, 0, 0) mciSendString("Play Med", 0, 0, 0)
TrackBar1.Minimum = 0 TrackBar1.Maximum = 24000 TrackBar1.SmallChange = 1000 'To make intervale of 1 second... End Sub
Private Sub ProgressBar1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ProgressBar1.Click
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
End Sub
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
|
Hi I'm an almost comlete beginner trying to write a midi jukebox app ( I am a pro musician wrote a midi jukebox prog in GFA basic on Atari ! )
I've done everything as you said. - put a button on the form and put the play code into the button_click section . The prog stops and hilights the line
lRet = mciSendString("stop track", "", 0, 0
in the Private function stopfile() section in green and says ' PInvokeStackImbalance has been detected and some stuff I don't understand . Can you help Thanks Tony ( in France )
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
My application has a module in which we want to be able to give the user an option to add a voice tag to their module. Any idea what classes I could use to do that ? I tried a bunch of the stuff available online but nothing really worked ?
-Parik. http://parik.in
|
| Sign In·View Thread·PermaLink | 1.00/5 (4 votes) |
|
|
|
 |
|
|
Hi. I can play the mid files with no problem, and now i wish to make my program into an installation package. I followed the instruction, but it raises an error when I trying to add the primary output, it says
"The following files may have dependencies that cannot be determind automatically. Please cofirm that all dependencies have been added to te project.
c:\winnt\system32\dx7vb.dll"
what shoud I do. Thank you
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
 | Help! |  | Anonymous | 11:37 29 Jan '05 |
|
|
Sorry, this may sound like a stupid question, but what do you mean by "Make a reference to DirectX7 COM-Object"? And could this be why I have build errors in that class when i try and compile it? Also, the sound doesnt play.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
To add a reference, go to the project menu and select add reference close to the bottom, select the .com sub-menu and find directx 7, click select and then ok. The problems should be fixed.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
I'm very new to programming. I created your SoundPlayer. Where do the "Dim Sound As New SoundPlayer (".\sound\testmidi.mid")... code lines fit in? Are these put into the same "class" file that was added?Also, how would I play 3 files sequentially...no pause in between. Thanks for any help!
|
| Sign In·View Thread·PermaLink | 1.33/5 (3 votes) |
|
|
|
 |
|
|
hi, i am also new to VB_NET. I struggle quite a while on this (no sound coming out). because when invalid file given, the program did not give warning... Now finally able to play mid sound.
"Dim Sound As New SoundPlayer (".\sound\testmidi.mid")... code can fit in your Private Sub form1_Load and any other SUB under the Public form1 class... you can put your test.mid in bin directory then use "Dim Sound As New SoundPlayer ("test.mid")" [Initially i tried (".test.mid") which is not valid]
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
 |
|
|
 |
|
|
General News Question Answer Joke Rant Admin
|