UWP--Naïve Media Player, Part 2

Github连接:See Here


本次新增的两个功能:

第一,  在线播放

二,  将文件缓存到本地音乐文件夹中


功能一:在线播放,

        这个没有什么复杂的,就是将对应的Uri传给mediaplayer.sourse就行了,方法如下:

我的代码:

private void playOnline_Click(object sender,RoutedEventArgs e)

       {

            var mediaPlayer = new MediaPlayer();

            mediaPlayer.Source =MediaSource.CreateFromUri(new Uri("http://www.neu.edu.cn/indexsource/neusong.mp3"));

            mediaPlayer.Play();

       }

相关链接:See Here

官方文档代码:

private void LoadMediaFromString(string path)

{

    try

    {

        Uri pathUri = new Uri(path);

        mediaPlayer.Source =MediaSource.CreateFromUri(pathUri);

    }

    catch (Exception ex)

    {

        if (ex is FormatException)

        {

            // handleexception.

            // For example:Log error or notify user problem with file

        }

}

在这里我直接将Uri设置成http://www.neu.edu.cn/indexsource/neusong.mp3是为了方便演示,

其实可以在界面上直接输入Uri来设置,进行在线播放。

 

功能二:将文件缓存到本地音乐文件内

相关链接:See Here

官方代码:

private async void StartDownload_Click(object sender, RoutedEventArgse)

       {

            try

            {

                Uri source = new Uri(serverAddressField.Text.Trim());

                string destination = fileNameField.Text.Trim();

 

                StorageFile destinationFile = await KnownFolders.PicturesLibrary.CreateFileAsync(

                    destination,CreationCollisionOption.GenerateUniqueName)

                BackgroundDownloader downloader= new BackgroundDownloader();

                DownloadOperation download =downloader.CreateDownload(source, destinationFile);

                // Attach progressand completion handlers.

                HandleDownloadAsync(download, true);

            }

            catch (Exception ex)

            {

               LogException("Download Error", ex);

            }

        }

 

我的代码:

private async void download_Click(object sender,RoutedEventArgs e)

       {

            Uri source = newUri(serverAddressField.Text.Trim());

            string destination = fileNameField.Text.Trim();

 

            StorageFile destinationFile = awaitKnownFolders.MusicLibrary.CreateFileAsync(

                destination,CreationCollisionOption.GenerateUniqueName);

            BackgroundDownloader downloader = newBackgroundDownloader();

            DownloadOperation download =downloader.CreateDownload(source, destinationFile);

            await download.StartAsync();

        }

 

在这里一个问题是要获得相应的权限:如图

Package.appmainifest中,选择功能

选中音乐库

 

才可以运行通过,否者不行。
PS(问题:应用界面依然没有美化,看起来很挫,悲伤


阅读更多

没有更多推荐了,返回首页