In the xNa 3.0 project Audio & amp; #45, use the xact1_simple .wav file.

Source: Internet
Author: User
Problem

You want to play a sound in the game.

Note:Zune does not support xact, so refer to the 7-1 and 7-2 tutorials to learn how to play a sound on Zune.

Solution

By using xact, a free tool provided by xNa game studio 3.0, you can create xact projects that contain all the sounds used in the game. You can import this xact project to the xNa project, so that you can use a simple lineCodePlay back the sound stored in the xact project.

Working Principle

First, make sure that you have opened an xNa project and stored it somewhere on the disk. To better organize materials, you should create a new folder to store all the sound materials. In the solution explorer of the xNa project, find the content, right-click it, and select Add → new folder to start a name.

Click Start> Programs> Microsoft xNa game studio 30> Tools> Microsoft cross-platform Audio creation tool (xact) to open xact.

When you start xact, the first thing you need to do is to select File> new project to start a new project and name your project, and save it to the content \ audio directory of the xNa project.

Now we have a new empty project. In the tree directory on the left, find wave banks, which contains waves, which is the audio file. Right-click wave banks and select new wave bank. Then, right-click sound banks and select new sound bank to create a new sound bank. Sound bank contains sound, which are the objects actually played in the xNa project. A sound points to a wave, and some effects can be added to the wave.

Now there should be two panels on the right of the window. Click the wave Bank Panel and you will see its properties in the lower left corner of the xact window. Find the name attribute and change it from wave bank to mywavebank. Then, click the sound bank panel and change the name attribute to mysoundbank. These names can be accessed from the xNa project.

You can use wave bank to store all. wav files, so right-click a part of the panel and Choose Insert wave file (s ). Browse to the. Wav file you want to play in the game on the disk and click open. You can see that it is added to the wave bank and displayed in red, because you have not created a sound in the sound bank to use this wave.

TIPS:The file is stored relative to the xact project. If you change the location of the audio file or the location of the xact project, you may have trouble copying the audio files to the content \ audio directory before importing them to the wave bank.

A wave is a reference to a sound file on a disk. A sound uses a wave to allow you to add effects, such as changing the volume and sound height. You can generate multiple sound from a wave. However,ProgramThe user uses a cue to access the sound. Therefore, you need to create a cue for each sound before it can be accessed through xNa code. A single cue can contain multiple sounds, so one line of code can play multiple sounds.

Therefore, before you can play a wave from the wave bank, you need to create a sound and use the cue of this wave, which can be done by selecting the wave and dragging it to the cue area of the sound bank panel, 7-1.

Beware:Make sure to drag the sound to the cue list at the bottom of the sound bank panel, instead of the top sound list.

Wave bank and sound bank are separated, so that you can create multiple sounds from a wave file. In this way, you can add different sound effects to different sounds on the same wave.

Figure 7-1 drag your wave to the cue Area

This is the usage of the xact tool. Do not forget to save the project by selecting File> Save projec and import the file to the xNa project. The method is the same as importing an image or object file. You can drag the. xap file to the content \ audio folder, or right-click the audio folder in Solution Explorer and select Add → existing item.

The myxactproject. xap file is immediately displayed in the Solution Explorer of the xNa game studio 3.0 program.

When a project is compiled, the xact project is read and converted to a binary file, which is written to the output folder of the project. For each. xap xact project file. An xNa content pipeline will create one. the xgs binary file (the same as the xact project name) creates one for each wave bank. create an xwb binary file for each sound bank. xsb binary file.

In the xNa project, you need to connect these three files to the corresponding variables. First, declare them at the top of the Code:

 
Audioengine; wavebank; soundbank;

Connect them to binary files (they are created/updated when you compile the project), for example, in the initialize method:

 
Audioengine = new audioengine ("content/Audio/myxactproject. xgs "); wavebank = new wavebank (audioengine," content/Audio/mywavebank. xwb "); soundbank = new soundbank (audioengine," content/Audio/mysoundbank. xsb ");

The soundbank variable contains all the cue that can be played. However, when you instantiate a new soundbank, you need to pass it to an audioengine. Therefore, you need to initialize the audioengine first. As long as you play a cue from soundbank, it will find the wave from the wavebank corresponding to audioengine, so you need the second line of code.

Note:The names of mywavebank and mysoundbank are named in the xact tool.

Now, when you want to play a simple sound, all you need to do is call the playcue method of the soundbank variable and pass the name of the cue to be played to this method!

 
Soundbank. playcue ("audio1 ");

When the playback ends, make sure that all cue is removed from the memory. Therefore, you must call the update method of audioengine at each frame:

 
Audioengine. Update ();

Beware:After you change the xact project and store it to a file, you cannot simply run the. exe of the xNa project and expect it to apply the change to the xact project. First, you need to re-compile (Press F5) The xNa project so that the content pipeline creates a new binary file from the xact project.

Code

You need to use global variables to play the voice that is included in the xact project. Cue:

 
Audioengine; wavebank; soundbank;

Initialize at the beginning of the project:

Protected override void initialize ()... {audioengine = new audioengine ("myxactproject. xgs "); wavebank = new wavebank (audioengine," mywavebank. xwb "); soundbank = new soundbank (audioengine," mysoundbank. xsb "); base. initialize ();}

Use the soundbank. playcue method to play a sound. For example, in the following code, press the next key to play a sound. Make sure that the audioengine. Update method is called for each frame:

 
Protected override void Update (gametime )... {If (gamepad. getstate (playerindex. one ). buttons. back = buttonstate. pressed) This. exit (); keyboardstate keybstate = keyboard. getstate (); If (keybstate. iskeydown (keys. space) soundbank. playcue ("audio1"); audioengine. update (); base. update (gametime );}

Now when you run the code, the sound is played when you press the Space key.

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.