XNA First Sound Project

esse quam videri
Revision as of 18:28, 25 January 2016 by Jeff (talk | contribs) (Text replacement - "</csharp>" to "</syntaxhighlight>")
Jump to: navigation, search


Setup Project in GSE

Open Visual Studio C# Game Studio Express

Select New Project

GameStudioExpressNewProject.png

Select Windows Game and Create a new folder on a local dirve

GameStudioExpressNewProjectSettings.png

Game Studio Express creates the default game files

GameStudioExpressGame1Files.png

It should also open the Main Game file game1.cs

  1 #region Using Statements
  2 using System;
  3 using System.Collections.Generic;
  4 using Microsoft.Xna.Framework;
  5 using Microsoft.Xna.Framework.Audio;
  6 using Microsoft.Xna.Framework.Content;
  7 using Microsoft.Xna.Framework.Graphics;
  8 using Microsoft.Xna.Framework.Input;
  9 using Microsoft.Xna.Framework.Storage;
 10 #endregion
 11 
 12 namespace TestGame1
 13 {
 14     /// <summary>
 15     /// This is the main type for your game
 16     /// </summary>
 17     public class Game1 : Microsoft.Xna.Framework.Game
 18     {
 19         GraphicsDeviceManager graphics;
 20         ContentManager content;
 21 
 22 
 23         public Game1()
 24         {
 25             graphics = new GraphicsDeviceManager(this);
 26             content = new ContentManager(Services);
 27         }
 28 
 29 
 30         /// <summary>
 31         /// Allows the game to perform any initialization it needs to before starting to run.
 32         /// This is where it can query for any required services and load any non-graphic
 33         /// related content.  Calling base.Initialize will enumerate through any components
 34         /// and initialize them as well.
 35         /// </summary>
 36         protected override void Initialize()
 37         {
 38             // TODO: Add your initialization logic here
 39 
 40             base.Initialize();
 41         }
 42 
 43 
 44         /// <summary>
 45         /// Load your graphics content.  If loadAllContent is true, you should
 46         /// load content from both ResourceManagementMode pools.  Otherwise, just
 47         /// load ResourceManagementMode.Manual content.
 48         /// </summary>
 49         /// <param name="loadAllContent">Which type of content to load.</param>
 50         protected override void LoadGraphicsContent(bool loadAllContent)
 51         {
 52             if (loadAllContent)
 53             {
 54                 // TODO: Load any ResourceManagementMode.Automatic content
 55             }
 56 
 57             // TODO: Load any ResourceManagementMode.Manual content
 58         }
 59 
 60 
 61         /// <summary>
 62         /// Unload your graphics content.  If unloadAllContent is true, you should
 63         /// unload content from both ResourceManagementMode pools.  Otherwise, just
 64         /// unload ResourceManagementMode.Manual content.  Manual content will get
 65         /// Disposed by the GraphicsDevice during a Reset.
 66         /// </summary>
 67         /// <param name="unloadAllContent">Which type of content to unload.</param>
 68         protected override void UnloadGraphicsContent(bool unloadAllContent)
 69         {
 70             if (unloadAllContent == true)
 71             {
 72                 content.Unload();
 73             }
 74         }
 75 
 76 
 77         /// <summary>
 78         /// Allows the game to run logic such as updating the world,
 79         /// checking for collisions, gathering input and playing audio.
 80         /// </summary>
 81         /// <param name="gameTime">Provides a snapshot of timing values.</param>
 82         protected override void Update(GameTime gameTime)
 83         {
 84             // Allows the default game to exit on Xbox 360 and Windows
 85             if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
 86                 this.Exit();
 87 
 88             // TODO: Add your update logic here
 89 
 90             base.Update(gameTime);
 91         }
 92 
 93 
 94         /// <summary>
 95         /// This is called when the game should draw itself.
 96         /// </summary>
 97         /// <param name="gameTime">Provides a snapshot of timing values.</param>
 98         protected override void Draw(GameTime gameTime)
 99         {
100             graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
101 
102             // TODO: Add your drawing code here
103 
104             base.Draw(gameTime);
105         }
106     }
107 }

