SoundPlayerとは? わかりやすく解説

SoundPlayer イベント


パブリック イベントパブリック イベント

参照参照

関連項目

SoundPlayer クラス
System.Media 名前空間
SystemSounds

その他の技術情報

SoundPlayer クラスの概要

SoundPlayer クラス

メモ : このクラスは、.NET Framework version 2.0 で新しく追加されたものです。

.wav ファイルからのサウンドの再生を制御します。

名前空間: System.Media
アセンブリ: System (system.dll 内)
構文構文

<SerializableAttribute> _
Public Class SoundPlayer
    Inherits Component
    Implements ISerializable
[SerializableAttribute] 
public class SoundPlayer : Component, ISerializable
[SerializableAttribute] 
public ref class SoundPlayer : public
 Component, ISerializable
/** @attribute SerializableAttribute() */ 
public class SoundPlayer extends Component
 implements ISerializable
SerializableAttribute 
public class SoundPlayer extends
 Component implements ISerializable
解説解説

SoundPlayer クラスには、.wav ファイルの読み込みと再生のための簡単なインターフェイスが用意されています。SoundPlayer クラスは、ファイル パス、URL、または .wav ファイルを格納している Stream や埋め込みリソースからの .wav ファイルの読み込みをサポートしています。

SoundPlayer クラスを使用してサウンドを再生するには、SoundPlayer に .wav ファイルのパスを設定し、play メソッドの 1 つを呼び出します。いずれかのコンストラクタを使用するか、SoundLocation プロパティまたは Stream プロパティのどちらかを設定して、再生する .wav ファイルを指定できます。再生する前に、load メソッドの 1 つを使用してファイルを読み込むことも、いずれかの play メソッドが呼び出されるまで読み込みを延期することもできます。Stream または URL から .wav ファイルを読み込むように構成された SoundPlayer は、再生を開始する前に .wav ファイルをメモリに読み込む必要があります。

同期的または非同期的に、.wav ファイルを読み込んだり再生したりできます。同期的に実行される load メソッドまたは play メソッドを呼び出す場合、呼び出し元のスレッドはそのメソッドから制御が戻るまで待機します。この場合、描画などの他のイベントは中断される場合があります。非同期的に実行される load メソッドまたは play メソッドを呼び出す場合、呼び出し元のスレッドは中断されることなく続行できます。非同期メソッド呼び出しの詳細については、「方法 : バックグラウンドで操作を実行する」を参照してください。

SoundPlayer が .wav ファイルの読み込みを完了すると、LoadCompleted イベントが発生します。イベント ハンドラで AsyncCompletedEventArgs をチェックして、読み込みが成功したか失敗したかを判断できます。オーディオ ソースに新しいファイル パスまたは新しい URL が設定されると、SoundLocationChanged イベントが発生します。オーディオ ソースに新しい Stream が設定されると、StreamChanged イベントが発生します。イベント処理の詳細については、「イベントの利用」を参照してください。

SoundPlayer の詳細については、「SoundPlayer クラスの概要」を参照してください。

使用例使用例

SoundPlayer クラスを使用して、ローカル パスまたは URI (Uniform Resource Identifier) から .wav ファイルを再生するコード例を次に示します。

Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Diagnostics
Imports System.Drawing
Imports System.Media
Imports System.Windows.Forms

