Dmitrij Solov

Dmitrij Solov

  • NA
  • 43
  • 2.4k

xna sound-issue

Jul 25 2017 2:29 PM
The program seems to slow down, the system seems overloaded whe I try to play music in xna with the following code. What goes wrong?
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using Microsoft.Xna.Framework;  
  5. using Microsoft.Xna.Framework.Audio;  
  6. using Microsoft.Xna.Framework.Content;  
  7. using Microsoft.Xna.Framework.GamerServices;  
  8. using Microsoft.Xna.Framework.Graphics;  
  9. using Microsoft.Xna.Framework.Input;  
  10. using Microsoft.Xna.Framework.Media;  
  11.   
  12. namespace WindowsGame1  
  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.         SpriteBatch spriteBatch;  
  21.         //1 erstelle sound-ordner und sound-datei in diesem ordner wie in diesem programm  
  22.         Song mySong01;//2  
  23.         Song mySong02;//2  
  24.   
  25.         public Game1()  
  26.         {  
  27.             graphics = new GraphicsDeviceManager(this);  
  28.             Content.RootDirectory = "Content";  
  29.         }  
  30.   
  31.         /// <summary>  
  32.         /// Allows the game to perform any initialization it needs to before starting to run.  
  33.         /// This is where it can query for any required services and load any non-graphic  
  34.         /// related content.  Calling base.Initialize will enumerate through any components  
  35.         /// and initialize them as well.  
  36.         /// </summary>  
  37.         protected override void Initialize()  
  38.         {  
  39.             // TODO: Add your initialization logic here  
  40.   
  41.             base.Initialize();  
  42.         }  
  43.   
  44.         /// <summary>  
  45.         /// LoadContent will be called once per game and is the place to load  
  46.         /// all of your content.  
  47.         /// </summary>  
  48.         protected override void LoadContent()  
  49.         {  
  50.             // Create a new SpriteBatch, which can be used to draw textures.  
  51.             spriteBatch = new SpriteBatch(GraphicsDevice);  
  52.   
  53.             // TODO: use this.Content to load your game content here  
  54.               
  55.             mySong01 = Content.Load<Song>(@"Sound/01-song");//3  
  56.             mySong02 = Content.Load<Song>(@"Sound/02-song");  
  57.         }  
  58.   
  59.         /// <summary>  
  60.         /// UnloadContent will be called once per game and is the place to unload  
  61.         /// all content.  
  62.         /// </summary>  
  63.         protected override void UnloadContent()  
  64.         {  
  65.             // TODO: Unload any non ContentManager content here  
  66.         }  
  67.   
  68.         /// <summary>  
  69.         /// Allows the game to run logic such as updating the world,  
  70.         /// checking for collisions, gathering input, and playing audio.  
  71.         /// </summary>  
  72.         /// <param name="gameTime">Provides a snapshot of timing values.</param>  
  73.         protected override void Update(GameTime gameTime)  
  74.         {  
  75.             // Allows the game to exit  
  76.             if (Keyboard.GetState().IsKeyDown(Keys.F12))  
  77.                 this.Exit();  
  78.   
  79.             // TODO: Add your update logic here  
  80.             string []keys = new string []{"i1","i2","i3","i4"};  
  81.               
  82.             if (Keyboard.GetState().IsKeyDown(Keys.F4))  
  83.                 keys[0] = "i1";  
  84.             else if (Keyboard.GetState().IsKeyDown(Keys.F5))  
  85.                 keys[0] = "i2";  
  86.             switch(keys[0])  
  87.             {  
  88.                 case "i1":  
  89.                     MediaPlayer.Play(mySong01);  
  90.                     break;  
  91.                 case "i2":  
  92.                     MediaPlayer.Play(mySong02);  
  93.                     break;  
  94.             }  
  95.   
  96.             base.Update(gameTime);  
  97.         }  
  98.   
  99.         /// <summary>  
  100.         /// This is called when the game should draw itself.  
  101.         /// </summary>  
  102.         /// <param name="gameTime">Provides a snapshot of timing values.</param>  
  103.         protected override void Draw(GameTime gameTime)  
  104.         {  
  105.             GraphicsDevice.Clear(Color.CornflowerBlue);  
  106.   
  107.             // TODO: Add your drawing code here  
  108.   
  109.             base.Draw(gameTime);  
  110.         }  
  111.     }  

 

Answers (2)