peyman gilmour
peyman gilmour

Reputation: 1218

windows phone 8.0 MediaElement plays sound without any call

When i click button no 1 , MediaElement will play the pronunciation of that sentence . then if i click button no 2 i will navigate to SoundRecord page . then if i click the back button to previous page (as you see in the picture ) again , MediaElement play the previous pronunciation that you have chosen without call any function ... what is the reason and how can i fix this problem ?

enter image description here

also this is the code for play the sound files :

public void PlayPronunciation(string fileName, MediaElement soundPlayer)
    {
        var path = string.Format("Assets/Audio/{0}.mp3", fileName);
        soundPlayer.Source = new Uri(path, UriKind.Relative);
        soundPlayer.AutoPlay = true;

        if (soundPlayer.CurrentState == MediaElementState.Stopped)
            soundPlayer.Play();
    } 

For temporary solution in page unload event i did something like this but i think this not a nice way to fix my problem :

private void PhoneApplicationPage_Unloaded(object sender, RoutedEventArgs e)
{
SoundPlayer.AutoPlay = false;
}

Upvotes: 0

Views: 133

Answers (1)

csharpwinphonexaml
csharpwinphonexaml

Reputation: 3683

  • Remove this line: soundPlayer.AutoPlay = true; if you use

    if (soundPlayer.CurrentState == MediaElementState.Stopped)
       soundPlayer.Play();`
    
  • Set AutoPlay="False" in the xaml and delete any soundPlayer.AutoPlay=true; from your code

  • Change this:

    if (soundPlayer.CurrentState == MediaElementState.Stopped) 
    

    to this:

    if (soundPlayer.CurrentState != MediaElementState.Playing)
    

Upvotes: 1

Related Questions