Public Class SoundTestForm
    Inherits System.Windows.Forms.Form
    Private label1 As System.Windows.Forms.Label
    Private WithEvents filepathTextbox As
 System.Windows.Forms.TextBox
    Private WithEvents playOnceSyncButton As
 System.Windows.Forms.Button
    Private WithEvents playOnceAsyncButton
 As System.Windows.Forms.Button
    Private WithEvents playLoopAsyncButton
 As System.Windows.Forms.Button
    Private WithEvents selectFileButton As
 System.Windows.Forms.Button

    Private WithEvents stopButton As
 System.Windows.Forms.Button
    Private statusBar As System.Windows.Forms.StatusBar
    Private WithEvents loadSyncButton As
 System.Windows.Forms.Button
    Private WithEvents loadAsyncButton As
 System.Windows.Forms.Button
    Private player As SoundPlayer


    Public Sub New()

        ' Initialize Forms Designer generated code.
        InitializeComponent()

        ' Disable playback controls until a valid .wav file 
        ' is selected.
        EnablePlaybackControls(False)

        ' Set up the status bar and other controls.
        InitializeControls()

        ' Set up the SoundPlayer object.
        InitializeSound()

    End Sub


    ' Sets up the status bar and other controls.
    Private Sub InitializeControls()

        ' Set up the status bar.
        Dim panel As New
 StatusBarPanel
        panel.BorderStyle = StatusBarPanelBorderStyle.Sunken
        panel.Text = "Ready."
        panel.AutoSize = StatusBarPanelAutoSize.Spring
        Me.statusBar.ShowPanels = True
        Me.statusBar.Panels.Add(panel)

    End Sub


    ' Sets up the SoundPlayer object.
    Private Sub InitializeSound()

        ' Create an instance of the SoundPlayer class.
        player = New SoundPlayer

        ' Listen for the LoadCompleted event.
        AddHandler player.LoadCompleted, AddressOf
 player_LoadCompleted

        ' Listen for the SoundLocationChanged event.
        AddHandler player.SoundLocationChanged, AddressOf
 player_LocationChanged

    End Sub


    Private Sub selectFileButton_Click(ByVal
 sender As Object, _
        ByVal e As System.EventArgs) Handles
 selectFileButton.Click

        ' Create a new OpenFileDialog.
        Dim dlg As New OpenFileDialog

        ' Make sure the dialog checks for existence of the 
        ' selected file.
        dlg.CheckFileExists = True

        ' Allow selection of .wav files only.
        dlg.Filter = "WAV files (*.wav)|*.wav"
        dlg.DefaultExt = ".wav"

        ' Activate the file selection dialog.
        If dlg.ShowDialog() = System.Windows.Forms.DialogResult.OK
 Then
            ' Get the selected file's path from the dialog.
            Me.filepathTextbox.Text = dlg.FileName

            ' Assign the selected file's path to the SoundPlayer object.
            player.SoundLocation = filepathTextbox.Text
        End If

    End Sub


    ' Convenience method for setting message text in 
    ' the status bar.
    Private Sub ReportStatus(ByVal
 statusMessage As String)

        ' If the caller passed in a message...
        If Not (statusMessage Is
 Nothing) AndAlso _
            statusMessage <> [String].Empty Then
            ' ...post the caller's message to the status bar.
            Me.statusBar.Panels(0).Text = statusMessage
        End If

    End Sub


    ' Enables and disables play controls.
    Private Sub EnablePlaybackControls(ByVal
 enabled As Boolean)

        Me.playOnceSyncButton.Enabled = enabled
        Me.playOnceAsyncButton.Enabled = enabled
        Me.playLoopAsyncButton.Enabled = enabled
        Me.stopButton.Enabled = enabled

    End Sub


    Private Sub filepathTextbox_TextChanged(ByVal
 sender As Object, _
        ByVal e As EventArgs) Handles
 filepathTextbox.TextChanged

        ' Disable playback controls until the new .wav is loaded.
        EnablePlaybackControls(False)

    End Sub


    Private Sub loadSyncButton_Click(ByVal
 sender As Object, _
        ByVal e As System.EventArgs) Handles
 loadSyncButton.Click

        ' Disable playback controls until the .wav is successfully
        ' loaded. The LoadCompleted event handler will enable them.
        EnablePlaybackControls(False)

        Try
            ' Assign the selected file's path to the SoundPlayer object.
            player.SoundLocation = filepathTextbox.Text

            ' Load the .wav file.
            player.Load()
        Catch ex As Exception
            ReportStatus(ex.Message)
        End Try

    End Sub


    Private Sub loadAsyncButton_Click(ByVal
 sender As System.Object, _
        ByVal e As System.EventArgs) Handles
 loadAsyncButton.Click

        ' Disable playback controls until the .wav is successfully
        ' loaded. The LoadCompleted event handler will enable them.
        EnablePlaybackControls(False)

        Try
            ' Assign the selected file's path to the SoundPlayer object.
            player.SoundLocation = Me.filepathTextbox.Text

            ' Load the .wav file.
            player.LoadAsync()
        Catch ex As Exception
            ReportStatus(ex.Message)
        End Try

    End Sub


    ' Synchronously plays the selected .wav file once.
    ' If the file is large, UI response will be visibly 
    ' affected.
    Private Sub playOnceSyncButton_Click(ByVal
 sender As Object, _
        ByVal e As System.EventArgs) Handles
 playOnceSyncButton.Click

        ReportStatus("Playing .wav file synchronously.")
        player.PlaySync()
        ReportStatus("Finished playing .wav file synchronously.")

    End Sub


    ' Asynchronously plays the selected .wav file once.
    Private Sub playOnceAsyncButton_Click(ByVal
 sender As Object, _
        ByVal e As System.EventArgs) Handles
 playOnceAsyncButton.Click

        ReportStatus("Playing .wav file asynchronously.")
        player.Play()

    End Sub


    ' Asynchronously plays the selected .wav file until the user
    ' clicks the Stop button.
    Private Sub playLoopAsyncButton_Click(ByVal
 sender As Object, _
        ByVal e As System.EventArgs) Handles
 playLoopAsyncButton.Click

        ReportStatus("Looping .wav file asynchronously.")
        player.PlayLooping()

    End Sub


    ' Stops the currently playing .wav file, if any.
    Private Sub stopButton_Click(ByVal
 sender As System.Object, _
        ByVal e As System.EventArgs) Handles
 stopButton.Click

        player.Stop()
        ReportStatus("Stopped by user.")

    End Sub


    ' Handler for the LoadCompleted event.
    Private Sub player_LoadCompleted(ByVal
 sender As Object, _
        ByVal e As AsyncCompletedEventArgs)

        Dim message As String
 = [String].Format("LoadCompleted: {0}", _
            Me.filepathTextbox.Text)
        ReportStatus(message)
        EnablePlaybackControls(True)

    End Sub

    ' Handler for the SoundLocationChanged event.
    Private Sub player_LocationChanged(ByVal
 sender As Object, _
        ByVal e As EventArgs)
        Dim message As String
 = [String].Format("SoundLocationChanged: {0}", _
            player.SoundLocation)
        ReportStatus(message)
    End Sub

    Private Sub InitializeComponent()
        Me.filepathTextbox = New System.Windows.Forms.TextBox
        Me.selectFileButton = New System.Windows.Forms.Button
        Me.label1 = New System.Windows.Forms.Label
        Me.loadSyncButton = New System.Windows.Forms.Button
        Me.playOnceSyncButton = New System.Windows.Forms.Button
        Me.playOnceAsyncButton = New System.Windows.Forms.Button
        Me.stopButton = New System.Windows.Forms.Button
        Me.playLoopAsyncButton = New System.Windows.Forms.Button
        Me.statusBar = New System.Windows.Forms.StatusBar
        Me.loadAsyncButton = New System.Windows.Forms.Button
        Me.SuspendLayout()
        ' 
        ' filepathTextbox
        ' 
        Me.filepathTextbox.Anchor = CType(System.Windows.Forms.AnchorStyles.Top
 Or System.Windows.Forms.AnchorStyles.Left Or
 System.Windows.Forms.AnchorStyles.Right, System.Windows.Forms.AnchorStyles)
        Me.filepathTextbox.Location = New System.Drawing.Point(7,
 25)
        Me.filepathTextbox.Name = "filepathTextbox"
        Me.filepathTextbox.Size = New System.Drawing.Size(263,
 20)
        Me.filepathTextbox.TabIndex = 1
        Me.filepathTextbox.Text = ""
        ' 
        ' selectFileButton
        ' 
        Me.selectFileButton.Anchor = CType(System.Windows.Forms.AnchorStyles.Top
 Or System.Windows.Forms.AnchorStyles.Right, System.Windows.Forms.AnchorStyles)
        Me.selectFileButton.Location = New
 System.Drawing.Point(276, 25)
        Me.selectFileButton.Name = "selectFileButton"
        Me.selectFileButton.Size = New System.Drawing.Size(23,
 21)
        Me.selectFileButton.TabIndex = 2
        Me.selectFileButton.Text = "..."
        ' 
        ' label1
        ' 
        Me.label1.Location = New System.Drawing.Point(7,
 7)
        Me.label1.Name = "label1"
        Me.label1.Size = New System.Drawing.Size(145,
 17)
        Me.label1.TabIndex = 3
        Me.label1.Text = ".wav path or URL:"
        ' 
        ' loadSyncButton
        ' 
        Me.loadSyncButton.Location = New System.Drawing.Point(7,
 53)
        Me.loadSyncButton.Name = "loadSyncButton"
        Me.loadSyncButton.Size = New System.Drawing.Size(142,
 23)
        Me.loadSyncButton.TabIndex = 4
        Me.loadSyncButton.Text = "Load Synchronously"
        ' 
        ' playOnceSyncButton
        ' 
        Me.playOnceSyncButton.Location = New
 System.Drawing.Point(7, 86)
        Me.playOnceSyncButton.Name = "playOnceSyncButton"
        Me.playOnceSyncButton.Size = New System.Drawing.Size(142,
 23)
        Me.playOnceSyncButton.TabIndex = 5
        Me.playOnceSyncButton.Text = "Play
 Once Synchronously"
        ' 
        ' playOnceAsyncButton
        ' 
        Me.playOnceAsyncButton.Location = New
 System.Drawing.Point(149, 86)
        Me.playOnceAsyncButton.Name = "playOnceAsyncButton"
        Me.playOnceAsyncButton.Size = New System.Drawing.Size(147,
 23)
        Me.playOnceAsyncButton.TabIndex = 6
        Me.playOnceAsyncButton.Text = "Play
 Once Asynchronously"
        ' 
        ' stopButton
        ' 
        Me.stopButton.Location = New System.Drawing.Point(149,
 109)
        Me.stopButton.Name = "stopButton"
        Me.stopButton.Size = New System.Drawing.Size(147,
 23)
        Me.stopButton.TabIndex = 7
        Me.stopButton.Text = "Stop"
        ' 
        ' playLoopAsyncButton
        ' 
        Me.playLoopAsyncButton.Location = New
 System.Drawing.Point(7, 109)
        Me.playLoopAsyncButton.Name = "playLoopAsyncButton"
        Me.playLoopAsyncButton.Size = New System.Drawing.Size(142,
 23)
        Me.playLoopAsyncButton.TabIndex = 8
        Me.playLoopAsyncButton.Text = "Loop
 Asynchronously"
        ' 
        ' statusBar
        ' 
        Me.statusBar.Location = New System.Drawing.Point(0,
 146)
        Me.statusBar.Name = "statusBar"
        Me.statusBar.Size = New System.Drawing.Size(306,
 22)
        Me.statusBar.SizingGrip = False
        Me.statusBar.TabIndex = 9
        Me.statusBar.Text = "(no status)"
        ' 
        ' loadAsyncButton
        ' 
        Me.loadAsyncButton.Location = New System.Drawing.Point(149,
 53)
        Me.loadAsyncButton.Name = "loadAsyncButton"
        Me.loadAsyncButton.Size = New System.Drawing.Size(147,
 23)
        Me.loadAsyncButton.TabIndex = 10
        Me.loadAsyncButton.Text = "Load Asynchronously"
        ' 
        ' SoundTestForm
        '
        Me.ClientSize = New System.Drawing.Size(306,
 168)
        Me.Controls.Add(loadAsyncButton)
        Me.Controls.Add(statusBar)
        Me.Controls.Add(playLoopAsyncButton)
        Me.Controls.Add(stopButton)
        Me.Controls.Add(playOnceAsyncButton)
        Me.Controls.Add(playOnceSyncButton)
        Me.Controls.Add(loadSyncButton)
        Me.Controls.Add(label1)
        Me.Controls.Add(selectFileButton)
        Me.Controls.Add(filepathTextbox)
        Me.MinimumSize = New System.Drawing.Size(310,
 165)
        Me.Name = "SoundTestForm"
        Me.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Show
        Me.Text = "Sound API Test Form"
        Me.ResumeLayout(False)
    End Sub 'InitializeComponent

    <STAThread()> _
    Shared Sub Main()
        Application.Run(New SoundTestForm)
    End Sub
