Windows 8 Store App - MediaElement不再播放音频

时间:2013-02-21 18:10:55

标签: c# xaml audio windows-8 mediaelement

我在按下切换按钮时尝试播放音频文件(.wav)(再次按下时暂停)。我最初工作,但现在我必须弄乱一些东西,我正在寻求帮助。这就是我的做法:

在XAML中创建MediaElement

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

然后我的ToggleButton就是这样:

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

在我的代码隐藏中,我有ToggleButton的已选中/未选中的处理程序:

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

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

有关可能出现的问题或如何检查的任何想法?谢谢!


编辑:调试了一些。看起来myMediaElement没有超过Opening州?

3 个答案:

答案 0 :(得分:1)

显然这是一个硬件问题。我的电脑(运行Bootcamp的MacBook)就是问题所在。终于在这篇文章中找到了答案 - &gt; MediaElement in WinRT / Win8 does not work at all

感谢大家提供的所有帮助

答案 1 :(得分:0)

您的媒体元素是否具有视觉效果对您来说重要吗?

请在点击事件中尝试此操作:

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 :(得分:0)

已显示播放音频文件所需的代码。 (播放下一个音频的代码是奖金)

1.将媒体元素,播放/暂停/停止按钮添加到XAML文件中。

<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.将以下代码添加到代码隐藏文件中:

    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();
    }