All we need to do is to create a folder for the audio. Right click on the project and select 'New Folder' name the folder 'Audio'.

GameStudioExpressNewFolder.png

Now copy at least one 44,100 Hz wav file to the folder to use in the test project. You can do this in windows.

Now we can use XACT http://msdn2.microsoft.com/en-us/library/bb174772.aspx

XACT Time

Open XACT

XACTOpen.png

Add a new WaveBank. Wave Banks hold a collection of sound files.

XACTNewWaveBank.png

Now create a new Sound Bank. A Sound Bank is a collection of Events, Tracks and Cues.

XACTNewSoundBank.png

I like to tile the XACT windows horizontally

XACTTile.png

Now we can import sound files.

XACTReadyForImport.png

Select Wave Bank and Import Files or right click on the Wave Bank Window and select Import Wave.

XACTNewSound.png

Now we have a sound in the Wave Bank we need to add it to the Sound Bank. Drah the sound from the Wave Bank window to the Sound Bank window.

XACTDragFromWaveBank.png

Now that the sound is in the Sound Bank we need to add it as a Cue. Drag the sound from the Sound Bank to the Cue section.

XACTDragFromSoundBank.png

Now you should have the sound in all three sections.

XACTCueCreated.png

Now we have a sound in the Wave Bank and Sound Bank and a Named Cue we are ready to save our XACT project. Save the project as 'sound1.xap' in the 'Audio' folder that you created in the Game Studio Express Project.

XACTSaveAs.png

The last step is to build the XACT project to the GSE and integrate it into the game pipeline. Select build from the File menu in XACT.

XACTABuild.png

Import into GSE

GameStudioExpressAddExistingItem.png

GameStudioExpressAddExistingItemXAPFile.png

GameStudioExpressCopyIfNewer.png

Time to Code

Now we need to add the code to the project to play the sound


 1 public class Game1 : Microsoft.Xna.Framework.Game
 2     {
 3         GraphicsDeviceManager graphics;
 4         ContentManager content;
 5 
 6 
 7         public Game1()
 8         {
 9             graphics = new GraphicsDeviceManager(this);
10             content = new ContentManager(Services);
11         }

add

 //Declare Audio Objects
       public AudioEngine engine;
       public WaveBank wavebank;
       public SoundBank soundbank;
 1 public class Game1 : Microsoft.Xna.Framework.Game
 2     {
 3         GraphicsDeviceManager graphics;
 4         ContentManager content;
 5 
 6         //Declare Audio Objects
 7         public AudioEngine engine;
 8         public WaveBank wavebank;
 9         public SoundBank soundbank;
10 
11 
12         public Game1()
13         {
14             graphics = new GraphicsDeviceManager(this);
15             content = new ContentManager(Services);

now add to the constructor

//initalize Audio Objects
           engine = new AudioEngine(@"Audio\sound1.xgs");
           wavebank = new WaveBank(engine, @"Audio\Wave Bank.xwb");
           soundbank = new SoundBank(engine, @"Audio\Sound Bank.xsb");


 1 public Game1()
 2         {
 3             graphics = new GraphicsDeviceManager(this);
 4             content = new ContentManager(Services);
 5 
 6             //initalize Audio Objects
 7             engine = new AudioEngine(@"Audio\sound1.xgs");
 8             wavebank = new WaveBank(engine, @"Audio\Wave Bank.xwb");
 9             soundbank = new SoundBank(engine, @"Audio\Sound Bank.xsb");
10         }

and finally add to the update method

//Play Sound
soundbank.GetCue("FlameWhooshBy").Play();
 1 protected override void Update(GameTime gameTime)
 2         {
 3             // Allows the default game to exit on Xbox 360 and Windows
 4             if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
 5                 this.Exit();
 6 
 7             // TODO: Add your update logic here
 8 
 9             base.Update(gameTime);
10 
11             //Play Sound
12             soundbank.GetCue("FlameWhooshBy").Play();
13         }

Now you can test you game by running the project (press F5) or

GameStudioExpressDebug.png

XACTInstances.png