End Class 'SoundTestForm
using System;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Media;
using System.Windows.Forms;

namespace SoundApiExample
{
    public class SoundTestForm : System.Windows.Forms.Form
    {
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.TextBox filepathTextbox;
        
        private System.Windows.Forms.Button playOnceSyncButton;
        private System.Windows.Forms.Button playOnceAsyncButton;
        private System.Windows.Forms.Button playLoopAsyncButton;
        private System.Windows.Forms.Button selectFileButton;
        
        private System.Windows.Forms.Button stopButton;
        private System.Windows.Forms.StatusBar statusBar;
        private System.Windows.Forms.Button loadSyncButton;
        private System.Windows.Forms.Button loadAsyncButton; 
       
        private SoundPlayer player;

        public SoundTestForm()
        {
            // Initialize Forms Designer generated code.
            InitializeComponent();
            
            // Disable playback controls until a valid .wav file 
            // is selected.
            EnablePlaybackControls(false);

            // Set up the status bar and other controls.
            InitializeControls();

            // Set up the SoundPlayer object.
            InitializeSound();
        }

        // Sets up the status bar and other controls.
        private void InitializeControls()
        {
            // Set up the status bar.
            StatusBarPanel panel = new StatusBarPanel();
            panel.BorderStyle = StatusBarPanelBorderStyle.Sunken;
            panel.Text = "Ready.";
            panel.AutoSize = StatusBarPanelAutoSize.Spring;
            this.statusBar.ShowPanels = true;
            this.statusBar.Panels.Add(panel);
        }

