DirectX programming: [elementary] C # Use DirectSound to play WAV-format sound. [at least 4 Sentences]

Source: Internet
Author: User

Many friends on the Internet have introduced how to use DirectSound to play sound in C. I tried it myself today and found that it is really easy for beginners. You only need a few short lines of code. The key is only four sentences or so, and the code is OK.

As follows:

Platform: VS. NET 2005, DirectX SDK (June 2008)

External DLL to be referenced: Microsoft. DirectX. dll and Microsoft. DirectX. DirectSound. dll.

Namespace to be referenced: using Microsoft. DirectX. DirectSound.

The general steps to achieve the playback effect are as follows: 1. Create the playback device object; 2. Create a buffer object; 3. Set the buffer collaboration level; 4. Set the playback buffer.

Because it is relatively simple, let's look at the Code directly. Among them, the "Play" button mainly has four sentences to achieve the playing effect, but its buffer information is the default. The "GlobalPlay" button adjusts the buffer zone by setting the buffer information so that playback can continue when the focus is lost. In addition to the playback function, you can also control the volume and channels.

PlayWav
Using System;
Using System. Collections. Generic;
Using System. ComponentModel;
Using System. Data;
Using System. Drawing;
Using System. Text;
Using System. Windows. Forms;

// Referenced namespace
Using Microsoft. DirectX. DirectSound;

Namespace MyVoice
{
Public partial class Form4: Form
{
Public Form4 ()
{
InitializeComponent ();
}

Private SecondaryBuffer secBuffer; // buffer object
Private Device secDev; // Device object

Private void button#click (object sender, EventArgs e)
{
OpenFileDialog1.Filter = "(*. *) | *. * | (.wav) | *. wav ";
DialogResult dlgResult = openFileDialog1.ShowDialog ();
If (dlgResult = DialogResult. OK)
{
TextBox1.Text = openFileDialog1.FileName;
}

}

Private void button2_Click (object sender, EventArgs e)
{
If (textBox1.Text. Length> 0)
{
SecDev = new Device ();
SecDev. SetCooperativeLevel (this, CooperativeLevel. Normal); // sets the device collaboration level.
SecBuffer = new SecondaryBuffer (textBox1.Text, secDev); // create a secondary buffer
SecBuffer. Play (0, BufferPlayFlags. Looping); // set the buffer to loop playback.
}
}

Private void button3_Click (object sender, EventArgs e)
{
If (textBox1.Text. Length> 0)
{
SecDev = new Device ();
SecDev. SetCooperativeLevel (this, CooperativeLevel. Normal); // sets the device collaboration level.
BufferDescription buffDes = new BufferDescription ();
BuffDes. GlobalFocus = true; // sets the global focus of the buffer.
BuffDes. ControlVolume = true; // specifies that the buffer can control the sound.
BuffDes. ControlPan = true; // specifies that the buffer can control sound channel balancing.
SecBuffer = new SecondaryBuffer (textBox1.Text, buffDes, secDev); // create a secondary buffer
SecBuffer. Play (0, BufferPlayFlags. Looping); // set the buffer to loop playback.
}
}

Private void button4_Click (object sender, EventArgs e)
{
If (secBuffer! = Null)
{
SecBuffer. Stop ();
}
}

Private void trackBar1_Scroll (object sender, EventArgs e)
{
If (secBuffer! = Null)
{
SecBuffer. Volume =-trackBar1.Value * 400; // if the Volume is 0, it indicates the maximum Volume. Therefore, the value must be negative.
}
}

Private void trackBar2_Scroll (object sender, EventArgs e)
{
If (secBuffer! = Null)
{
If (trackBar2.Value = 0)
{
SecBuffer. Pan = Convert. ToInt32 (Pan. Left); // Left audio channel
}
Else if (trackBar2.Value = 2)
{
SecBuffer. Pan = Convert. ToInt32 (Pan. Right); // Right audio channel
}
Else
{
SecBuffer. Pan = Convert. ToInt32 (Pan. Center );
}
}
}

}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.