Playing a Sound in Windows Phone 8.1

I am developing a cross-platform app for Android, iOS and Windows Phone platforms.

What I am simply trying to do is play a sound on my app, simple as that.
I can manage that by using Dependency attributes on Android but I can't seem to play a sound on "Windows Phone".

This is the code:

        public bool PlaySound(string fileName)
        {
            var snd = new MediaElement
            {
                AutoPlay = false,
                Source = new Uri("ms-appx:///Assets/Audio/test.wav", UriKind.RelativeOrAbsolute)
            };
            snd.Play();

            return true;
        }

And this is the structure of my files:

Am I pointing the wrong location? Shouldn't I use MediaElement? What should I do?

Best Answer

Answers

  • RobertJetRobertJet UAMember

    I have this problem. MediaElement need add in visual tree(or using in xaml). But it is not work for me. Please help. Any ideas?

  • RobertJetRobertJet UAMember
    edited December 2016

    @Caner
    Thank you, but it not work for me. I use MediaElement in App.Resources. It is my code:

        <ResourceDictionary>
                 <MediaElement x:Key="PlayElement"/>
        </ResourceDictionary>
    
        public   MainPage()
        {
             this.InitializeComponent();
             var app = new XDayTimer.App();
         LoadApplication(app);
         this.NavigationCacheMode = NavigationCacheMode.Required;
         _play = (MediaElement)App.Current.Resources["PlayElement"];
         _play.Source = new Uri("ms-appx:///Assets/Audio/ring.wav", UriKind.RelativeOrAbsolute);
         _play.MediaOpened += PlayElement_OnMediaOpened;
         _play.MediaFailed += PlayElement_OnMediaFailed;
         }
    

    My events PlayElement_OnMediaOpened and PlayElement_OnMediaFailed- never work.
    I have spent a lot of time on this issue. Maybe I'll use toast.

  • CanerCaner USMember ✭✭
    I also had problem with MediaElement, try using MediaPlayer
Sign In or Register to comment.