我有一个MediaElement,我用它来跨页面播放音乐。因此,我不得不将其作为一个资源保存在App.xaml中。
直到我在我的可湿性粉剂中按下Windows按钮,事情才能正常工作。应用程序会被逻辑删除,MediaElement会按预期停止播放。在我的Application_Deactivated上,我显式地调用Player.Stop()
当我恢复应用程序时出现问题。所有其他状态都会恢复,但music元素不会播放音乐。我可以看到负责音乐的代码正在被攻击,但是MediaElement的MediaOpened没有被激活。我是不是漏掉了什么明显的东西?
编辑以澄清KeyboardP的问题
<Application.Resources>
<MediaElement x:Name="ME" MediaEnded="RepeatMedia" Volume="1" AutoPlay="False" Height="0" Source="/Sounds/mywave.wav" />
</Application.Resources>在我的App.XAML.CS中,我有一个名为...
public MediaElement player = null;
private void InitializeMusic()
{
if (App.Current.Resources.Contains("ME"))
{
player = App.Current.Resources["ME"] as MediaElement;
}
player.MediaOpened += player_MediaOpened;
}我正在重新初始化它...
// Code to execute when the application is activated (brought to foreground)
// This code will not execute when the application is first launched
private void Application_Activated(object sender, ActivatedEventArgs e)
{
InitializeMusic();
}发布于 2013-04-04 18:45:17
我不知道为什么,但是Source属性在逻辑删除之后被删除了,所以尝试在后面的代码中重置源代码。
if (App.Current.Resources.Contains("ME"))
{
player =(MediaElement) App.Current.Resources["ME"] as MediaElement;
player.Source = new Uri("/Sounds/mywave.wav", UriKind.Relative);
}https://stackoverflow.com/questions/15757343
复制相似问题