简体   繁体   English

Windows 8 Metro App中的Mediaelement

[英]Mediaelement in Windows 8 Metro App

I have a scenario specific to my app. 我有一个特定于我的应用程序的方案。 I am managing music file playlist in XML for my metro app. 我正在为我的metro应用程序管理XML格式的音乐文件播放列表。 And its saving music files actual path like this 它保存的音乐文件就是这样的实际路径

D:\\MyMedia\\1.mp3 d:\\玛雅\\ 1.MP3

I have media element in my XAML page and I am setting its Source like this. 我的XAML页面中有media元素,我正在设置它的Source。

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

but its not playing the media and its giving the error like this 但它没有播放媒体,而是给出了这样的错误

MF_MEDIA_ENGINE_ERR_SRC_NOT_SUPPORTED : HRESULT - 0x80070005

So someone tell me how I can play some media file in MediaElement of metro app with absoulte path. 所以有人告诉我如何使用absoulte路径在Metro应用程序的MediaElement中播放一些媒体文件。 Or how I can get stream of my local file to play this media in my mediaElement of Metro app. 或者我如何获取我的本地文件流以在我的MediaElement Metro应用程序中播放此媒体。

To open files on the local system, you can use the FileOpenPicker to get the file and SetSource to set the media source. 要在本地系统上打开文件,可以使用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();
}

There is only a limited number of locations on a user's PC that you can access. 您可以访问的用户PC上只有有限数量的位置。 "D:\\Mymedia" is not one of those. “D:\\ Mymedia”不是其中之一。 You will find all the necessary information in the Microsoft help. 您将在Microsoft帮助中找到所有必要的信息。 Check out these two articles: 看看这两篇文章:

This can be done without a file picker. 这可以在没有文件选择器的情况下完成。 You just have to add Music Library capabilities to your app manifest, make sure the music is in My Music or on an SD Card, and use KnownFolders.MusicLibrary. 您只需将音乐库功能添加到您的应用清单,确保音乐在我的音乐或SD卡中,并使用KnownFolders.MusicLibrary。

In short and in keeping with the music theme of the question: 简而言之,与问题的音乐主题保持一致:

"You might think file picker... “你可能会认为文件选择器......

...but all I need is StorageFile" ......但我需要的只是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();
            }
        }
    }

I know it's an old problem, I found a really simple solution.. 我知道这是一个老问题,我找到了一个非常简单的解决方案..

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

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

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