扩展功能后的媒体播放

GitHub:https://github.com/Tong12138/MediaPlayer

点击下载音乐后效果如下

打开本地 html python 打开本地音乐文件夹_技术问题

 

textbox允许用户自己输入网址,如果为空则下载neusong。

技术问题1

如何找到用户的音乐文件夹

首先,我想到的是检索一下带有music关键字的文件夹,但是我在网上搜索了很长时间也没能找到一个让我满意的答案,能满足老师例子中列出的各种情况。我就想到这是一个windows自带的音乐文件夹,应该是会有确定的方法来打开它,然后我就找到了如下网址:https://docs.microsoft.com/zh-cn/windows/uwp/files/quickstart-managing-folders-in-the-music-pictures-and-videos-libraries

网页中提到的代码如下,我知道了该如何将一个文件存入到指定音乐库的文件中。

 

StorageFolder testFolder = await StorageFolder.GetFolderFromPathAsync(@"C:\test");
StorageFile sourceFile = await testFolder.GetFileAsync("TestImage.jpg");
StorageFile destinationFile = await KnownFolders.CameraRoll.CreateFileAsync("MyTestImage.jpg");

using (var sourceStream = await sourceFile.OpenReadAsync())
{
    using (var sourceInputStream = sourceStream.GetInputStreamAt(0))
    {
        using (var destinationStream = await destinationFile.OpenAsync(FileAccessMode.ReadWrite))
        {
            using (var destinationOutputStream = destinationStream.GetOutputStreamAt(0))
            {
                await RandomAccessStream.CopyAndCloseAsync(sourceInputStream, destinationStream);
            }
        }
    }
}

 

 

技术问题2

如何从网上下载文件,我查询了一下在网上下载文件的方法,找到了如下网址。

   该网址介绍了应该如何下载文件,并且知道了保存文件的方式可以覆盖同名文件。

public async Task<StorageFile> Load()
{
    try
    {
        var httpClient = new HttpClient();
        var buffer = await httpClient.GetBufferAsync(new Uri("http://dx.sc.chinaz.com/Files/DownLoad/sound1/201207/1771.mp3"));
        if (buffer != null && buffer.Length > 0u)
        {
            var file = await ApplicationData.Current.LocalCacheFolder.CreateFileAsync("1771.mp3", CreationCollisionOption.ReplaceExisting);
            using (var stream = await file.OpenAsync(FileAccessMode.ReadWrite))
            {
                await stream.WriteAsync(buffer);
                await stream.FlushAsync();
            }
            return file;
        }
    }
    catch { }
    return null;
}

  

根据以上两个网址,我整合了代码,但是,刚开始的时候可以运行,但是再次打开后就不能运行了,(着急改错,代码删掉了……),于是我仔细看了一下这段代码,发现下面的下载是已经将网址上的文件保存好了,如果我只是盲目的将两段代码合在一起的话,就是将一个文件打开再将它写会去,这是不对的。我用在官网上看到了如下代码

https://docs.microsoft.com/zh-cn/windows/uwp/networking/httpclient

try
{
    //Send the GET request
    httpResponse = await httpClient.GetAsync(requestUri);
    httpResponse.EnsureSuccessStatusCode();
    httpResponseBody = await httpResponse.Content.ReadAsStringAsync();
}
catch (Exception ex)
{
    httpResponseBody = "Error: " + ex.HResult.ToString("X") + " Message: " + ex.Message;
}

在了解之后发现httpResponse可以转换为流文件

于是我写了如下代码

代码如下

 

StorageFile file = await KnownFolders.MusicLibrary.CreateFileAsync(Filename,CreationCollisionOption.ReplaceExisting);

            HttpClient httpClient = new HttpClient();
            HttpResponseMessage httpResponse = new Windows.Web.Http.HttpResponseMessage();
            string httpResponseBody = "";
            try
            {
                //Send the GET request
                httpResponse = await httpClient.GetAsync(uri);
                httpResponse.EnsureSuccessStatusCode();
                httpResponseBody = await httpResponse.Content.ReadAsStringAsync();
                var sourceStream= await httpResponse.Content.ReadAsInputStreamAsync();
                    using (var destinationStream = await file.OpenAsync(FileAccessMode.ReadWrite))
                    {
                        using (var destinationOutputStream = destinationStream.GetOutputStreamAt(0))
                        {
                            await RandomAccessStream.CopyAndCloseAsync(sourceStream, destinationStream);
                        }
                    }

                var stream1 = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
                mediaplayer.SetSource(stream1, file.ContentType);
                mediaplayer.Play();
                filename.Text = Filename;

            }
            catch (Exception ex)
            {
                httpResponseBody = "Error: " + ex.HResult.ToString("X") + " Message: " + ex.Message;
            }

  终于可以成功了。

一点小感悟

以后找资料的时候主要还是应该找官方文件,里面的内容还是原理比较多,明白了基本内容才能更好的应用。还有就是以后在写博客的时候,代码写到什么程度就在博客上写一点,这样才不会丢代码,只是知道当时哪里错了。