方法 : サウンドの再生

[このドキュメントはプレビュー版であり、後のリリースで変更されることがあります。 空白のトピックは、プレースホルダーとして挿入されています。]

この例ではプラットフォームを使用する方法を示します埋め込みリソースやコンテンツとして、1、2 つの WAV ファイルを再生する起動します。

To implement your WAV file as an embedded resource in Visual Studio, in the Properties pane, set its BuildAction property to "Embedded Resource."コマンドラインでコンパイルは、埋め込みリソースをコンパイルする方法については、方法 : コマンド プロンプトでコンパイルします。 表示します。

注意

.NET Compact Framework バージョン 3. 5 は、サウンドを再生する SoundPlayer をサポートします。詳細については、「.NET Compact Framework で SoundPlayer」を参照してください。

次の使用例は、Windows CE の CoreDll.dll を使用して次ネイティブ コード機能を提供する Sound クラスを定義します。

  • プラットフォーム呼び出しメソッド宣言は、ファイル名またはストリームを使用してサウンドを再生します。

  • プラットフォームで引数を渡すためのビット値の列挙体はメソッドの呼び出しを呼び出します。

  • Play メソッドを呼び出し、正しいプラットフォームは、別のファイルまたは埋め込みリソースを再生するメソッドを呼び出します。

次の使用例は埋め込みリソースとしてコンパイルする Chimes.wav という名前のファイルをサウンド使用します。 Visual Studioでプロジェクトに追加するサウンド ファイルと、 のソリューション エクスプローラーで、埋め込まれたリソース に、ビルド アクション プロパティを設定する必要があります。 埋め込みリソースとしてサウンド ファイルを含める場合は前アセンブリ名前空間を GetManifestResourceStream への呼び出しで元のファイルの名前を付けますファイルのリソース ストリームを返します。

注意

