Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

MediaPlayer don't work well in WPF

$
0
0

I have a very strange problem with MediaPlayer in WPF. The player is set in the main window consturctor, its works fine, but if I put it in some timer or inside a method its just dont play the sound file!

The source is fine, and the properties are set to copy always. I checked also with the debugger and it does read the line.

I'm really out of ideas, please let me know if you have something.

Here is the code that works fine.

public MediaPlayer shot;

public MainWindow()
{
    InitializeComponent();
    Controller = new GameControl(this, Canvas);
    Controller.SetLevelsBtns(Canvas);

    shot = new MediaPlayer();
    shot.Open(new Uri(@"AbsolutePath\GunShot.mp3", UriKind.Absolute));
    shot.Play();
}

And thats the code that dosnt play:

public void fireTimer_Tick(object sender, EventArgs e)
    {
        if (Keyboard.IsKeyDown(Key.Space) && Controller.GameOn && bulletsCartridge <= Controller.bullets.Length)
        {
            shot = new MediaPlayer();
            shot.Open(new Uri(@"AbsolutePath\GunShot.mp3", UriKind.Absolute));
            shot.Play();
            Controller.fireTimer.Start();
            Controller.bullets[bulletsCartridge].SetBullet(Controller.player);
            Controller.bullets[bulletsCartridge].Draw(Canvas);
            Controller.bullets[bulletsCartridge].Alive = true;
            bulletsCartridge++;
            Controller.fireTimer.Stop();
        }
    }

Thanks.




Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>