Search…

Thao Tác Âm Thanh trong C# Qua Thư Viện WMPLib

06/09/20202 min read
Hướng dẫn sử dụng WMPLib phát âm thanh trong C#.

WMPLib là thư viện âm thanh của Windows Media Player, thư viện này có sẵn trong hệ điều hành Windows, hỗ trợ xử lý nhiều loại file âm thanh như WAV, MP3, ... và có nhiều phương thức thao tác với âm thanh như Play, Pause, Stop, Volume, Playlist.

Thêm thư viện WMPLib vào Project

Để thêm thư viện vào project C#,  nhấp chuột phải vào Project và chọn Add → Reference...

Thao tác âm thanh trong csharp

Tiếp theo, một hộp thoại xuất hiện:

  • Chọn xuống tab Browse và nhấn vào nút Browse… (1)
  • Chọn đến file wmp.dll nằm trong đường dẫn C:\Windows\System32 (2)
  • Sau đó đánh dấu tích vào thư viện wmp.dll và chọn nút OK (3)
Thao tác âm thanh trong csharp 2

Thao tác âm thanh qua thư viện WMPLib

Đầu tiên, thêm using WMPLib trong code.

using WMPLib;

Khởi tạo đối tượng WindowsMediaPlayer và gán giá trị đường dẫn đến file âm thanh.

WindowsMediaPlayer sound = new WindowsMediaPlayer();
sound.URL = "STDIO_Sound.mp3";

Sau đó, sử dụng các phương thức có sẵn dưới đây để thao tác với âm thanh.

sound.controls.play();  // Play sound
sound.controls.pause(); // Pause sound
sound.controls.stop();  // Stop sound

Ngoài 3 phương thức cơ bản này, còn nhiều phương thức khác các nữa.

Mã nguồn

File SoundManager.cs

using WMPLib;

namespace SoundAplication
{
    public class SoundManager
    {
        WindowsMediaPlayer sound;

        public SoundManager(string _filePath)
        {
            sound = new WindowsMediaPlayer();
            sound.URL = _filePath;
        }

        public void Play()
        {
            sound.controls.play();
        }

        public void Stop()
        {
            sound.controls.stop();
        }

        public void Pause()
        {
            sound.controls.pause();
        }

        public void Resume()
        {
            if (sound.status == "Paused")
                sound.controls.play();
        }
    }
}

File Program.cs

using System;

namespace SoundAplication
{
    class Program
    {
        static void Main(string[] args)
        {
            SoundManager sound = new SoundManager("STDIO_Sound.wav");

            string cmd;
            cmd = Console.ReadLine();

            while (true)
            {
                if (cmd == "/play")
                {
                    sound.Play();
                    cmd = Console.ReadLine();
                }
                else if (cmd == "/pause")
                {
                    sound.Pause();
                    cmd = Console.ReadLine();
                }
                else if (cmd == "/resume")
                {
                    sound.Resume();
                    cmd = Console.ReadLine();
                }
                else if (cmd == "/stop")
                {
                    sound.Stop();
                    cmd = Console.ReadLine();
                }
                else
                    cmd = Console.ReadLine();
            }
        }
    }
}
IO Stream

IO Stream Co., Ltd

30 Trinh Dinh Thao, Hoa Thanh ward, Tan Phu district, Ho Chi Minh city, Vietnam
+84 28 22 00 11 12
developer@iostream.co

383/1 Quang Trung, ward 10, Go Vap district, Ho Chi Minh city
Business license number: 0311563559 issued by the Department of Planning and Investment of Ho Chi Minh City on February 23, 2012

©IO Stream, 2013 - 2024