הערה
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות להיכנס או לשנות מדריכי כתובות.
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות לשנות מדריכי כתובות.
Question
Sunday, January 8, 2017 11:50 PM
I have a Timer active in my program, which checks if certain conditions are true/false, if they're true I want my sound to play, then stop if it's false.
e.g
If condition = True and My.Resources.Sound1 IsNot Playing Then
My.Computer.Audio.Play(My.Resources.Sound1, AudioPlayMode.BackgroundLoop)
ElseIf condition = False Then
My.Computer.Audio.Stop()
End If
My problem is, since it's within a timer, each time it goes by that condition while it's set to true, it just loops back the audio to the beginning rather than letting the audio play until the condition is false as im not sure how to check if it's already playing.
Is there any way around this/check if it's playing before executing?
(This is the only audio file within my program, so the code doesn't have to be defined to one/certain audio files.)
All replies (2)
Monday, January 9, 2017 12:40 AM ✅Answered
My problem is, since it's within a timer, each time it goes by that condition while it's set to true, it just loops back the audio to the beginning rather than letting the audio play until the condition is false as im not sure how to check if it's already playing.
Create a form-level variable to indicate whether or not you have started the background sound.
Dim IsPlaying As Boolean
...
If Condition = False Then
My.Computer.Audio.Stop()
IsPlaying = False
ElseIf IsPlaying = False Then
My.Computer.Audio.Play(My.Resources.Sound1, AudioPlayMode.BackgroundLoop)
IsPlaying = True
End If
Monday, January 9, 2017 12:44 AM
Whoops.....deleted last post.
My.Computer.Audio does not have method to determine its playstate.
The SoundPlayer class also does not have a method to determine its playstate.
The MediaPlayer class has a MediaEnded event which could be used to determine if the MediaPlayer has finished playing. Windows Media Player control can be used with events also. However both can not use an embedded resource and must have a path to a file so they can load the file.
On the other hand I just found this thread, in C#, where possibly an "in memory" URI can be provided such that MediaPlayer can then actually perform in some fashion using an audio file store in Memory. But I've no idea if it will work or not. See Read audio file to stream with MediaPlayer (WPF) and note that for MediaPlayer you will need to add appropriate refs and imports statements I suppose for System.Windows.Media although the actual ref will probably be presentationcore but I can't remember off hand.
So you've an option to write your resource to disk and use either MediaPlayer or WindowsMediaPlayer to play file from disk or see if the C# code at link, converted to VB, works to play resource from memory. You can try Telerik to convert the C# code to VB.
La vida loca