        // Sets up the SoundPlayer object.
        private void InitializeSound()
        {
            // Create an instance of the SoundPlayer class.
            player = new SoundPlayer();

            // Listen for the LoadCompleted event.
            player.LoadCompleted += new AsyncCompletedEventHandler(player_LoadCompleted);

            // Listen for the SoundLocationChanged event.
            player.SoundLocationChanged += new EventHandler(player_LocationChanged);
        }

        private void selectFileButton_Click(object
 sender, 
            System.EventArgs e)
        {
            // Create a new OpenFileDialog.
            OpenFileDialog dlg = new OpenFileDialog();

            // Make sure the dialog checks for existence of the 
            // selected file.
            dlg.CheckFileExists = true;

            // Allow selection of .wav files only.
            dlg.Filter = "WAV files (*.wav)|*.wav";
            dlg.DefaultExt = ".wav";

            // Activate the file selection dialog.
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                // Get the selected file's path from the dialog.
                this.filepathTextbox.Text = dlg.FileName;

                // Assign the selected file's path to 
                // the SoundPlayer object.  
                player.SoundLocation = filepathTextbox.Text;
            }
        }

        // Convenience method for setting message text in 
        // the status bar.
        private void ReportStatus(string
 statusMessage)
        {
            // If the caller passed in a message...
            if ((statusMessage != null) &&
 (statusMessage != String.Empty))
            {
                // ...post the caller's message to the status bar.
                this.statusBar.Panels[0].Text = statusMessage;
            }
        }
        
        // Enables and disables play controls.
        private void EnablePlaybackControls(bool
 enabled)
        {   
            this.playOnceSyncButton.Enabled = enabled;
            this.playOnceAsyncButton.Enabled = enabled;
            this.playLoopAsyncButton.Enabled = enabled;
            this.stopButton.Enabled = enabled;
        }
    
        private void filepathTextbox_TextChanged(object
 sender, 
            EventArgs e)
        {
            // Disable playback controls until the new .wav is loaded.
            EnablePlaybackControls(false);
        }

