WAVEファイルを再生する

注意:
この文書は以前「.NETでいきまっしょい!」で公開していたものですが、公開以降メンテナンスされていません。 今や古い情報となった内容が記載されている場合があるのでご注意ください。

 .NET FrameworkにはWAVEファイルの再生に関するクラスがないので、API関数のPlaySoundを用いる必要がある。 PlaySound関数はファイルから直接再生するバージョンと、メモリ上にあるバッファから再生するバージョンがある。 何度も再生されるような音声は、バッファを作成した方が効率的。
VB.NET
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
' ファイルから再生する
<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
C#
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
// ファイルから再生する
[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 );
}