.NET Framework 1.xにはWAVEファイルの再生に関するクラスがないので、API関数のPlaySoundを用いる必要がある。 PlaySound関数はファイルから直接再生するバージョンと、メモリ上にあるバッファから再生するバージョンがある。 何度も再生されるような音声は、バッファを作成した方が効率的。
' ファイルから再生する <DllImport("winmm.dll", EntryPoint:="PlaySound")> _ Private Shared Function PlaySound( _ <MarshalAs(UnmanagedType.LPStr)> ByVal pszSound As String, _ ByVal hModule As Integer, _ ByVal dwFlags As Integer) _ As Integer End Function ' メモリ上のバッファから再生する <DllImport("winmm.dll", EntryPoint:="PlaySound")> _ Private Shared Function PlaySound( _ <MarshalAs(UnmanagedType.LPArray)> ByVal pszSound As Byte(), _ ByVal hModule As Integer, _ ByVal dwFlags As Integer) _ As Integer End Function ' 再生フラグ Private Const SND_SYNC As Integer = &H0 ' 同期再生 Private Const SND_ASYNC As Integer = &H1 ' 非同期再生 Private Const SND_MEMORY As Integer = &H4 ' バッファからの再生 Private Const SND_LOOP As Integer = &H8 ' ループ再生 Private Const SND_NOSTOP As Integer = &H10 ' 再生中のサウンドを停止しない Private Const SND_NOWAIT As Integer = &H2000 ' ビジー状態なら即座に処理を返す ' バッファ Dim buffer() As Byte Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ' ファイルからバイト配列に読み込み Dim stream As FileStream Dim reader As BinaryReader stream = New FileStream("E:\Test.wav", FileMode.Open, FileAccess.Read) reader = New BinaryReader(stream) buffer = reader.ReadBytes(CInt(stream.Length)) reader.Close() stream.Close() End Sub Private Sub Form1_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed buffer = Nothing End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click ' ファイルから再生 PlaySound("E:\Test.wav", Nothing, SND_ASYNC) End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click ' バッファから再生 PlaySound(buffer, Nothing, SND_MEMORY Or SND_ASYNC) End Sub_ Private Shared Function PlaySound( _ByVal pszSound As String, _ ByVal hModule As Integer, _ ByVal dwFlags As Integer) _ As Integer End Function ' メモリ上のバッファから再生する _ Private Shared Function PlaySound( _ ByVal pszSound As Byte(), _ ByVal hModule As Integer, _ ByVal dwFlags As Integer) _ As Integer End Function ' 再生フラグ Private Const SND_SYNC As Integer = &H0 ' 同期再生 Private Const SND_ASYNC As Integer = &H1 ' 非同期再生 Private Const SND_MEMORY As Integer = &H4 ' バッファからの再生 Private Const SND_LOOP As Integer = &H8 ' ループ再生 Private Const SND_NOSTOP As Integer = &H10 ' 再生中のサウンドを停止しない Private Const SND_NOWAIT As Integer = &H2000 ' ビジー状態なら即座に処理を返す ' バッファ Dim buffer() As Byte Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ' ファイルからバイト配列に読み込み Dim stream As FileStream Dim reader As BinaryReader stream = New FileStream("E:\Test.wav", FileMode.Open, FileAccess.Read) reader = New BinaryReader(stream) buffer = reader.ReadBytes(CInt(stream.Length)) reader.Close() stream.Close() End Sub Private Sub Form1_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed buffer = Nothing End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click ' ファイルから再生 PlaySound("E:\Test.wav", Nothing, SND_ASYNC) End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click ' バッファから再生 PlaySound(buffer, Nothing, SND_MEMORY Or SND_ASYNC) End Sub]]>
// ファイルから再生する [DllImport( "winmm.dll", EntryPoint = "PlaySound" )] private static extern int PlaySound( [MarshalAs(UnmanagedType.LPStr)] string pszSound, int hModule, int dwFlags ); // メモリ上のバッファから再生する [DllImport( "winmm.dll", EntryPoint = "PlaySound" )] private static extern int PlaySound( [MarshalAs(UnmanagedType.LPArray)] byte[] pszSound, int hModule, int dwFlags ); // 再生フラグ private const int SND_SYNC = 0x0; // 同期再生 private const int SND_ASYNC = 0x1; // 非同期再生 private const int SND_MEMORY = 0x4; // バッファからの再生 private const int SND_LOOP = 0x8; // ループ再生 private const int SND_NOSTOP = 0x10; // 再生中のサウンドを停止しない private const int SND_NOWAIT = 0x2000; // ビジー状態なら即座に処理を返す // バッファ byte[] buffer; private void Form1_Load( System.Object sender, System.EventArgs e ) { // ファイルからバイト配列に読み込み FileStream stream; BinaryReader reader; stream = new FileStream( @"E:\Test.wav", FileMode.Open, FileAccess.Read ); reader = new BinaryReader( stream ); buffer = reader.ReadBytes( (int)stream.Length ); reader.Close(); stream.Close(); } private void Form1_Closed( object sender, System.EventArgs e ) { buffer = null; } private void Button1_Click( System.Object sender, System.EventArgs e ) { // ファイルから再生 PlaySound( @"E:\Test.wav", 0, SND_ASYNC ); } private void Button2_Click( System.Object sender, System.EventArgs e ) { // バッファから再生 PlaySound( buffer, 0, SND_MEMORY | SND_ASYNC ); }
なお、.NET Framework 2.0以降はPlaySound関数の代わりにSystem.Media.SoundPlayerクラスまたはSystem.Media.SystemSoundsクラスを用いることができる。
Dim sound As New SoundPlayer() sound.SoundLocation = "D:\Test.wav" sound.LoadAsync() ' 読み込みが完了するまで待つ Do Until sound.IsLoadCompleted Thread.Sleep(50) Console.WriteLine("Loading...") Loop ' 同期的に再生 sound.Play() ' 1000ms再生して停止 Thread.Sleep(1000) sound.Stop() sound.Dispose()