Dream come true XNA (6)-sound and sound effects

Source: Internet
Author: User

[Index page]
[Download source code]

Dream come true XNA (6)-sound and sound effects

Author: webabcd

Introduction
XNA: Sound and sound effects

  • XACT-Cross-platform Audio Creation Tool, a Cross-platform sound production Tool
  • SoundEffect-sound object
  • MediaPlayer-Media Player Object; Song-music object

Example
1. Use XACT to process and play sound effects (press the K key on the keyboard to load this Demo)
Component/Audio/XACT. cs

/** XACT (Cross-platform Audio Creation Tool)-A Cross-platform sound production Tool for collecting and editing Audio files. XNA provides APIs to play XACT projects. The Reach Standard does not support XACT * 1. XACT can only be processed. audio files in wav format * 2. Microsoft XNA Game Studio 4.0-> Tools-> Microsoft Cross-Platform Audio Creation Tool 3 (XACT3) the XACT editing tool * 3 is enabled. The XACT editing project type is. xap text file (which can be opened in Notepad. xap file and Its referenced. after the wav file is saved to the Content project, Content Pipeline processes it. * 4. Create a Wave Banks (waveform library) and. add the wav file to this library * 5. Create a Sound Banks (audio and audio database). Sound Banks is divided into two windows: Sound Name and Cue Name *) after dragging an Item to Cue Name in Wave Banks, an Item * B will be created in the Sound Name and Cue Name respectively. The number of Sound repetitions can be modified in the Sound Name, volume, high bass, etc. * c) XNA uses the Cue name to play sound effects. A key role of Cue is: A SoundBank object can play multiple Cue * 6 at the same time. If you want to play a sound in the XACT editor, you need to start Microsoft XNA Game Studio 4.0-> Tools-> XACT Auditioning Utility console */using System; using System. collections. generic; using System. linq; using Microsoft. xna. framework; using Microsoft. xna. framework. audio; using Microsoft. xna. framework. content; using Microsoft. xna. framework. gamerServices; using Microsoft. xna. framework. graphics; using Microsoft. xna. framework. input; using Microsoft. xna. framework. media; namespace XNA. component. audio {public class XACT: Microsoft. xna. framework. drawableGameComponent {// audio engine _ AudioEngine; public XACT (Game game): base (game) {} public override void Initialize () {base. initialize ();} protected override void LoadContent () {// Wave Banks (waveform library) object WaveBank waveBank; // Sound Banks (Audio Library) object SoundBank soundBank; /** after compilation, Content Pipeline will. xap files are divided into multiple files as follows: * 1. the xap file will generate a file with the same name. xgs file, which needs to be used when instantiating AudioEngine * 2 ,. each waveform library in xap generates a corresponding one. xwb file. The file name is the name of the corresponding waveform database. This file is required when instantiating the WaveBank. * 3 ,. each audio database in xap generates a corresponding one. xsb file. The file name is the corresponding audio and video library name. when instantiating SoundBank, you need to use this file */_ audioEngine = new AudioEngine (Game. content. rootDirectory + "/Audio/XACT. xgs "); waveBank = new WaveBank (_ audioEngine, Game. content. rootDirectory + "/Audio/Wave Bank. xwb "); soundBank = new SoundBank (_ audioEngine, Game. content. rootDirectory + "/Audio/Sound Bank. xsb ");/** SoundBank. playCue (string cueName)-specifies the cue for playing. A single SoundBank object supports playing multiple cue * SoundBank at the same time. getCue (string cueName)-Get the specified cue information and return Microsoft. xna. framework. audio. cue object */soundBank. playCue ("Thunder"); soundBank. playCue ("Rain");/** Cue-cue information * Cue. play ()-Play * Cue. pause ()-Pause * Cue. resume ()-continue * Cue. stop (AudioStopOptions options)-Stop. If you play a new video, you need to instantiate the Cue object * AudioStopOptions. asAuthored-Stop after completing the effect specified by XACT (for example, in this example, Rain is marked as Infinite and Play Release. The former indicates that Rain will Play cyclically, and the latter indicates that it is calling Stop (AudioStopOptions. after AsAuthored), wait until the Rain audio stops playing.) * AudioStopOptions. immediate-Stop now */Cue cueThunder = soundBank. getCue ("Thunder"); System. diagnostics. debug. write (cueThunder. name);} public override void Update (GameTime gameTime) {// AudioEngine. update ()-When each frame is used, the AudioEngine must make some adjustments to the sound under its jurisdiction. In order to make the sound play normally at any time, this method _ audioEngine must be called every frame. update (); base. update (gameTime);} public override void Draw (GameTime gameTime) {Game. graphicsDevice. clear (Color. cornflowerBlue); base. update (gameTime );}}}

2. Play the sound using SoundEffect (press the L key on the keyboard to load this Demo)
Component/Audio/SoundEffectDemo. cs

Using System; using System. collections. generic; using System. linq; using Microsoft. xna. framework; using Microsoft. xna. framework. audio; using Microsoft. xna. framework. content; using Microsoft. xna. framework. gamerServices; using Microsoft. xna. framework. graphics; using Microsoft. xna. framework. input; using Microsoft. xna. framework. media; using System. diagnostics; namespace XNA. component. audio {public class SoundEffectDemo: Microsoft. xna. framework. drawableGameComponent {public SoundEffectDemo (Game game): base (game) {}public override void Initialize () {base. initialize ();} protected override void LoadContent () {/** SoundEffect-sound object * SoundEffect. play (float volume, float pitch, float pan)-Play sound * volume-volume value, between 0 and 1 * pitch-tweeter value,-1 to 1, -1 is pure bass, 1 is pure tweeter * pan-left and right audio balanced value,-1 to 1,-1 is only left audio, 1 is only right audio * SoundEffect. duration-sound Duration * SoundEffect. name-Name of the sound object */SoundEffect soundEffect; soundEffect = Game. content. load <SoundEffect> ("Audio/Thunder"); soundEffect. play (0.5f,-1f, 0f); Debug. writeLine (soundEffect. duration. totalSeconds. toString (), "Duration");/** SoundEffectInstance-SoundEffect object instance * SoundEffect. createInstance ()-return SoundEffect object * SoundEffectInstance. volume-Volume value, between 0 and 1 * SoundEffectInstance. pitch-tweeter value,-1 to 1,-1 is pure bass, 1 is pure tweeter * SoundEffectInstance. pan-left-right audio balancing value,-1 to 1,-1 is only left sound, 1 is only right sound * SoundEffectInstance. isLooped-whether to play cyclically * SoundEffectInstance. play ()-Play * SoundEffectInstance. pause ()-Pause * SoundEffectInstance. resume ()-continue playing * SoundEffectInstance. stop ()-Stop * SoundEffectInstance. state-returns the current State of the sound object [Microsoft. xna. framework. audio. soundState enumeration] * SoundState. playing-Playing status * SoundState. paused-Pause status * SoundState. stopped-stop status * // * SoundEffectInstance soundEffectInstance = soundEffect. createInstance (); soundEffectInstance. volume = 0.5f; soundEffectInstance. pitch =-1f; soundEffectInstance. pan = 0f; soundEffectInstance. isLooped = true; soundEffectInstance. play (); soundEffectInstance. pause (); soundEffectInstance. resume (); soundEffectInstance. stop (); SoundState soundState = soundEffectInstance. state; */} public override void Update (GameTime gameTime) {base. update (gameTime);} public override void Draw (GameTime gameTime) {Game. graphicsDevice. clear (Color. cornflowerBlue); base. update (gameTime );}}}

3. Play music through MediaPlayer and Song (press the M key on the keyboard to load this Demo)
Component/Audio/MediaPlayerDemo. cs

Using System; using System. collections. generic; using System. linq; using Microsoft. xna. framework; using Microsoft. xna. framework. audio; using Microsoft. xna. framework. content; using Microsoft. xna. framework. gamerServices; using Microsoft. xna. framework. graphics; using Microsoft. xna. framework. input; using Microsoft. xna. framework. media; using System. diagnostics; namespace XNA. component. audio {public class MediaPlayerDemo: Microsoft. xna. framework. drawableGameComponent {public MediaPlayerDemo (Game game): base (game) {}public override void Initialize () {base. initialize ();} protected override void LoadContent () {/** Song-music object * Song. fromUri ()-load the music file from the specified network address * // ** MediaPlayer-Media Player Object * MediaPlayer. isRepeating-whether to play repeatedly * MediaPlayer. isMuted-whether to mute * MediaPlayer. playPosition-current playback position * MediaPlayer. volume-Volume * MediaPlayer. state-Playback status. Microsoft is returned. xna. framework. media. mediaState enumeration * MediaState. playing-play * MediaState. paused-Pause status * MediaState. stopped-stop status * MediaPlayer. mediaStateChanged-events triggered after the playback status changes * MediaPlayer. queue-obtains the playlist object, type: Microsoft. xna. framework. media. mediaQueue * MediaQueue. activeSong-The Song object currently playing * MediaQueue. activeSongIndex-index of the currently playing Song object in the playlist * MediaQueue. count-Total number of Song objects in the playlist * MediaPlayer. play ()-Play the specified Song object; or Play the specified Song object set (that is, the playlist), and you can specify the number of music in the playlist to start playing * MediaPlayer. pause ()-Pause * MediaPlayer. resume ()-continue playing * MediaPlayer. stop ()-Stop * // Song song = Song. fromUri ("Demo", new Uri ("http://www.abc.com/demo.mp3"); Song song = Game. content. load <Song> ("Audio/Demo"); MediaPlayer. isRepeating = true; MediaPlayer. isMuted = false; MediaPlayer. volume = 0.5f; MediaPlayer. play (song);} public override void Update (GameTime gameTime) {base. update (gameTime);} public override void Draw (GameTime gameTime) {Game. graphicsDevice. clear (Color. cornflowerBlue); base. update (gameTime );}}}

OK
[Download source code]

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.