1

I am trying to play an audio file (.wav) when a toggle button is pressed (and pause when pressed again). I had it working initially, but now I must of messed something up and am looking for help. This is how I'm doing it:

Create MediaElement in XAML

<MediaElement x:Name="myMediaElement" HorizontalAlignment="Center" VerticalAlignment="Center" PosterSource="vuvuzela.png" IsLooping="True" Source="Assets/vuvuzela.wav" Grid.Row="1" AutoPlay="False"/>

Then My ToggleButton is this:

<ToggleButton x:Name="ToggleButton" Content="Activate" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Row="2" FontSize="32" Style="{StaticResource ToggleButtonStyle1}" Checked="Tog_Checked" Unchecked="Tog_Unchecked"/>

And in my Code-behind, I have the ToggleButton's checked/unchecked handlers:

private void Tog_Checked(object sender, RoutedEventArgs e)
    {
        myMediaElement.Play();
    }

    private void Tog_Unchecked(object sender, RoutedEventArgs e)
    {
        myMediaElement.Pause();
    }

Any ideas as to what might be going wrong or how to check it? Thanks!


EDIT: Debugged some more. Looks like the myMediaElement is not getting past the Opening state?

5
  • Just to let you know, it happens with my windows phone that sound doesn't work anymore until I reboot it. Started noticing that when my wake-up alarm didn't sound one day, and I couldn't play any sound on the phone until I restarted it. So maybe you ran into this weird system bug?
    – Qortex
    Feb 21, 2013 at 18:39
  • not a bad idea, but rebooting was to no avail
    – Alan Tate
    Feb 21, 2013 at 19:13
  • I noticed that setting autoplay to true in the toggle event/click event (whatever trigger) might do the trick. Try myMediaElement.AutoPlay = true; myMediaElement.Play(); Feb 21, 2013 at 22:39
  • @IrisClasson thanks but that did not seem to work either. Do you have any other ideas? the MediaElementis never getting Opened
    – Alan Tate
    Feb 23, 2013 at 19:21
  • I remember a similar issue a few months ago, the person did a VS repair, updated all the drivers for the computer and did a restart. Give that a try if you haven't already. Fingers crossed for you! Feb 23, 2013 at 20:41

3 Answers 3

1

Apparently it was a hardware problem. My computer (MacBook running Bootcamp) was the issue. Finally found that answer in this post --> MediaElement in WinRT / Win8 does not work at all

Thanks for all the help though everyone

0

Is it important to you that your media element be visual like that?

Try this in your click event instead:

var _Media = new Windows.UI.Xaml.Controls.MediaElement() { AutoPlay = false };
var _Location = Windows.ApplicationModel.Package.Current.InstalledLocation;
var _Folder = await _Location.GetFolderAsync("Assets");
var _File = await _Folder.GetFileAsync("Ding.wav");
var _Stream = await _File.OpenAsync(Windows.Storage.FileAccessMode.Read);
_Media.SetSource(_Stream, _File.ContentType);
_Media.Play();
2
  • No it does not have to be visual I guess. I tried your code though and still got nothing. When I debug with breakpoints, the CurrentState of _Media is Opening. Never gets to Play. Does that mean something?
    – Alan Tate
    Feb 21, 2013 at 19:15
  • remove AutoPlay = false and it should work. If it is in the click event for a play button it doesn't need to be false anyway. Otherwise set to false in XAML and true before play in code. Feb 21, 2013 at 22:43
0

Have shown code required to play audio file. (code for playing next audio is bonus )

1.Add media element, play/pause/stop buttons to the XAML file.

<MediaElement x:Name="media" Source="Assets/page1/para1.mp3"  
              Grid.Column="0" Grid.Row="0"  AutoPlay="True" />
<Button Click="StopMedia" Grid.Column="0" Grid.Row="1" Content="Stop" />
<Button Click="PauseMedia"  Grid.Column="1" Grid.Row="1" Content="Pause" />
<Button Click="PlayMedia" Grid.Column="2" Grid.Row="1" Content="Play" />

2.Add the following code to the code-behind file:

    private void StopMedia(object sender, RoutedEventArgs e)
    {
        media.Stop();
    }
    private void PauseMedia(object sender, RoutedEventArgs e)
    {
        media.Pause();
    }
    private  void PlayMedia(object sender, RoutedEventArgs e)
    {
        media.Source = new Uri(this.BaseUri, "Assets/page1/para1.mp3");
        media.Play();
    }

    protected override async void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
    {
        media.MediaEnded += media_MediaEnded;

    }

    private void media_MediaEnded(object sender, RoutedEventArgs e)
    {
        media.Source = new Uri(this.BaseUri, "Assets/page1/para2.mp3");
        media.Play();
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.