将 Resource 中的音频文件写入临时文件,然后使用 WMPLib 播放。
//Set up the temp path, I'm using a GUID for the file name to avoid any conflicts
var temporaryFilePath = String.Format("{0}{1}{2}", System.IO.Path.GetTempPath(), Guid.NewGuid().ToString("N"), ".mp3") ;
//Your resource accessor, my resource is called AudioFile
using (var memoryStream = new MemoryStream(Properties.Resources.AudioFile))
using(var tempFileStream = new FileStream(temporaryFilePath, FileMode.Create, FileAccess.Write))
{
//Set the memory stream position to 0
memoryStream.Position = 0;
//Reads the bytes from the audio file in resource, and writes them to the file
memoryStream.WriteTo(tempFileStream);
}
//Play your file
WMPLib.WindowsMediaPlayer wplayer = new WMPLib.WindowsMediaPlayer();
wplayer.URL = temporaryFilePath;
wplayer.settings.setMode("loop", false);
wplayer.controls.play();
//Delete the file after use
if(File.Exists(temporaryFilePath))
File.Delete(temporaryFilePath);