使用するための別の簡単なオプションは、次のようになります。
WindowsMediaPlayer myplayer = new WindowsMediaPlayer();
string mp3FileName = "music.mp3";
myplayer.URL = AppDomain.CurrentDomain.BaseDirectory + mp3FileName;
myplayer.controls.play();
これはあなたの実行ファイルが置かれているディレクトリからMP3を再生します何の反射が必要とされていないことに注意することも重要であり、その不必要なパフォーマンスコストを追加する可能性があります。それが追加された後、次のコードを実装することができ、資源としてMP3を埋め込むについてコメントをフォローアップとして
:
Assembly assembly = Assembly.GetExecutingAssembly();
string tmpMP3 = AppDomain.CurrentDomain.BaseDirectory + "temp.mp3";
using (Stream stream = assembly.GetManifestResourceStream("YourAssemblyName.music.mp3"))
using (Stream tmp = new FileStream(tmpMP3, FileMode.Create))
{
byte[] buffer = new byte[32 * 1024];
int read;
while ((read = stream.Read(buffer, 0, buffer.Length)) > 0)
{
// Creates a temporary MP3 file in the executable directory
tmp.Write(buffer, 0, read);
}
}
WindowsMediaPlayer myplayer = new WindowsMediaPlayer();
myplayer.URL = tmpMP3;
myplayer.controls.play();
// Checks the state of the player, and sends the temp file path for deletion
myplayer.PlayStateChange += (NewState) =>
{
Myplayer_PlayStateChange(NewState, tmpMP3);
};
private static void Myplayer_PlayStateChange(int NewState, string tmpMP3)
{
if (NewState == (int)WMPPlayState.wmppsMediaEnded)
{
// Deletes the temp MP3 file
File.Delete(tmpMP3);
}
}
もう一度やり直していただきありがとうございます!ちなみに、私はそのファイルがexeファイルに埋め込まれていることがわかりましたか?プロパティ/ビルドアクション - 埋め込みリソースについて私が正しければ、どうすればそれをmyplayerに呼び出すことができますか.URL? – ERS
これを達成する方法については、私の編集をご覧ください。 –
もう一度、ありがとう! – ERS