        private void loadSyncButton_Click(object
 sender, 
            System.EventArgs e)
        {   
            // Disable playback controls until the .wav is 
            // successfully loaded. The LoadCompleted event 
            // handler will enable them.
            EnablePlaybackControls(false);

            try
            {
                // Assign the selected file's path to 
                // the SoundPlayer object.  
                player.SoundLocation = filepathTextbox.Text;

                // Load the .wav file.
                player.Load();
            }
            catch (Exception ex)
            {
                ReportStatus(ex.Message);
            }
        }

        private void loadAsyncButton_Click(System.Object
 sender, 
            System.EventArgs e)
        {
            // Disable playback controls until the .wav is 
            // successfully loaded. The LoadCompleted event 
            // handler will enable them.
            EnablePlaybackControls(false);

            try
            {
                // Assign the selected file's path to 
                // the SoundPlayer object.  
                player.SoundLocation = this.filepathTextbox.Text;

                // Load the .wav file.
                player.LoadAsync();
            }
            catch (Exception ex)
            {
                ReportStatus(ex.Message);
            }
        }

        // Synchronously plays the selected .wav file once.
        // If the file is large, UI response will be visibly 
        // affected.
        private void playOnceSyncButton_Click(object
 sender, 
            System.EventArgs e)
        {    
            ReportStatus("Playing .wav file synchronously.");
            player.PlaySync();
            ReportStatus("Finished playing .wav file synchronously.");
        }

        // Asynchronously plays the selected .wav file once.
        private void playOnceAsyncButton_Click(object
 sender, 
            System.EventArgs e)
        {
            ReportStatus("Playing .wav file asynchronously.");
            player.Play();
        }

        // Asynchronously plays the selected .wav file until the user
        // clicks the Stop button.
        private void playLoopAsyncButton_Click(object
 sender, 
            System.EventArgs e)
        {
            ReportStatus("Looping .wav file asynchronously.");
            player.PlayLooping();
        }

        // Stops the currently playing .wav file, if any.
        private void stopButton_Click(System.Object
 sender,
            System.EventArgs e)
        {    
            player.Stop();
            ReportStatus("Stopped by user.");
        }

        // Handler for the LoadCompleted event.
        private void player_LoadCompleted(object
 sender, 
            AsyncCompletedEventArgs e)
        {   
            string message = String.Format("LoadCompleted:
 {0}", 
                this.filepathTextbox.Text);
            ReportStatus(message);
            EnablePlaybackControls(true);
        }

        // Handler for the SoundLocationChanged event.
        private void player_LocationChanged(object
 sender, EventArgs e)
        {   
            string message = String.Format("SoundLocationChanged:
 {0}", 
                player.SoundLocation);
            ReportStatus(message);
        }

