header
CodePanic! > C#.NET Tips > 今ここ

■WAVEファイルを再生、停止する

SoundPlayerクラスを利用します。
SoundPlayerクラスはSystem.Media名前空間内にあるので

using System.Media;

の宣言を忘れずに。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Media; // これを追加しておくこと

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        SoundPlayer player = new SoundPlayer(@"c:\temp\test.wav");

        public Form1()
        {
            InitializeComponent();
        }

        private void buttonPlay_Click(object sender, EventArgs e)
        {
            // 再生
            player.Play();              // 別スレッドで再生
            //player.PlayLooping();     // 別スレッドでループ再生
            //player.PlaySync();        // 当該スレッドで再生(再生中は他の操作ができません)
        }

        private void buttonStop_Click(object sender, EventArgs e)
        {
            // 停止
            player.Stop();
        }
    }
}





Copyright © 2008.07 - shougo suzaki