1

I started to develop application using xamarin, and one of projects inside my solution is UWP.

I need to play sound there when someone clicked button, I'm using MediaPlayer to achieve my goal, and on windows 10 (desktop) it works fine, but on my Windows Mobile 10 (Lumia 930) it starts with long delay (about 1 second).

Below I provide my code to play audio source:

MediaPlayer _player = BackgroundMediaPlayer.Current;
_player.SetUriSource(new Uri(String.Format("ms-appx:///Assets/Sound/5s.wav", UriKind.Absolute)));            
_player.Play();

My Question is:

Is there any other way to play audio in UWP than MediaPlayer?

FoldFence
  • 2,674
  • 4
  • 33
  • 57
  • have you tried to preload the music ? you should have look here:https://blogs.windows.com/buildingapps/2016/01/13/the-basics-of-background-audio/ – FoldFence May 20 '16 at 09:42

2 Answers2

1

If you don't have specific reason to use background audio, you can use just media element to play audio in foreground:

<!-- create element in XAML or in code -->
<MediaElement Name="mediaElement" ... />


// Code - set source or reference to stream
MediaElement mediaElement = new MediaElement();
mediaElement.Source = new Uri("msappx:///Media/sound.mp3");    

I would also recommend to check with the list of supported codecs.

In more complex scenarios you may want to look at Audio Graph API.

Konstantin
  • 884
  • 6
  • 12
  • Thanks for your answer, but in my application music is sensitive spot, I only have to play it for 5 seconds, but I need to do it without delay and in background. Audio Graph didn't help me in this case also. I will keep looking for solution to my problem. – Wojciech Golab May 19 '16 at 19:55
0

I'm not sure if this is bad practice or not, but I am able to get instant playback if I pre-load the media.

Something like this example in pseudo-code (c# style):

class Foo
{
   private MediaPlayer _player;

    Foo() //constructor
    {
      _player = BackgroundMediaPlayer.Current;
      _player.AutoPlay = false;
      _player.SetUriSource(new Uri(String.Format("ms-appx:///Assets/Sound/5s.wav", UriKind.Absolute)));            
    }

    void ButtonClicked(Object sender, EventArgs event)
    {
        _player.Play();
    }
}
Evan de la Cruz
  • 1,966
  • 1
  • 13
  • 17