In the xNa 3.0 project, click audio and play and control the simple .wav audio file.

Source: Internet
Author: User
Tags ranges
Problem

You want to have a method for playing sound quickly.

Solution

In xna3.0, you can directly load and play sounds from xNa without loading them using xact. Although this method removes some advanced xact functions, it makes playing sound very easy.

Working Principle

First, add a sound file to the project. The method is the same as adding an image to the project. For more information, see tutorial 3-1. The simplest solution is to drag a .wav file to the content folder of the solution.

After adding the .wav file to the projectCodeDeclare a soundeffect variable on the top, which will be linked to the following audio file:

 
Soundeffect myeffect;

In loadcontent, upload a .wav file with the following code:

 
Myeffect = content. Load <soundeffect> ("effect ");

You can use the following code to play a sound:

 
Myeffect. Play ();
Soundeffect. Play () overload method

The soundeffect. Play () method has many overload methods that provide more control. The most complex overload method has four parameters:

    • The first volume parameter allows you to control the volume. The range is between 0.0f and 1.0f, And the 1.0f parameter is set to soundeffect. mastervolumn, which is also between 0.0f and 1.0f. 1.0f corresponds to the system volume (which can be seen in the speaker icon on the Windows system toolbar ).
    • The second parameter, pitch, is the pitch (speed). The pitch value ranges from-1.0 to 1.0. A negative value indicates that the playback speed is slow, the positive value is fast, and 0.0 indicates that the playback speed is normal. Note that this can be used to produce different versions of a sound: the shotgun and *** use the same sound effect, but the pitch is different.
    • The third parameter pan is the audio channel. The input value ranges from-1.0 to 1.0.-1.0 indicates that the video is played only in the left audio channel, and 1.0 indicates that the video is played only in the right audio channel, A positive value indicates that the volume of the right audio channel is greater than that of the left audio channel.
    • The last loop parameter allows you to set whether to loop. The input value is a Boolean value. 1 indicates true, that is, after the sound is played, it is automatically played again, and 0 indicates false, this means that the sound will not be played again after it is played. You can see the following chapter to learn how to prevent loop playback of a sound.
Control sound effects

The previous code is enough to play sound effects, but xNa also provides the soundeffectinstance class. You can use this class to pause or stop sound effects and change the volume. First, you must add a variable at the top of the Code:

 
Soundeffectinstance mydomaintinstance;

When the play method of soundeffectinstance is called, The soundeffect object is automatically converted to a soundeffectinstance object.

Now you can access the soundeffectinstance object. You can use this object to change the sound height and volume or check whether it has stopped playing:

If (mydomaintinstance! = NULL) {mydomaintinstance. Volume * = 0.9f; If (mydomaintinstance. State = soundstate. Stopped) {mydomaintinstance. Dispose (); mydomaintinstance = NULL ;}}

This code continuously reduces the volume and removes references to this object when the playback is stopped.

Note:Zune can play 16 soundeffectinstances at the same time, but there is no limit on the number of soundeffectinstances to load. Xbox360 can play 300 soundeffectinstances at the same time, but only 300 soundeffectinstances can be loaded at a time. There are no limits on PC. Note that when writing a book, There is a bug in the soundeffectinstance. Play method, which will be ignored when using the soundeffectinstance. Resume method.

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.