-
Nov 24th, 2016, 12:02 AM
#1
Thread Starter
Lively Member
Is it possible to play a .wav file through Windows Media Player?
Hello, I am trying to create a game that needs to have more then 2 sounds play at one time. I want Windows Media Player to play the background sound on a loop that is not interuppted by the other sound which will play from a button. I have been reading tons of posts and none have worked. Do I need to use the Soundplayer? I do not know what to do. Please help me. Thanks!
-
Nov 24th, 2016, 05:02 AM
#2
Re: Is it possible to play a .wav file through Windows Media Player?
When I want multiple sounds I've used the old standard, windows multimedia dll and the mciSendString interface.
If you want the sound to loop, I've found that if you specify the type as "mpegvideo" then the repeat option will work.
It doesn't matter if what you're actually playing is a wav file, or mpg file or midi file, etc. If you say the type is mpegvideo on the play command, it will play the file and be able to loop using the repeat option.
Here is some code I used to experiment with some aspects of mci audio a little over six years ago.
It was probably in response to someone wanting to fade between two background loops, so as they walked around outside one loop would be playing, and if they walked into a building, the outside loop would fade out as the inside building loop would fade in (and vice versa when leaving the building).
For the example, I just added a trackbar with its maximum value set to 1000.
When you moved the trackbar it would adjust the volume of the two sounds, increasing one as the trackbar value increased and the opposite for the other.
If you had the trackbar at either extreme you only heard one, and in the middle you would hear both.
I also had two buttons to just turn off or on the audio from one of the loops, just to test the option of silencing one of the tracks, but not stop it from continuing to play.
Now, I've just added another button (button4) to play a short firing sound wav file each time the button is pressed, as an example to support what you've described.
One thing that sometimes people don't do when first trying to use mciSendString is to seek back to the beginning of the sound file each time they request it to play, so it only "works" once.
You could close the sound and reopen it each time before you play it, but I think that is just extra overhead compared to just resetting the play position each time.
You won't need the cross fading and two songs looping simultaneously, but since the code is already here and working, I'll post it as is, and you will need to pull from it what you want.
I copied both sound files to the executable directory, and tried different ways of opening the file just for experimentation, so you'll see two different methods used to do the initial commands.
The button1 code only needs to be done once, and initially, so could be done in the form load event. I just put it in the first button because I usually do that when testing something out, i.e. 1. Open a new project, 2. Add a button, 3. Put test code in button click event, 4. Run and hit button to test.
Code:
Public Class Form1
Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" _
(ByVal lpstrCommand As String, ByVal lpstrReturnString As String, _
ByVal uReturnLength As Integer, ByVal hwndCallback As Integer) As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim status As Integer
Dim openCommand As String = String.Format("open ""{0}"" type mpegvideo alias myAudio", IO.Path.Combine(Application.StartupPath, "Sleep Away.mp3"))
status = mciSendString(openCommand, Nothing, 0, 0)
status = mciSendString("open FlaxenHair.mp3 type mpegvideo alias audio2", Nothing, 0, 0)
status = mciSendString("open DefenderFire.wav type mpegvideo alias DefenderFire", Nothing, 0, 0)
mciSendString("play myAudio repeat", Nothing, 0, 0)
mciSendString("play audio2 repeat", Nothing, 0, 0)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
mciSendString("setaudio myAudio off", Nothing, 0, 0)
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
mciSendString("setaudio myAudio on", Nothing, 0, 0)
End Sub
Private Sub TrackBar1_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar1.Scroll
Dim s1 As String = TrackBar1.Value.ToString
Dim s2 As String = (1000 - TrackBar1.Value).ToString
mciSendString("setaudio myAudio volume to " & s1, Nothing, 0, 0)
mciSendString("setaudio audio2 volume to " & s2, Nothing, 0, 0)
End Sub
Private Sub Button4_Click(sender As System.Object, e As System.EventArgs) Handles Button4.Click
mciSendString("seek DefenderFire to start", CStr(0), 0, 0)
mciSendString("play DefenderFire", CStr(0), 0, 0)
End Sub
Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
mciSendString("Close DefenderFire", CStr(0), 0, 0)
mciSendString("Close myAudio", CStr(0), 0, 0)
mciSendString("Close audio2", CStr(0), 0, 0)
End Sub
End Class
Last edited by passel; Nov 24th, 2016 at 05:07 AM.
-
Nov 24th, 2016, 12:41 PM
#3
Thread Starter
Lively Member
Re: Is it possible to play a .wav file through Windows Media Player?
Alright, thank you very much!
-
Nov 26th, 2016, 08:06 PM
#4
Hyperactive Member
Re: Is it possible to play a .wav file through Windows Media Player?
Last edited by georgesutfin; Nov 27th, 2016 at 03:06 AM.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|