一个可以缓存的音乐播放器

3 篇文章 0 订阅

这次没有接着上次的做,主要是因为上次那个做的有点丑,不忍直视了。。

这次做一个可以缓存的音乐播放器

缓存的呢是NEU的校歌,其实想想很简单,就是主要获取URL的过程,获取URL的方法,我在文档中找到了

在这个mediaelement类中对各种空间有着很详细的介绍

那么我们就直接说了,、

private void LoadMediaFromString(string path)
        {
            try
            {
                media123.Source = MediaSource.CreateFromUri(new Uri(path));
            }
            catch (Exception ex)
            {
                if (ex is FormatException)
                {
                    // handle exception. 
                    // For example: Log error or notify user problem with file
                }
            }

        }

我们通过一个MediaSource.CreateFromUri(new Uri(path));来获取一个地址,这样就能够实现网络播放

然后缓存到本地要怎么解决?

private void DoenLoad_Click(object sender, RoutedEventArgs e)
        {


            media123.Source = MediaSource.CreateFromUri(new Uri(txtFilePath.Text));
            //  Debug.WriteLine(KnownFolders.MusicLibrary.); // 路径位置


            using (HttpClient httpClient = new HttpClient())
            {
                using (HttpResponseMessage response = httpClient.GetAsync(new Uri(txtFilePath.Text)).Result)
                {
                    var filename = txtFilePath.Text.Split('/');
                    Write(filename[filename.Length - 1], response.Content.ReadAsByteArrayAsync().Result);


                }
            }

        }

其实,我们就是仿照文件打开的方式,做缓存的,然后还有最重要的就是httpclient这个类

这个获取方法很像爬虫,老师上课的时候讲过,大家可以用当时做json web service那个样例程序来做榜样就行了。

然后就是和上面相辅相成的写了,有读文件就有写文件

private async void Write(string fileName, byte[] html)
        {
            try
            {
                StorageFolder folder = KnownFolders.MusicLibrary;
                StorageFile a = await folder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
                using (StorageStreamTransaction x = await a.OpenTransactedWriteAsync())
                {
                    using (DataWriter w = new DataWriter(x.Stream))
                    {
                        w.WriteBytes(html);
                        x.Stream.Size = await w.StoreAsync();
                        await x.CommitAsync();
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }
        }

    }

我的代码是这样的

然后附上github:https://github.com/656756130/media123

不多放图了,csdn还要过审核

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

旋转跳跃我闭着眼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值