また、Visual Studio 内のプロジェクト リソースとして、サウンド ファイルを含めるでき、コード内で名前で、リソースを参照できます。詳細については、「リソースの追加と編集 (Visual C#)」または「方法 : Visual Basic でのオーディオ リソースを取得します。」を参照してください。

プラットフォームを使用してサウンドを再生するには、呼び出し

  1. Sound クラスをプロジェクトに追加します。

                                Public
                                Class Sound
       Private m_soundBytes() AsBytePrivate m_fileName AsStringPublicDeclareFunction WCE_PlaySound Lib"CoreDll.dll"Alias"PlaySound" (ByVal szSound AsString, ByVal hMod As IntPtr, ByVal flags AsInteger) AsIntegerPublicDeclareFunction WCE_PlaySoundBytes Lib"CoreDll.dll"Alias"PlaySound" (ByVal szSound() AsByte, ByVal hMod As IntPtr, ByVal flags AsInteger) AsIntegerPrivateEnum Flags
          SND_SYNC = &H0 ' play synchronously (default) 
          SND_ASYNC = &H1 ' play asynchronously 
          SND_NODEFAULT = &H2 ' silence (!default) if sound not found 
          SND_MEMORY = &H4 ' pszSound points to a memory file 
          SND_LOOP = &H8 ' loop the sound until next sndPlaySound 
          SND_NOSTOP = &H10 ' don't stop any currently playing sound 
          SND_NOWAIT = &H2000 ' don't wait if the driver is busy 
          SND_ALIAS = &H10000 ' name is a registry alias 
          SND_ALIAS_ID = &H110000 ' alias is a predefined ID 
          SND_FILENAME = &H20000 ' name is file name 
          SND_RESOURCE = &H40004 ' name is resource name or atom 
        EndEnum
    
        ' Construct the Sound object to play sound data from the specified file.PublicSubNew(ByVal fileName AsString)
            m_fileName = fileName
        EndSub
    
        ' Construct the Sound object to play sound data from the specified stream.PublicSubNew(ByVal stream As Stream)
            ' read the data from the stream
            m_soundBytes = NewByte(stream.Length) {}
            stream.Read(m_soundBytes, 0, Fix(stream.Length))
        EndSub 'New
    
        ' Play the soundPublicSub Play()
            ' If a file name has been registered, call WCE_PlaySound,         ' otherwise call WCE_PlaySoundBytes.IfNot (m_fileName IsNothing) Then
                WCE_PlaySound(m_fileName, IntPtr.Zero, Fix(Flags.SND_ASYNC Or Flags.SND_FILENAME))
            Else
                WCE_PlaySoundBytes(m_soundBytes, IntPtr.Zero, Fix(Flags.SND_ASYNC Or Flags.SND_MEMORY))
            EndIfEndSubEndClass
    
                                public
                                class Sound
    {
        privatebyte[] m_soundBytes;
        privatestring m_fileName;
    
        privateenum Flags {
            SND_SYNC = 0x0000,  /* play synchronously (default) */
            SND_ASYNC = 0x0001,  /* play asynchronously */
            SND_NODEFAULT = 0x0002,  /* silence (!default) if sound not found */
            SND_MEMORY = 0x0004,  /* pszSound points to a memory file */
            SND_LOOP = 0x0008,  /* loop the sound until next sndPlaySound */
            SND_NOSTOP = 0x0010,  /* don't stop any currently playing sound */
            SND_NOWAIT = 0x00002000, /* don't wait if the driver is busy */
            SND_ALIAS = 0x00010000, /* name is a registry alias */
            SND_ALIAS_ID = 0x00110000, /* alias is a predefined ID */
            SND_FILENAME = 0x00020000, /* name is file name */
            SND_RESOURCE = 0x00040004  /* name is resource name or atom */
        }
    
        [DllImport("CoreDll.DLL", EntryPoint="PlaySound", SetLastError=true)]
        privateexternstaticint WCE_PlaySound(string szSound, IntPtr hMod, int flags);
    
        [DllImport("CoreDll.DLL", EntryPoint="PlaySound", SetLastError=true)]
        privateexternstaticint WCE_PlaySoundBytes (byte[] szSound, IntPtr hMod, int flags);
    
        /// <summary>/// Construct the Sound object to play sound data from the specified file./// </summary>public Sound (string fileName) {
            m_fileName = fileName;
        }
    
        /// <summary>/// Construct the Sound object to play sound data from the specified stream./// </summary>public Sound(Stream stream)    {
            // read the data from the stream
            m_soundBytes = newbyte [stream.Length];
            stream.Read(m_soundBytes, 0,(int)stream.Length);
        }
    
        /// <summary>/// Play the sound/// </summary>publicvoid Play () {
            // if a file name has been registered, call WCE_PlaySound,//  otherwise call WCE_PlaySoundBytesif (m_fileName != null)
                WCE_PlaySound(m_fileName, IntPtr.Zero, (int) (Flags.SND_ASYNC | Flags.SND_FILENAME));
            else
                WCE_PlaySoundBytes (m_soundBytes, IntPtr.Zero, (int) (Flags.SND_ASYNC | Flags.SND_MEMORY));
        }
    }
    
  2. Sound クラスのインスタンスを作成して、ボタンの Click イベントでなど、ファイルを再生するメソッドを追加します。

                                ' To return a Stream object associated with an embedded
                                ' resource, you must prepend the namespace to the original
                                ' name of the file in the project.
                                Private
                                Sub btnEmbedded_Click(ByVal sender AsObject, ByVal e As System.EventArgs) Handles btnEmbedded.Click
        Dim sound AsNew Sound([Assembly].GetExecutingAssembly().GetManifestResourceStream("SoundSample.chimes.wav"))
        sound.Play()
    EndSubPrivateSub btnFile_Click(ByVal sender AsObject, ByVal e As System.EventArgs) Handles btnFile.Click
        Dim sound AsNew Sound("Program Files\SoundSample\chord.wav")
        sound.Play()
    EndSub
    
                                // To return a Stream object associated with an embedded
                                // resource, you must prepend the namespace to the original
                                // name of the file in the project.
                                private
                                void btnEmbedded_Click(object sender, System.EventArgs e) {
        Sound sound = new Sound (Assembly.GetExecutingAssembly().GetManifestResourceStream("SoundSample.chimes.wav"));
        sound.Play();
    }
    
    privatevoid btnFile_Click(object sender, System.EventArgs e) {
        Sound sound = new Sound ("Program Files\\SoundSample\\chord.wav");
        sound.Play();
    }
    

コードのコンパイル方法

この例では、次の名前空間への参照が必要です。

参照

概念

.NET コンパクトなフレームワーク方法を説明したトピックの検索

その他の技術情報

.NET Compact Framework の相互運用性