Playing embedded resource mp3 file

橙三吉。 提交于 2019-12-24 08:14:42

问题


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);
    }
}

Thanks in advance


回答1:


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();  
} } }


来源:https://stackoverflow.com/questions/14002046/playing-embedded-resource-mp3-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!