Windows 8 Metro App中的Mediaelement

时间:2013-06-30 10:05:24

标签: c# xaml windows-runtime windows-store-apps winrt-xaml

我有一个特定于我的应用的方案。我正在为我的metro应用程序管理XML格式的音乐文件播放列表。它的保存音乐文件实际路径就像这样

d:\玛雅\ 1.MP3

我的XAML页面中有media元素,我正在设置它的Source。

 mediaElement.Source = new Uri(@"D:\MyMedia\1.mp3", UriKind.Absolute);
 mediaElement.Play();

但是它没有播放媒体,而是像这样给出错误

MF_MEDIA_ENGINE_ERR_SRC_NOT_SUPPORTED : HRESULT - 0x80070005

所以有人告诉我如何用absoulte路径在Metro应用程序的MediaElement中播放一些媒体文件。或者我如何获取我的本地文件流以在我的Media应用程序的Metro应用程序中播放此媒体。

4 个答案:

答案 0 :(得分:4)

要在本地系统上打开文件,可以使用FileOpenPicker获取文件,使用SetSource设置媒体源。

var openPicker = new Windows.Storage.Pickers.FileOpenPicker();

openPicker.FileTypeFilter.Add(".mp3");
var file = await openPicker.PickSingleFileAsync();

var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);

if (null != file)
{
    mediaElement.SetSource(stream, file.ContentType);
    mediaElement.Play();
}

答案 1 :(得分:0)

用户的PC上只能访问有限数量的位置。 “D:\ Mymedia”不是其中之一。您将在Microsoft帮助中找到所有必要的信息。看看这两篇文章:

答案 2 :(得分:0)

这可以在没有文件选择器的情况下完成。您只需将音乐库功能添加到您的应用清单,确保音乐在我的音乐或SD卡中,并使用KnownFolders.MusicLibrary。

简而言之,与问题的音乐主题保持一致:

"你可能会认为文件选择器......

...但我需要的只是StorageFile"

    //
    // Opens and plays song titled "[Cars]You_Might_Think.mp3"
    // in the 80s folder.
    //
    // IMPORTANT: the example song must be in the user's Music
    // folder or SD card to be found.   Also, the app manifest
    // must have 'Music Library' capabilities enabled.
    //
    // 
    //
    async void OpenAndPlayAwesomeSongFromThe80s()
    {            
        Windows.Storage.StorageFolder folder = KnownFolders.MusicLibrary;

        StorageFile song = await folder.GetFileAsync("80s\\[Cars]You_Might_Think.mp3");

        if (song != null)
        {
            IRandomAccessStream stream = await song.OpenAsync(Windows.Storage.FileAccessMode.Read);

            if (stream != null)
            {
                mediaElement = new MediaElement();
                mediaElement.SetSource(stream, song.ContentType);
                mediaElement.Play();
            }
        }
    }

答案 3 :(得分:0)

我知道这是一个老问题,我找到了一个非常简单的解决方案......

Windows.Storage.StorageFile file = null;
mediaElement player = new MediaElement();


    async Task PlayAsync()
    {
        if (file == null)
        {
            file = await OpenFileAsync();

            try { player.MediaOpened -= Player_MediaOpenedAsync; } catch { }
            try { player.MediaEnded -= Player_MediaEnded; } catch { }
            try { player.MediaFailed -= Player_MediaFailed; } catch { }

            player.MediaOpened += Player_MediaOpenedAsync;
            player.MediaEnded += Player_MediaEnded;
            player.MediaFailed += Player_MediaFailed;

            player.SetPlaybackSource(MediaSource.CreateFromStorageFile(file)); //Works with media playback..
            //player.Source = new Uri(mediasource, UriKind.RelativeOrAbsolute); //Doesn't work with media playback for some reason..
            Playing = true;
            Paused = false;
        }
        else
        {
            if (Paused)
            {
                player.Play();
                Paused = false;
            }
            else
            {
                player.Pause();
                Paused = true;
            }
        }
    }

    async Task<StorageFile> OpenFileAsync()
    {
        try
        {
            var ofd = new FileOpenPicker();
            ofd.FileTypeFilter.Add("*");
            return await ofd.PickSingleFileAsync();
        }
        catch { }
        return null;
    }