        #region Windows Form Designer generated code
        private void InitializeComponent()
        {
            this.filepathTextbox = new System.Windows.Forms.TextBox();
            this.selectFileButton = new System.Windows.Forms.Button();
            this.label1 = new System.Windows.Forms.Label();
            this.loadSyncButton = new System.Windows.Forms.Button();
            this.playOnceSyncButton = new System.Windows.Forms.Button();
            this.playOnceAsyncButton = new
 System.Windows.Forms.Button();
            this.stopButton = new System.Windows.Forms.Button();
            this.playLoopAsyncButton = new
 System.Windows.Forms.Button();
            this.statusBar = new System.Windows.Forms.StatusBar();
            this.loadAsyncButton = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // filepathTextbox
            // 
            this.filepathTextbox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top
 | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right)));
            this.filepathTextbox.Location = new
 System.Drawing.Point(7, 25);
            this.filepathTextbox.Name = "filepathTextbox";
            this.filepathTextbox.Size = new
 System.Drawing.Size(263, 20);
            this.filepathTextbox.TabIndex = 1;
            this.filepathTextbox.Text = "";
            this.filepathTextbox.TextChanged += new
 System.EventHandler(this.filepathTextbox_TextChanged);
            // 
            // selectFileButton
            // 
            this.selectFileButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top
 | System.Windows.Forms.AnchorStyles.Right)));
            this.selectFileButton.Location = new
 System.Drawing.Point(276, 25);
            this.selectFileButton.Name = "selectFileButton";
            this.selectFileButton.Size = new
 System.Drawing.Size(23, 21);
            this.selectFileButton.TabIndex = 2;
            this.selectFileButton.Text = "...";
            this.selectFileButton.Click += new
 System.EventHandler(this.selectFileButton_Click);
            // 
            // label1
            // 
            this.label1.Location = new System.Drawing.Point(7,
 7);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(145,
 17);
            this.label1.TabIndex = 3;
            this.label1.Text = ".wav path or URL:";
            // 
            // loadSyncButton
            // 
            this.loadSyncButton.Location = new
 System.Drawing.Point(7, 53);
            this.loadSyncButton.Name = "loadSyncButton";
            this.loadSyncButton.Size = new
 System.Drawing.Size(142, 23);
            this.loadSyncButton.TabIndex = 4;
            this.loadSyncButton.Text = "Load Synchronously";
            this.loadSyncButton.Click += new
 System.EventHandler(this.loadSyncButton_Click);
            // 
            // playOnceSyncButton
            // 
            this.playOnceSyncButton.Location = new
 System.Drawing.Point(7, 86);
            this.playOnceSyncButton.Name = "playOnceSyncButton";
            this.playOnceSyncButton.Size = new
 System.Drawing.Size(142, 23);
            this.playOnceSyncButton.TabIndex = 5;
            this.playOnceSyncButton.Text = "Play Once Synchronously";
            this.playOnceSyncButton.Click += new
 System.EventHandler(this.playOnceSyncButton_Click);
            // 
            // playOnceAsyncButton
            // 
            this.playOnceAsyncButton.Location = new
 System.Drawing.Point(149, 86);
            this.playOnceAsyncButton.Name = "playOnceAsyncButton";
            this.playOnceAsyncButton.Size = new
 System.Drawing.Size(147, 23);
            this.playOnceAsyncButton.TabIndex = 6;
            this.playOnceAsyncButton.Text = "Play Once Asynchronously";
            this.playOnceAsyncButton.Click += new
 System.EventHandler(this.playOnceAsyncButton_Click);
            // 
            // stopButton
            // 
            this.stopButton.Location = new
 System.Drawing.Point(149, 109);
            this.stopButton.Name = "stopButton";
            this.stopButton.Size = new System.Drawing.Size(147,
 23);
            this.stopButton.TabIndex = 7;
            this.stopButton.Text = "Stop";
            this.stopButton.Click += new System.EventHandler(this.stopButton_Click);
            // 
            // playLoopAsyncButton
            // 
            this.playLoopAsyncButton.Location = new
 System.Drawing.Point(7, 109);
            this.playLoopAsyncButton.Name = "playLoopAsyncButton";
            this.playLoopAsyncButton.Size = new
 System.Drawing.Size(142, 23);
            this.playLoopAsyncButton.TabIndex = 8;
            this.playLoopAsyncButton.Text = "Loop Asynchronously";
            this.playLoopAsyncButton.Click += new
 System.EventHandler(this.playLoopAsyncButton_Click);
            // 
            // statusBar
            // 
            this.statusBar.Location = new System.Drawing.Point(0,
 146);
            this.statusBar.Name = "statusBar";
            this.statusBar.Size = new System.Drawing.Size(306,
 22);
            this.statusBar.SizingGrip = false;
            this.statusBar.TabIndex = 9;
            this.statusBar.Text = "(no status)";
            // 
            // loadAsyncButton
            // 
            this.loadAsyncButton.Location = new
 System.Drawing.Point(149, 53);
            this.loadAsyncButton.Name = "loadAsyncButton";
            this.loadAsyncButton.Size = new
 System.Drawing.Size(147, 23);
            this.loadAsyncButton.TabIndex = 10;
            this.loadAsyncButton.Text = "Load Asynchronously";
            this.loadAsyncButton.Click += new
 System.EventHandler(this.loadAsyncButton_Click);
            // 
            // SoundTestForm
            // 
            this.ClientSize = new System.Drawing.Size(306,
 168);
            this.Controls.Add(this.loadAsyncButton);
            this.Controls.Add(this.statusBar);
            this.Controls.Add(this.playLoopAsyncButton);
            this.Controls.Add(this.stopButton);
            this.Controls.Add(this.playOnceAsyncButton);
            this.Controls.Add(this.playOnceSyncButton);
            this.Controls.Add(this.loadSyncButton);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.selectFileButton);
            this.Controls.Add(this.filepathTextbox);
            this.MinimumSize = new System.Drawing.Size(310,
 165);
            this.Name = "SoundTestForm";
            this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Show;
            this.Text = "Sound API Test Form";
            this.ResumeLayout(false);

        }
        #endregion
        
        [STAThread]
        static void Main()
        {
            Application.Run(new SoundTestForm());
        }
    }
}
継承階層継承階層
System.Object
   System.MarshalByRefObject
     System.ComponentModel.Component
      System.Media.SoundPlayer
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバの場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

SoundPlayer コンストラクタ ()


SoundPlayer コンストラクタ (Stream)

メモ : このコンストラクタは、.NET Framework version 2.0 で新しく追加されたものです。

SoundPlayer クラスの新しいインスタンスを初期化し、指定した Stream 内で .wav ファイルを結び付けます。

名前空間: System.Media
アセンブリ: System (system.dll 内)
構文構文

解説解説
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

SoundPlayer コンストラクタ (String)

メモ : このコンストラクタは、.NET Framework version 2.0 で新しく追加されたものです。

SoundPlayer クラスの新しいインスタンスを初期化し、指定した .wav ファイルを結び付けます。

名前空間: System.Media
アセンブリ: System (system.dll 内)
構文構文

Public Sub New ( _
    soundLocation As String _
)
Dim soundLocation As String

Dim instance As New SoundPlayer(soundLocation)
public SoundPlayer (
    string soundLocation
)
public:
SoundPlayer (
    String^ soundLocation
)
public SoundPlayer (
    String soundLocation
)
public function SoundPlayer (
    soundLocation : String
)

