如何从资源在C#中播放.MP3文件? [英] How to play .mp3 file from resources in C#?

查看:107
本文介绍了如何从资源在C#中播放.MP3文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我把music.mp3资源,然后我加入Windows媒体播放器的参考。我写这篇code:

I put music.mp3 in resources and then I added Windows Media Player to references. I wrote this code:

WindowsMediaPlayer wmp = new WindowsMediaPlayer();
            wmp.URL = "music.mp3";
            wmp.controls.play();

这是行不通的。如何从资源方面发挥的.mp3文件?

It doesn't work. How can I play .mp3 file from resources?

推荐答案

我做到了:

WindowsMediaPlayer wmp = new WindowsMediaPlayer();
        Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("PostGen.Resources.Kalimba.mp3");
        using (Stream output = new FileStream ("C:\\temp.mp3", FileMode.Create))
        {
            byte[] buffer = new byte[32*1024];
            int read;

            while ( (read= stream.Read(buffer, 0, buffer.Length)) > 0)
            {
                output.Write(buffer, 0, read);
            }
        }
        wmp.URL = "C:\\temp.mp3";
        wmp.controls.play();

我们要删除该临时文件:

We have to delete this temporary file:

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        File.Delete("C:\\temp.mp3");
    }

这篇关于如何从资源在C#中播放.MP3文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