123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- using System;
- using System.Collections.Generic;
- #if ANDROID
- using Android.App;
- #endif
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Content;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- using Microsoft.Xna.Framework.Storage;
- using Microsoft.Xna.Framework.Audio;
- namespace Microsoft.Xna.Samples.Sound
- {
- /// <summary>
- /// This is the main type for your game
- /// </summary>
- public class Game1 : Microsoft.Xna.Framework.Game
- {
- GraphicsDeviceManager graphics;
- KeyboardState oldSate;
- SoundEffect sound;
- SpriteBatch spriteBatch;
- SoundEffectInstance soundInst;
- SpriteFont font;
-
- public Game1 ()
- {
- graphics = new GraphicsDeviceManager (this);
-
- Content.RootDirectory = "Content";
-
- graphics.PreferMultiSampling = true;
- graphics.IsFullScreen = false;
- Window.AllowUserResizing = false;
- graphics.SupportedOrientations = DisplayOrientation.Portrait | DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight | DisplayOrientation.PortraitUpsideDown;
- }
-
- /// <summary>
- /// 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.
- /// </summary>
- protected override void Initialize ()
- {
- // TODO: Add your initialization logic here
-
- base.Initialize ();
- }
- /// <summary>
- /// LoadContent will be called once per game and is the place to load
- /// all of your content.
- /// </summary>
- protected override void LoadContent ()
- {
- // Create a new SpriteBatch, which can be used to draw textures.
- spriteBatch = new SpriteBatch(GraphicsDevice);
- sound = Content.Load<SoundEffect>("Explosion1");
- soundInst = sound.CreateInstance();
-
- font = Content.Load<SpriteFont>("spriteFont1");
- }
- /// <summary>
- /// Allows the game to run logic such as updating the world,
- /// checking for collisions, gathering input, and playing audio.
- /// </summary>
- /// <param name="gameTime">Provides a snapshot of timing values.</param>
- protected override void Update (GameTime gameTime)
- {
- KeyboardState ks = Keyboard.GetState();
-
- #if ANDROID
- if (soundInst.State != SoundState.Playing)
- {
- soundInst.Volume = 1f;
- soundInst.IsLooped = true;
- soundInst.Play();
- }
- #endif
-
- if (ks[Keys.Escape] == KeyState.Down)
- base.Exit();
-
- if (ks.IsKeyDown(Keys.A) && oldSate.IsKeyUp(Keys.A))
- soundInst.Play();
-
- if (ks.IsKeyDown(Keys.B) && oldSate.IsKeyUp(Keys.B))
- soundInst.Stop();
-
- if (ks.IsKeyDown(Keys.C) && oldSate.IsKeyUp(Keys.C))
- soundInst.Pause();
-
- if (ks.IsKeyDown(Keys.D) && oldSate.IsKeyUp(Keys.D))
- soundInst.IsLooped = !soundInst.IsLooped;
-
- if (ks.IsKeyDown(Keys.E) && oldSate.IsKeyUp(Keys.E))
- soundInst.Stop(true);
-
- if (ks.IsKeyDown(Keys.X))
- soundInst.Volume = MathHelper.Clamp(soundInst.Volume + 0.01f, 0f, 1f);
- else if (ks.IsKeyDown(Keys.Z))
- soundInst.Volume = MathHelper.Clamp(soundInst.Volume - 0.01f, 0f, 1f);;
-
- oldSate = ks;
- base.Update (gameTime);
- }
- /// <summary>
- /// This is called when the game should draw itself.
- /// </summary>
- /// <param name="gameTime">Provides a snapshot of timing values.</param>
- protected override void Draw (GameTime gameTime)
- {
- graphics.GraphicsDevice.Clear (Color.CornflowerBlue);
- spriteBatch.Begin();
- spriteBatch.DrawString(font, "A: play\nB: stop\nC: pause\nD: toggle looping\nE: immediate stop\nX/Z volume\nStatus: " +
- soundInst.State.ToString() + "\nLooping: " +
- soundInst.IsLooped.ToString() + "\nVolume: " +
- soundInst.Volume.ToString()
- , Vector2.Zero, Color.White);
-
- base.Draw(gameTime);
-
- spriteBatch.End();
- }
- }
- }
|