パラメータ

soundLocation

読み込む .wav ファイルの場所。

例外例外
例外の種類条件

UriFormatException

soundLocation で指定した URL の値が解決できません。

解説解説

soundLocation パラメータに渡すことができる文字列は、.wav ファイルのファイル パスまたは URL のいずれかです。パスまたは URL が有効でない場合でも SoundPlayer は生成されますが、以降の load メソッドまたは play メソッドの呼び出しは失敗します。

プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

SoundPlayer コンストラクタ (SerializationInfo, StreamingContext)

メモ : このコンストラクタは、.NET Framework version 2.0 で新しく追加されたものです。

SoundPlayer クラスの新しいインスタンスを初期化します。

名前空間: System.Media
アセンブリ: System (system.dll 内)
構文構文

Protected Sub New ( _
    serializationInfo As SerializationInfo, _
    context As StreamingContext _
)
Dim serializationInfo As SerializationInfo
Dim context As StreamingContext

Dim instance As New SoundPlayer(serializationInfo,
 context)
protected SoundPlayer (
    SerializationInfo serializationInfo,
    StreamingContext context
)
protected:
SoundPlayer (
    SerializationInfo^ serializationInfo, 
    StreamingContext context
)
protected SoundPlayer (
    SerializationInfo serializationInfo, 
    StreamingContext context
)
protected function SoundPlayer (
    serializationInfo : SerializationInfo, 
    context : StreamingContext
)

パラメータ

serializationInfo

逆シリアル化に使用する SerializationInfo。

context

逆シリアル化に使用する転送先。

例外例外
例外の種類条件

UriFormatException

serializationInfo で指定された SoundLocation が解決できません。

プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

SoundPlayer コンストラクタ

SoundPlayer クラスの新しいインスタンスを初期化します。
オーバーロードの一覧オーバーロードの一覧

参照参照

関連項目

SoundPlayer クラス
SoundPlayer メンバ
System.Media 名前空間

その他の技術情報

SoundPlayer クラスの概要

SoundPlayer プロパティ


パブリック プロパティパブリック プロパティ

プロテクト プロパティプロテクト プロパティ
参照参照

関連項目

SoundPlayer クラス
System.Media 名前空間
SystemSounds

その他の技術情報

SoundPlayer クラスの概要

SoundPlayer メソッド


パブリック メソッドパブリック メソッド

( プロテクト メソッド も参照)
  名前 説明
パブリック メソッド CreateObjRef  リモート オブジェクトとの通信に使用するプロキシの生成に必要な情報をすべて格納しているオブジェクトを作成します。 ( MarshalByRefObject から継承されます。)
パブリック メソッド Dispose  オーバーロードされます。 Component によって使用されているリソースを解放します。 ( Component から継承されます。)
パブリック メソッド Equals  オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 ( Object から継承されます。)
パブリック メソッド GetHashCode  特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 ( Object から継承されます。)
パブリック メソッド GetLifetimeService  対象のインスタンスの有効期間ポリシーを制御する、現在の有効期間サービス オブジェクトを取得します。 ( MarshalByRefObject から継承されます。)
パブリック メソッド GetType  現在のインスタンスの Type を取得します。 ( Object から継承されます。)
パブリック メソッド InitializeLifetimeService  対象のインスタンスの有効期間ポリシーを制御する、有効期間サービス オブジェクトを取得します。 ( MarshalByRefObject から継承されます。)
パブリック メソッド Load サウンドを同期的に読み込みます。
パブリック メソッド LoadAsync 新しいスレッドを使用して、ストリームまたは Web リソースから .wav ファイルを読み込みます。
パブリック メソッド Play 新しいスレッドを使用して .wav ファイルを再生します。.wav ファイルが読み込まれていない場合は、まずそのファイルを読み込みます。
パブリック メソッド PlayLooping 新しいスレッドを使用し、.wav ファイルを反復して再生します。ファイルが読み込まれていない場合は、まずそのファイルを読み込みます。
パブリック メソッド PlaySync ユーザー インターフェイス (UI) スレッドを使用して .wav ファイルを再生します。.wav ファイルが読み込まれていない場合は、まずそのファイルを読み込みます。
パブリック メソッド ReferenceEquals  指定した複数の Object インスタンスが同一かどうかを判断します。 ( Object から継承されます。)
パブリック メソッド Stop 再生が行われている場合、サウンドの再生を中断します。
パブリック メソッド ToString  Component の名前を格納している String を返します (存在する場合)。このメソッドはオーバーライドできません。 ( Component から継承されます。)
プロテクト メソッドプロテクト メソッド
  名前 説明
