播放嵌入式资源mp3文件 [英] Playing embedded resource mp3 file

查看:142
本文介绍了播放嵌入式资源mp3文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试播放mp3文件,该文件已嵌入我的c#应用程序(winforms)中,但没有结果.我不想从资源创建文件并播放它.我已经搜索了互联网,但没有找到任何可行的示例.它们都从资源创建文件并保存,然后将文件路径传递给mci或wmp.可以通过流吗?

I'm trying to play a mp3 file, which is embedded in my c# application (winforms) but with no result. I don't want to create a file from the resource and play it. I've searched the internet but haven't found any working examples. All of them are creating a file from the resource and save it, then pass the file path to mci or wmp. Is it possible to pass a stream?

public partial class Form1 : Form
{
    [DllImport("winmm.dll")]
    private static extern long mciSendString(string lpstrCommand, StringBuilder lpstrReturnString, int uReturnLength, int hwndCallback);
    public Form1()
    {
        InitializeComponent();
        Stream fileStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("mymp3.mp3");
        string command = "open" + fileStream not filePath + "type MPEGVideo alias MyMp3";
        mciSendString(command, null, 0, 0);
        command = "play MyMp3";
        mciSendString(command, null, 0, 0);
    }
}

预先感谢

推荐答案

您需要将MP3转换为可播放的格式.您已经有了MP3流,因此可以使用NAudio之类的东西将其转换为WAV流.完成此操作后,您可以使用SoundPlayer类.您得到类似以下内容.

You'd eed to convert the MP3 into a playable format. You already have the MP3 stream, so you can then use something like NAudio to convert to a WAV stream. Once you have done this you can use the SoundPlayer class. You get something like the following.

using (Mp3FileReader reader = new Mp3FileReader(fileStream)) {  
    using (WaveStream pcmStream = WaveFormatConversionStream.CreatePcmStream(reader)) {   
        SoundPlayer player = new SoundPlayer(pcmStream);  
        player.Play();  
} } }

这篇关于播放嵌入式资源mp3文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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