2012-12-22 191 views
1

我試圖播放一個mp3文件,它嵌入在我的c#應用程序(winforms)中,但沒有結果。我不想從資源創建文件並播放它。我搜索了互聯網,但沒有找到任何工作示例。他們都從資源創建一個文件並保存,然後將文件路徑傳遞給mci或wmp。是否有可能傳遞一個流?播放嵌入式資源mp3文件

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

在此先感謝

+0

然後你如何使用這個'filestream'? – ChrisF

+0

我已更新我的代碼,我想使用流而不是文件路徑。命令不會是一個字符串,我猜。 – mjekov

回答

0

你會EED到MP3轉換成可播放的格式。您已經擁有MP3流,因此您可以使用NAudio等功能轉換爲WAV流。一旦你完成了這個,你可以使用SoundPlayer類。你得到如下的東西。

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