プロテクト メソッド Dispose  オーバーロードされます。 Component によって使用されているリソースを解放します。 ( Component から継承されます。)
プロテクト メソッド Finalize  Component がガベージ コレクションによってクリアされる前に、アンマネージ リソースを解放し、その他のクリーンアップ操作を実行します。 ( Component から継承されます。)
プロテクト メソッド GetService  Component またはその Container で提供されるサービスを表すオブジェクトを返します。 ( Component から継承されます。)
プロテクト メソッド MemberwiseClone  オーバーロードされます。 ( MarshalByRefObject から継承されます。)
プロテクト メソッド OnLoadCompleted LoadCompleted イベントを発生させます。
プロテクト メソッド OnSoundLocationChanged SoundLocationChanged イベントを発生させます。
プロテクト メソッド OnStreamChanged StreamChanged イベントを発生させます。
明示的インターフェイスの実装明示的インターフェイスの実装
  名前 説明
インターフェイスの明示的な実装 System.Runtime.Serialization.ISerializable.GetObjectData このメンバの説明については、ISerializable.GetObjectData メソッドに関するトピックを参照してください。
参照参照

関連項目

SoundPlayer クラス
System.Media 名前空間
SystemSounds

その他の技術情報

SoundPlayer クラスの概要

SoundPlayer メンバ

.wav ファイルからのサウンドの再生を制御します。

SoundPlayer データ型で公開されるメンバを以下の表に示します。


パブリック コンストラクタパブリック コンストラクタ
プロテクト コンストラクタプロテクト コンストラクタ
パブリック プロパティパブリック プロパティ
プロテクト プロパティプロテクト プロパティ
パブリック メソッドパブリック メソッド
( プロテクト メソッド も参照)
  名前 説明
パブリック メソッド CreateObjRef  リモート オブジェクトとの通信に使用するプロキシの生成に必要な情報をすべて格納しているオブジェクトを作成します。 (MarshalByRefObject から継承されます。)
パブリック メソッド Dispose  オーバーロードされます。 Component によって使用されているリソースを解放します。 (Component から継承されます。)
パブリック メソッド Equals  オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 (Object から継承されます。)
パブリック メソッド GetHashCode  特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 (Object から継承されます。)
パブリック メソッド GetLifetimeService  対象のインスタンスの有効期間ポリシーを制御する、現在の有効期間サービス オブジェクトを取得します。 (MarshalByRefObject から継承されます。)
パブリック メソッド GetType  現在のインスタンスの Type を取得します。 (Object から継承されます。)
パブリック メソッド InitializeLifetimeService  対象のインスタンスの有効期間ポリシーを制御する、有効期間サービス オブジェクトを取得します。 (MarshalByRefObject から継承されます。)
パブリック メソッド Load サウンドを同期的に読み込みます。
パブリック メソッド LoadAsync 新しいスレッドを使用して、ストリームまたは Web リソースから .wav ファイルを読み込みます。
パブリック メソッド Play 新しいスレッドを使用して .wav ファイルを再生します。.wav ファイルが読み込まれていない場合は、まずそのファイルを読み込みます。
パブリック メソッド PlayLooping 新しいスレッドを使用し、.wav ファイルを反復して再生します。ファイルが読み込まれていない場合は、まずそのファイルを読み込みます。
パブリック メソッド PlaySync ユーザー インターフェイス (UI) スレッドを使用して .wav ファイルを再生します。.wav ファイルが読み込まれていない場合は、まずそのファイルを読み込みます。
パブリック メソッド ReferenceEquals  指定した複数の Object インスタンスが同一かどうかを判断します。 (Object から継承されます。)
パブリック メソッド Stop 再生が行われている場合、サウンドの再生を中断します。
パブリック メソッド ToString  Component の名前を格納している String を返します (存在する場合)。このメソッドはオーバーライドできません。 (Component から継承されます。)
プロテクト メソッドプロテクト メソッド
  名前 説明
プロテクト メソッド Dispose  オーバーロードされます。 Component によって使用されているリソースを解放します。 (Component から継承されます。)
プロテクト メソッド Finalize  Component がガベージ コレクションによってクリアされる前に、アンマネージ リソースを解放し、その他のクリーンアップ操作を実行します。 (Component から継承されます。)
プロテクト メソッド GetService  Component またはその Container で提供されるサービスを表すオブジェクトを返します。 (Component から継承されます。)
プロテクト メソッド MemberwiseClone  オーバーロードされます。 ( MarshalByRefObject から継承されます。)
プロテクト メソッド OnLoadCompleted LoadCompleted イベントを発生させます。
プロテクト メソッド OnSoundLocationChanged SoundLocationChanged イベントを発生させます。
プロテクト メソッド OnStreamChanged StreamChanged イベントを発生させます。
パブリック イベントパブリック イベント
明示的インターフェイスの実装明示的インターフェイスの実装
  名前 説明
インターフェイスの明示的な実装 System.Runtime.Serialization.ISerializable.GetObjectData このメンバの説明については、ISerializable.GetObjectData メソッドに関するトピックを参照してください。
参照参照

関連項目

SoundPlayer クラス
System.Media 名前空間
SystemSounds

その他の技術情報

SoundPlayer クラスの概要



英和和英テキスト翻訳

英語⇒日本語日本語⇒英語

辞書ショートカット

すべての辞書の索引

「SoundPlayer」の関連用語

SoundPlayerのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



SoundPlayerのページの著作権

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2026 Microsoft.All rights reserved.

©2026 GRAS Group, Inc.RSS