SoundSample.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Audio;
  3. using Microsoft.Xna.Framework.Content;
  4. using Microsoft.Xna.Framework.Graphics;
  5. using Microsoft.Xna.Framework.Input;
  6. using Microsoft.Xna.Framework.Media;
  7. using System;
  8. using System.Collections.Generic;
  9. namespace SoundSample
  10. {
  11. /// <summary>
  12. /// This is the main type for your game
  13. /// </summary>
  14. public class SoundGame : Game
  15. {
  16. GraphicsDeviceManager graphics;
  17. KeyboardState oldSate;
  18. SoundEffect sound;
  19. SpriteBatch spriteBatch;
  20. SoundEffectInstance soundInstance;
  21. SpriteFont font;
  22. public SoundGame ()
  23. {
  24. // Initialize fields to null to satisfy nullable checks
  25. sound = null!;
  26. spriteBatch = null!;
  27. soundInstance = null!;
  28. font = null!;
  29. graphics = new GraphicsDeviceManager (this);
  30. Content.RootDirectory = "Content";
  31. graphics.PreferMultiSampling = true;
  32. graphics.IsFullScreen = false;
  33. Window.AllowUserResizing = false;
  34. graphics.SupportedOrientations = DisplayOrientation.Portrait | DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight; // PortraitUpsideDown not supported on all platforms
  35. }
  36. /// <summary>
  37. /// Allows the game to perform any initialization it needs to before starting to run.
  38. /// This is where it can query for any required services and load any non-graphic
  39. /// related content. Calling base.Initialize will enumerate through any components
  40. /// and initialize them as well.
  41. /// </summary>
  42. protected override void Initialize ()
  43. {
  44. // TODO: Add your initialization logic here
  45. base.Initialize ();
  46. }
  47. /// <summary>
  48. /// LoadContent will be called once per game and is the place to load
  49. /// all of your content.
  50. /// </summary>
  51. protected override void LoadContent ()
  52. {
  53. // Create a new SpriteBatch, which can be used to draw textures.
  54. spriteBatch = new SpriteBatch(GraphicsDevice);
  55. font = Content.Load<SpriteFont>("Font");
  56. sound = Content.Load<SoundEffect>("Explosion1");
  57. soundInstance = sound.CreateInstance();
  58. }
  59. /// <summary>
  60. /// Allows the game to run logic such as updating the world,
  61. /// checking for collisions, gathering input, and playing audio.
  62. /// </summary>
  63. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  64. protected override void Update (GameTime gameTime)
  65. {
  66. KeyboardState ks = Keyboard.GetState();
  67. #if ANDROID
  68. if (soundInstance.State != SoundState.Playing)
  69. {
  70. soundInstance.Volume = 1f;
  71. soundInstance.IsLooped = true;
  72. soundInstance.Play();
  73. }
  74. #endif
  75. if (ks[Keys.Escape] == KeyState.Down)
  76. #if !ANDROID && !IOS
  77. base.Exit();
  78. #endif
  79. if (ks.IsKeyDown(Keys.A) && oldSate.IsKeyUp(Keys.A))
  80. soundInstance.Play();
  81. if (ks.IsKeyDown(Keys.B) && oldSate.IsKeyUp(Keys.B))
  82. soundInstance.Stop();
  83. if (ks.IsKeyDown(Keys.C) && oldSate.IsKeyUp(Keys.C))
  84. soundInstance.Pause();
  85. if (ks.IsKeyDown(Keys.D) && oldSate.IsKeyUp(Keys.D))
  86. soundInstance.IsLooped = !soundInstance.IsLooped;
  87. if (ks.IsKeyDown(Keys.E) && oldSate.IsKeyUp(Keys.E))
  88. soundInstance.Stop(true);
  89. if (ks.IsKeyDown(Keys.X))
  90. soundInstance.Volume = MathHelper.Clamp(soundInstance.Volume + 0.01f, 0f, 1f);
  91. else if (ks.IsKeyDown(Keys.Z))
  92. soundInstance.Volume = MathHelper.Clamp(soundInstance.Volume - 0.01f, 0f, 1f);;
  93. oldSate = ks;
  94. base.Update (gameTime);
  95. }
  96. /// <summary>
  97. /// This is called when the game should draw itself.
  98. /// </summary>
  99. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  100. protected override void Draw (GameTime gameTime)
  101. {
  102. graphics.GraphicsDevice.Clear (Color.CornflowerBlue);
  103. spriteBatch.Begin();
  104. spriteBatch.DrawString(font, "A: play\nB: stop\nC: pause\nD: toggle looping\nE: immediate stop\nX/Z volume\nStatus: " +
  105. soundInstance.State.ToString() + "\nLooping: " +
  106. soundInstance.IsLooped.ToString() + "\nVolume: " +
  107. soundInstance.Volume.ToString()
  108. , Vector2.Zero, Color.White);
  109. base.Draw(gameTime);
  110. spriteBatch.End();
  111. }
  112. }
  113. }