Playing Sounds
summary
Imports and plays a sound file created in XACT.
Operating environment
Prerequisites
Supported XNA Versions |
|
Supported Platforms |
|
Windows Required Vertex Shader Version | 1.1 |
Windows Required Pixel Shader Version | 1.1 |
Operating environment
platform |
How to work with the sample
Works keyboardXbox | 360 controllermouse | ||
---|---|---|---|
Playing Sounds | A | A | - |
substance
Note: These tips are based on XNA Game Studio 2.0.
In order to play the sound, you need to register the file created in "Creating a file for playing sound with XNA" to the project.
Right-click the Content folder and select Add - Existing Item.
The . Select the "xap" file to load. ("Audio.xap" in the sample)
The xap file is added to the Content folder.
Next, move the wave file that you used to create the xap file directly to the Content folder. One thing to keep in mind is that the WAV file must be placed in the same relative path as when the XAP file was created.
In this case, I'm moving the wave file directly to the folder, but I don't have any problem adding it to my project.
That's it, you're ready to go. From here on, it will be a program.
field
<summary>
オーディオエンジン
</summary>
private AudioEngine audioEngine = null;
<summary>
WaveBank
</summary>
private WaveBank waveBank = null;
<summary>
SoundBank
</summary>
private SoundBank soundBank = null;
To play sounds, use three things: "AudioEngine", "WaveBank", and "SoundBank". You'll most likely use SoundBanks or AudioEngines, but you'll need to create all three instances.
Creating an Instance
<summary>
ゲームが始まる前の初期化処理を行うメソッド
グラフィック以外のデータの読み込み、コンポーネントの初期化を行う
</summary>
protected override void Initialize()
{
// オーディオデータの読み込み
this.audioEngine = new AudioEngine(@"Content\Audio.xgs");
this.waveBank = new WaveBank(this.audioEngine, @"Content\Wave Bank.xwb");
this.soundBank = new SoundBank(this.audioEngine, @"Content\Sound Bank.xsb");
// コンポーネントの初期化などを行います
base.Initialize();
}
Sound-related instances are created in the Game.Initialize method. When you create each class, you set the argument to . xgs」「. xwb」「. .xsb" files, which are automatically created based on the xap project file when you build the project.
The names of the audio, soundbank, and wavebank files must be the same as the names you set when you created the project in XACT. Also, since the path of the file is relative to the executable file, it is necessary to include the path of the "Content" folder in this example.
AudioEngine
** Constructor
Creates an instance of the AudioEngine class for handling audio objects.
settingsFile | string | Path of the configuration file to load |
WaveBank
constructor
Creates an instance of the WaveBank class that contains a collection of wave files.
audioEngine | AudioEngine | WaveBank and AudioEngine |
nonStreamingWaveBankFilename | string | Path to the WaveBank file to load |
SoundBank
constructor
Creates an instance of the SoundBank class that contains a collection of queues.
audioEngine | AudioEngine | AudioEngine to connect to SoundBanks |
filename | string | Path to the SoundBank file to load |
Playing Sounds
if (keyState.IsKeyDown(Keys.A) || padState.Buttons.A == ButtonState.Pressed)
{
if (this.pressed == false)
{
// サウンドを再生
this.soundBank.PlayCue("Sample");
this.pressed = true;
}
}
else
{
this.pressed = false;
}
To play a sound, use the SoundBank.PlayCue method. The sound source to be played is specified by the name of the "Cue" set in XACT.
Once played, the sound will continue to flow until the end. In addition, the same sound source can be played over and over again by playing it multiple times.
In the sample, each A button or A key is pressed to play once.
SoundBank.PlayCue
method
Play the cue.
name | string | The name of the queue to play |
All Codes
using System;
using System.Collections.Generic;
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.Net;
using Microsoft.Xna.Framework.Storage;
namespace PlaySound
{
<summary>
ゲームメインクラス
</summary>
public class GameMain : Microsoft.Xna.Framework.Game
{
<summary>
グラフィックデバイス管理クラス
</summary>
private GraphicsDeviceManager graphics = null;
<summary>
スプライトのバッチ化クラス
</summary>
private SpriteBatch spriteBatch = null;
<summary>
オーディオエンジン
</summary>
private AudioEngine audioEngine = null;
<summary>
WaveBank
</summary>
private WaveBank waveBank = null;
<summary>
SoundBank
</summary>
private SoundBank soundBank = null;
<summary>
キープレスフラグ
</summary>
private bool pressed = false;
<summary>
GameMain コンストラクタ
</summary>
public GameMain()
{
// グラフィックデバイス管理クラスの作成
this.graphics = new GraphicsDeviceManager(this);
// ゲームコンテンツのルートディレクトリを設定
this.Content.RootDirectory = "Content";
}
<summary>
ゲームが始まる前の初期化処理を行うメソッド
グラフィック以外のデータの読み込み、コンポーネントの初期化を行う
</summary>
protected override void Initialize()
{
// オーディオデータの読み込み
this.audioEngine = new AudioEngine(@"Content\Audio.xgs");
this.waveBank = new WaveBank(this.audioEngine, @"Content\Wave Bank.xwb");
this.soundBank = new SoundBank(this.audioEngine, @"Content\Sound Bank.xsb");
// コンポーネントの初期化などを行います
base.Initialize();
}
<summary>
ゲームが始まるときに一回だけ呼ばれ
すべてのゲームコンテンツを読み込みます
</summary>
protected override void LoadContent()
{
// テクスチャーを描画するためのスプライトバッチクラスを作成します
this.spriteBatch = new SpriteBatch(this.GraphicsDevice);
// TODO: this.Content を使用してゲームコンテンツを読み込む
// ロジックを書いてください
}
<summary>
ゲームが終了するときに一回だけ呼ばれ
すべてのゲームコンテンツをアンロードします
</summary>
protected override void UnloadContent()
{
// TODO: ContentManager で管理されていないコンテンツを
// ここでアンロードしてください
}
<summary>
描画以外のデータ更新等の処理を行うメソッド
主に入力処理、衝突判定などの物理計算、オーディオの再生など
</summary>
<param name="gameTime">このメソッドが呼ばれたときのゲーム時間</param>
protected override void Update(GameTime gameTime)
{
// キーボードの情報取得
KeyboardState keyState = Keyboard.GetState();
// ゲームパッドの情報取得
GamePadState padState = GamePad.GetState(PlayerIndex.One);
// Xbox360 コントローラの BACK ボタンを押したときにゲームを終了させます
if (padState.Buttons.Back == ButtonState.Pressed)
{
this.Exit();
}
if (keyState.IsKeyDown(Keys.A) || padState.Buttons.A == ButtonState.Pressed)
{
if (this.pressed == false)
{
// サウンドを再生
this.soundBank.PlayCue("Sample");
this.pressed = true;
}
}
else
{
this.pressed = false;
}
// 登録された GameComponent を更新する
base.Update(gameTime);
}
<summary>
描画処理を行うメソッド
</summary>
<param name="gameTime">このメソッドが呼ばれたときのゲーム時間</param>
protected override void Draw(GameTime gameTime)
{
// 画面を指定した色でクリアします
this.GraphicsDevice.Clear(Color.CornflowerBlue);
// TODO: ここに描画処理を記述します
// 登録された DrawableGameComponent を描画する
base.Draw(gameTime);
}
}
}