简体   繁体   English

MediaElement可在WPF中工作,但不能在Windows 8.1 App中工作

[英]MediaElement working within WPF but not within Windows 8.1 App

The code shown below, which plays an audio file, runs fine within my WPF application. 下面显示的播放音频文件的代码在我的WPF应用程序中可以正常运行。 But when I execute the same code within a Windows 8.1 app , I am not getting any exceptions but I am also receiving no sound. 但是,当我在Windows 8.1应用程序中执行相同的代码时,没有任何异常,但也没有声音。 Can anyone help? 有人可以帮忙吗?

private void myMediaElement_MediaOpened(object sender, RoutedEventArgs e)
{
    myMediaElement.Source =
        new Uri(@"C:\Users\Soph\Music\Addicted.mp3", UriKind.Absolute);
    myMediaElement.Play();
}

private void btn1_Click(object sender, RoutedEventArgs e)
{
    myMediaElement_MediaOpened(sender,e);
}

EDIT: 编辑:

I have added per the advice the mediaFailed event (followed from http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.mediaelement.mediafailed ) 我已根据建议添加了mediaFailed事件(来自http://msdn.microsoft.com/zh-cn/library/windows/apps/windows.ui.xaml.controls.mediaelement.mediafailed

    private string GetHresultFromErrorMessage(ExceptionRoutedEventArgs e)
    {
        String hr = String.Empty;
        String token = "HRESULT - ";
        const int hrLength = 10;     // eg "0xFFFFFFFF"

        int tokenPos = e.ErrorMessage.IndexOf(token, StringComparison.Ordinal);
        if (tokenPos != -1)
        {
            hr = e.ErrorMessage.Substring(tokenPos + token.Length, hrLength);
        }

        return hr;
    }

    private void mycontrol_MediaFailed(object sender, ExceptionRoutedEventArgs e)
    {
        // get HRESULT from event args 
        string hr = GetHresultFromErrorMessage(e);

        // Handle media failed event appropriately 
    }

Then i tried to debug this: 然后我尝试调试此:

Name Value Type 名称值类型

this {PracMEWindowsApp.MainPage} PracMEWindowsApp.MainPage e {Windows.UI.Xaml.ExceptionRoutedEventArgs} Windows.UI.Xaml.ExceptionRoutedEventArgs hr "0x80070003" string token "HRESULT - " string tokenPos 40 int hrLength 10 int 此{PracMEWindowsApp.MainPage} PracMEWindowsApp.MainPage e {Windows.UI.Xaml.ExceptionRoutedEventArgs} Windows.UI.Xaml.ExceptionRoutedEventArgs hr“ 0x80070003”字符串令牌“ HRESULT-”字符串tokenPos 40 int hrLength 10 int

What is this hr capturing ? 这个小时捕获了什么? Why is my file not played ? 为什么我的文件没有播放?

To access the Music library specify the Music Library capability in app manifest. 要访问音乐库,请在应用清单中指定“音乐库”功能。 It is also good practice to always handle the MediaFailed event. 始终处理MediaFailed事件也是一个好习惯。

HRESULT 0x80070003 is "Directory Not Found". HRESULT 0x80070003是“找不到目录”。 That would suggest that the media element doesn't like something about the file path. 这表明media元素不喜欢有关文件路径的内容。 If you get HRESULT 0x80070005 (Access Denied), then it's probably a permissions-related thing. 如果收到HRESULT 0x80070005(拒绝访问),则可能与权限相关。

Windows Store app does not have access to files in local file system, it runs in a sandboxed environment and only has access to its own data folder, a path like C:\\Users\\Soph\\Music\\Addicted.mp3 is unrecognizable. Windows Store应用程序无权访问本地文件系统中的文件,它在沙盒环境中运行,并且只能访问其自己的数据文件夹,无法识别C:\\Users\\Soph\\Music\\Addicted.mp3这样的路径。

There are several ways in which you can play the media file. 有几种播放媒体文件的方法。

a.Specify the Music Library capability in Package.appxmanifest (this is required), and then Package.appxmanifest指定“音乐库”功能(这是必需的),然后

private async void btn1_Click(object sender, RoutedEventArgs e)
{
    var file = await Windows.Storage.KnownFolders.MusicLibrary.GetFileAsync("Addicted.mp3");
    var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
    myMediaElement.SetSource(stream, file.ContentType);
}

or 要么

b. b。 use FileOpenPicker , choose the song manually, you can access any .mp3 file in the file system. 使用FileOpenPicker ,手动选择歌曲,您可以访问文件系统中的任何.mp3文件。

private async void btn1_Click(object sender, RoutedEventArgs e)
{
    FileOpenPicker openPicker = new FileOpenPicker();
    openPicker.FileTypeFilter.Add(".mp3");
    openPicker.SuggestedStartLocation = PickerLocationId.MusicLibrary;
    var file = await openPicker.PickSingleFileAsync();
    var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
    myMediaElement.SetSource(stream, file.ContentType);
}

or 要么

c. C。 Add Addicted.mp3 to your project, and then 将Addicted.mp3添加到您的项目,然后

Uri uri = new Uri("ms-appx:///Addicted.mp3");
myMediaElement.Source = uri;

you can also set it in XAML 您还可以在XAML中进行设置

<MediaElement x:Name="myMediaElement" Source="Addicted.mp3" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM