C
C#3y ago
Exilon

Restarting audio with MediaPlayer?

I've made a button that uses MediaPlayer to play a custom MP3 file that I've made.
MediaPlayer flashSound;
flashSound = new MediaPlayer();
flashSound.Open(new Uri(@"FlashBangSound.mp3", UriKind.RelativeOrAbsolute));

...

private void Button_Click_1(object sender, RoutedEventArgs e)
{
flashSound.Play();
}
MediaPlayer flashSound;
flashSound = new MediaPlayer();
flashSound.Open(new Uri(@"FlashBangSound.mp3", UriKind.RelativeOrAbsolute));

...

private void Button_Click_1(object sender, RoutedEventArgs e)
{
flashSound.Play();
}
This does work too. It's just that it only works once. Once I press the button again, nothing happens and I believe its because the audio is finished and needs to be restarted, but how would I do that?
15 Replies
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Exilon
ExilonOP3y ago
Ill try that @PNTX I get this error: : 'The calling thread cannot access this object because a different thread owns it.' obviously not related to your solution yours probably works
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Exilon
ExilonOP3y ago
I already have that open lmfao
this.Dispatcher.Invoke(() =>
{
...// your code here.
});
this.Dispatcher.Invoke(() =>
{
...// your code here.
});
apparently this is the solution but I have no clue where to put this...
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Exilon
ExilonOP3y ago
The button is in another class. This is the function that is ran when someone clicks the mouse:
public void CSGOflash()
{
if (!flashbanged)
{
flashSound.Play();
flashbanged = true;
}
else
{
queue += 1;
}
}
public void CSGOflash()
{
if (!flashbanged)
{
flashSound.Play();
flashbanged = true;
}
else
{
queue += 1;
}
}
It works here for some reason Oh and dispatcher doesn't exist in the other class for some reason.
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Exilon
ExilonOP3y ago
ah
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Exilon
ExilonOP3y ago
yep it does the insance of the class that contains the sound is constructed on a different thread
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Exilon
ExilonOP3y ago
Ill check this out thanks Arent Task and Threading different?
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Tvde1
Tvde13y ago
one is a class and the other is a namespace
Exilon
ExilonOP3y ago
Oh... I didnt notice lmfao
Want results from more Discord servers?
Add your server