■XNAでAudio再生 Prev  Top  Next

今度は本当にAudioの再生します。


namespace Tutrial
{
   /// 
   /// This is the main type for your game
   /// 
   public class Game1 : Microsoft.Xna.Framework.Game
   {
      private GraphicsDeviceManager graphics = null;
      private ContentManager content = null;

      //****************************************************************
      //オブジェクトの定義
      //****************************************************************

      //サウンド
      private AudioEngine audioEngine; //オーディオエンジン
      private WaveBank waveBank;       //Waveバンク
      private SoundBank soundBank;     //Soundバンク

      public Game1()
      {
         this.graphics = new GraphicsDeviceManager(this);

         //リソースフォルダのルートパスを指定
         this.content = new ContentManager(this.Services);
      }


      /// 
      /// Allows the game to perform any initialization it needs to before starting to run.
      /// This is where it can query for any required services and load any non-graphic
      /// related content.  Calling base.Initialize will enumerate through any components
      /// and initialize them as well.
      /// 
      protected override void Initialize()
      {
         //****************************************************************
         //オブジェクトの初期化
         //****************************************************************

         //サウンドの読み込み
         audioEngine = new AudioEngine("Sample.xgs");
         waveBank = new WaveBank(audioEngine, "Wave Bank.xwb");
         soundBank = new SoundBank(audioEngine, "Sound Bank.xsb"); 

         base.Initialize();
      }


      /// 
      /// Load your graphics content.  If loadAllContent is true, you should
      /// load content from both ResourceManagementMode pools.  Otherwise, just
      /// load ResourceManagementMode.Manual content.
      /// 
      /// Which type of content to load.
      protected override void LoadGraphicsContent(bool loadAllContent)
      {
         if (loadAllContent)
         {
            // TODO: Load any ResourceManagementMode.Automatic content
         }

         // TODO: Load any ResourceManagementMode.Manual content
      }


      /// 
      /// Unload your graphics content.  If unloadAllContent is true, you should
      /// unload content from both ResourceManagementMode pools.  Otherwise, just
      /// unload ResourceManagementMode.Manual content.  Manual content will get
      /// Disposed by the GraphicsDevice during a Reset.
      /// 
      /// Which type of content to unload.
      protected override void UnloadGraphicsContent(bool unloadAllContent)
      {
         if (unloadAllContent == true)
         {
            content.Unload();
         }
      }


      /// 
      /// Allows the game to run logic such as updating the world,
      /// checking for collisions, gathering input and playing audio.
      /// 
      /// Provides a snapshot of timing values.
      protected override void Update(GameTime gameTime)
      {
         // Allows the default game to exit on Xbox 360 and Windows
         if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();

         //****************************************************************
         //サウンドの再生
         //****************************************************************
         
         soundBank.GetCue("00").Play();

         base.Update(gameTime);
      }


      /// 
      /// This is called when the game should draw itself.
      /// 
      /// Provides a snapshot of timing values.
      protected override void Draw(GameTime gameTime)
      {
         //背景色をグレーで塗りつぶす
         this.graphics.GraphicsDevice.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, new Vector4(0.2f, 0.2f, 0.2f, 1.0f), 1.0f, 0 );

         base.Draw(gameTime);
      }
   }
}

サウンドが再生されました。おしまい。

次回、ゲームの配布。

Prev  Top  Next
inserted by FC2 system