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?

4

3 回答 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

于 2013-02-24T07:30:03.907 回答
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();
于 2013-02-21T18:31:52.583 回答
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();
    }
于 2013-04-03T18:10:16.753 回答