XNA L06–Input, Audio and Video Playback
-
Upload
mohammad-shaker -
Category
Technology
-
view
302 -
download
0
Transcript of XNA L06–Input, Audio and Video Playback
![Page 1: XNA L06–Input, Audio and Video Playback](https://reader034.fdocuments.us/reader034/viewer/2022052505/5553b46cb4c905d9448b4c6d/html5/thumbnails/1.jpg)
Mohammad Shakermohammadshaker.com
@ZGTRShaker2011, 2012, 2013, 2014
XNA Game DevelopmentL06 – Input, Audio and Video Playback
![Page 2: XNA L06–Input, Audio and Video Playback](https://reader034.fdocuments.us/reader034/viewer/2022052505/5553b46cb4c905d9448b4c6d/html5/thumbnails/2.jpg)
Input Keyboard, Mouse, Touch, Joystick, Sensors, etc.
![Page 3: XNA L06–Input, Audio and Video Playback](https://reader034.fdocuments.us/reader034/viewer/2022052505/5553b46cb4c905d9448b4c6d/html5/thumbnails/3.jpg)
Keyboard Input
![Page 4: XNA L06–Input, Audio and Video Playback](https://reader034.fdocuments.us/reader034/viewer/2022052505/5553b46cb4c905d9448b4c6d/html5/thumbnails/4.jpg)
Keyboard Input
• Why static?
KeyboardState state = Keyboard.GetState();
![Page 5: XNA L06–Input, Audio and Video Playback](https://reader034.fdocuments.us/reader034/viewer/2022052505/5553b46cb4c905d9448b4c6d/html5/thumbnails/5.jpg)
Keyboard Input
• Using Keyboard states
KeyboardState state = Keyboard.GetState();
if(state.IsKeyDown(Keys.Left))
{
// do something here
}
![Page 6: XNA L06–Input, Audio and Video Playback](https://reader034.fdocuments.us/reader034/viewer/2022052505/5553b46cb4c905d9448b4c6d/html5/thumbnails/6.jpg)
Keyboard Input
• Using Keyboard states
bool leftArrowKeyDown = state.IsKeyDown(Keys.Left);
if(state.IsKeyDown(Keys.Left))
{
// do something here
}
![Page 7: XNA L06–Input, Audio and Video Playback](https://reader034.fdocuments.us/reader034/viewer/2022052505/5553b46cb4c905d9448b4c6d/html5/thumbnails/7.jpg)
Keyboard Input
• Using Keyboard states
KeyboardState state = Keyboard.GetState();
if(state.IsKeyDown(Keys.Left))
{
// do something here
}
![Page 8: XNA L06–Input, Audio and Video Playback](https://reader034.fdocuments.us/reader034/viewer/2022052505/5553b46cb4c905d9448b4c6d/html5/thumbnails/8.jpg)
Keyboard Input
• Using Keyboard states
KeyboardState state = Keyboard.GetState();
if(state.IsKeyDown(Keys.Left))
{
// do something here
}
![Page 9: XNA L06–Input, Audio and Video Playback](https://reader034.fdocuments.us/reader034/viewer/2022052505/5553b46cb4c905d9448b4c6d/html5/thumbnails/9.jpg)
Keyboard Input - Checking for Key Presses
if(state.IsKeyDown(Keys.Left))
{
// do something here
}
![Page 10: XNA L06–Input, Audio and Video Playback](https://reader034.fdocuments.us/reader034/viewer/2022052505/5553b46cb4c905d9448b4c6d/html5/thumbnails/10.jpg)
Keyboard Input - Checking for Key Presses
if(state.IsKeyDown(Keys.Left))
{
// do something here
}
![Page 11: XNA L06–Input, Audio and Video Playback](https://reader034.fdocuments.us/reader034/viewer/2022052505/5553b46cb4c905d9448b4c6d/html5/thumbnails/11.jpg)
Keyboard Input - Checking for Key Presses
Keys[] pressedKeys = state.GetPressedKeys();
![Page 12: XNA L06–Input, Audio and Video Playback](https://reader034.fdocuments.us/reader034/viewer/2022052505/5553b46cb4c905d9448b4c6d/html5/thumbnails/12.jpg)
Keyboard Input - Key Modifiers
if(
(
state.IsKeyDown(Keys.LeftControl)|| state.IsKeyDown(Keys.RightControl)
)
&& state.IsKeyDown(Keys.C)
)
{
// Do something here when Ctrl-C is pressed
}
![Page 13: XNA L06–Input, Audio and Video Playback](https://reader034.fdocuments.us/reader034/viewer/2022052505/5553b46cb4c905d9448b4c6d/html5/thumbnails/13.jpg)
Mouse Input
![Page 14: XNA L06–Input, Audio and Video Playback](https://reader034.fdocuments.us/reader034/viewer/2022052505/5553b46cb4c905d9448b4c6d/html5/thumbnails/14.jpg)
Mouse Input - Showing the Mouse
this.IsMouseVisible = true;
![Page 15: XNA L06–Input, Audio and Video Playback](https://reader034.fdocuments.us/reader034/viewer/2022052505/5553b46cb4c905d9448b4c6d/html5/thumbnails/15.jpg)
Mouse Input - Mouse Input?
MouseState mouseState = Mouse.GetState();
if(mouseState.LeftButton == ButtonState.Pressed)
{
// Do whatever you want here
}
![Page 16: XNA L06–Input, Audio and Video Playback](https://reader034.fdocuments.us/reader034/viewer/2022052505/5553b46cb4c905d9448b4c6d/html5/thumbnails/16.jpg)
Mouse Input - Mouse Input?
• Mouse built-in methods
– LeftMouseButton
– MiddleMouseButton
– RightMouseButton
– XButton1
– XButton2
– X
– Y
– ScrollWheelValue
![Page 17: XNA L06–Input, Audio and Video Playback](https://reader034.fdocuments.us/reader034/viewer/2022052505/5553b46cb4c905d9448b4c6d/html5/thumbnails/17.jpg)
Mouse Input - Location
• Getting Location
int x = mouseState.X;
int y = mouseState.Y;
Mouse.SetPosition(xLocation, yLocation);
• Setting Location
![Page 18: XNA L06–Input, Audio and Video Playback](https://reader034.fdocuments.us/reader034/viewer/2022052505/5553b46cb4c905d9448b4c6d/html5/thumbnails/18.jpg)
Xbox(Or Other!) Controller Input
![Page 19: XNA L06–Input, Audio and Video Playback](https://reader034.fdocuments.us/reader034/viewer/2022052505/5553b46cb4c905d9448b4c6d/html5/thumbnails/19.jpg)
Controller Input
![Page 20: XNA L06–Input, Audio and Video Playback](https://reader034.fdocuments.us/reader034/viewer/2022052505/5553b46cb4c905d9448b4c6d/html5/thumbnails/20.jpg)
Controller Input
• GamePadState gamePadState = GamePad.GetState(PlayerIndex.One);
if(gamePadState.IsConnected)
{
// then it is connected, and we can do stuff here
}
if(gamePadState.Buttons.X == ButtonState.Pressed)
{
// do something
}
![Page 21: XNA L06–Input, Audio and Video Playback](https://reader034.fdocuments.us/reader034/viewer/2022052505/5553b46cb4c905d9448b4c6d/html5/thumbnails/21.jpg)
Controller Input
float maxSpeed = 0.1f;
float changeInAngle = gamePadState.Thumbsticks.Left.X * maxSpeed;
// this variable is defined elsewhere
angle += changeInAngle;
![Page 22: XNA L06–Input, Audio and Video Playback](https://reader034.fdocuments.us/reader034/viewer/2022052505/5553b46cb4c905d9448b4c6d/html5/thumbnails/22.jpg)
Controller Input – Vibration effect!
![Page 23: XNA L06–Input, Audio and Video Playback](https://reader034.fdocuments.us/reader034/viewer/2022052505/5553b46cb4c905d9448b4c6d/html5/thumbnails/23.jpg)
Controller Input – Vibration Cool effect!
GamePad.SetVibration(PlayerIndex.One, 1.0f, 1.0f);
![Page 24: XNA L06–Input, Audio and Video Playback](https://reader034.fdocuments.us/reader034/viewer/2022052505/5553b46cb4c905d9448b4c6d/html5/thumbnails/24.jpg)
Audio
![Page 25: XNA L06–Input, Audio and Video Playback](https://reader034.fdocuments.us/reader034/viewer/2022052505/5553b46cb4c905d9448b4c6d/html5/thumbnails/25.jpg)
Audio
• Audio in XNA
• Audio Suggestions for Games
• A Simple Way to Play Sound Effects in XNA
• A Simple Way to Play Background Music in XNA
• Using XACT
• Using XACT Projects in an XNA Game
• XACT Sound Loops
• 3D Audio Effects: Location
• 3D Audio Effects: Attenuation based on Distance
![Page 26: XNA L06–Input, Audio and Video Playback](https://reader034.fdocuments.us/reader034/viewer/2022052505/5553b46cb4c905d9448b4c6d/html5/thumbnails/26.jpg)
Audio in XNA
• The Concept
![Page 27: XNA L06–Input, Audio and Video Playback](https://reader034.fdocuments.us/reader034/viewer/2022052505/5553b46cb4c905d9448b4c6d/html5/thumbnails/27.jpg)
Audio Suggestions For Games
• Audio Suggestions For Games
– royalty free music in google.com
– http://www.incompetech.com/m/c/royalty-free/
– http://www.flashkit.com/
• Use sound in your game. It adds a lot to the game. Don't just ignore it. ( Not in
our uni :( )
![Page 28: XNA L06–Input, Audio and Video Playback](https://reader034.fdocuments.us/reader034/viewer/2022052505/5553b46cb4c905d9448b4c6d/html5/thumbnails/28.jpg)
Playing Sound Effects
• Adding Sound Effects to your Game
– Managing Content!
![Page 29: XNA L06–Input, Audio and Video Playback](https://reader034.fdocuments.us/reader034/viewer/2022052505/5553b46cb4c905d9448b4c6d/html5/thumbnails/29.jpg)
Playing Sound Effects
• WAV Audio File (.wav extension)
private SoundEffect effect;
effect = Content.Load<SoundEffect>("SoundFX/ExtraLife");
![Page 30: XNA L06–Input, Audio and Video Playback](https://reader034.fdocuments.us/reader034/viewer/2022052505/5553b46cb4c905d9448b4c6d/html5/thumbnails/30.jpg)
Playing Sound Effects
• WAV Audio File (.wav extension)
private SoundEffect effect;
effect = Content.Load<SoundEffect>("SoundFX/ExtraLife");
effect.Play();
float volume = 1.0f;
effect.Play(volume);
float volume = 1.0f;
float pitch = -1.0f;
float pan = -1.0f;
bool loop = true;
effect.Play(volume, pitch, pan, loop);
![Page 31: XNA L06–Input, Audio and Video Playback](https://reader034.fdocuments.us/reader034/viewer/2022052505/5553b46cb4c905d9448b4c6d/html5/thumbnails/31.jpg)
Playing Sound Effects
• SoundEffectInstances class
SoundEffectInstance effectInstance = effect1.Play();
effectInstance.Stop();
![Page 32: XNA L06–Input, Audio and Video Playback](https://reader034.fdocuments.us/reader034/viewer/2022052505/5553b46cb4c905d9448b4c6d/html5/thumbnails/32.jpg)
Playing Sound Effects
• SoundEffectInstances class
SoundEffectInstance effectInstance = effect1.Play();
effectInstance.Stop();
![Page 33: XNA L06–Input, Audio and Video Playback](https://reader034.fdocuments.us/reader034/viewer/2022052505/5553b46cb4c905d9448b4c6d/html5/thumbnails/33.jpg)
Playing Background Music
• Using the Song and MediaPlayer Classes to Play Audio
Song song = Content.Load<Song>("song_title");
// Put the name of your song here instead of "song_title"
MediaPlayer.Play(song);
MediaPlayer.IsRepeating = true;
![Page 34: XNA L06–Input, Audio and Video Playback](https://reader034.fdocuments.us/reader034/viewer/2022052505/5553b46cb4c905d9448b4c6d/html5/thumbnails/34.jpg)
Using XACT
• Cross-platform Audio Creation Tool
• The concept
• Using XACT
– Using XACT
– Using XACT Projects in an XNA Game
– XACT Sound Loops
– 3D Audio Effects: Location
– 3D Audio Effects: Attenuation based on Distance
![Page 35: XNA L06–Input, Audio and Video Playback](https://reader034.fdocuments.us/reader034/viewer/2022052505/5553b46cb4c905d9448b4c6d/html5/thumbnails/35.jpg)
Using XACT
• Small Tutorials
– http://rbwhitaker.wikidot.com/audio-tutorials
• http://rbwhitaker.wikidot.com/using-xact
• http://rbwhitaker.wikidot.com/playing-sound
• http://rbwhitaker.wikidot.com/xact-sound-loops
• http://rbwhitaker.wikidot.com/3d-audio-effects-location
– Books
• Microsoft Book
• Aaron Reed (O’Reilly)
![Page 36: XNA L06–Input, Audio and Video Playback](https://reader034.fdocuments.us/reader034/viewer/2022052505/5553b46cb4c905d9448b4c6d/html5/thumbnails/36.jpg)
Video Playback
![Page 37: XNA L06–Input, Audio and Video Playback](https://reader034.fdocuments.us/reader034/viewer/2022052505/5553b46cb4c905d9448b4c6d/html5/thumbnails/37.jpg)
Video Playback
• Loading the Video in XNA, Game1 Global Scope
Video video;
VideoPlayer player;
![Page 38: XNA L06–Input, Audio and Video Playback](https://reader034.fdocuments.us/reader034/viewer/2022052505/5553b46cb4c905d9448b4c6d/html5/thumbnails/38.jpg)
Video Playback
• Loading the Video in XNA, Game1 Global Scope
• LoadContent()
Video video;
VideoPlayer player;
video = Content.Load<Video>("AVideoToPlayback");
player = new VideoPlayer();
![Page 39: XNA L06–Input, Audio and Video Playback](https://reader034.fdocuments.us/reader034/viewer/2022052505/5553b46cb4c905d9448b4c6d/html5/thumbnails/39.jpg)
Video Playback
• Starting the Video
player.Play(video);
![Page 40: XNA L06–Input, Audio and Video Playback](https://reader034.fdocuments.us/reader034/viewer/2022052505/5553b46cb4c905d9448b4c6d/html5/thumbnails/40.jpg)
Video Playback
• Starting the Video
• Update()
player.Play(video);
if (player.State == MediaState.Stopped)
{
player.IsLooped = true;
player.Play(video);
}
![Page 41: XNA L06–Input, Audio and Video Playback](https://reader034.fdocuments.us/reader034/viewer/2022052505/5553b46cb4c905d9448b4c6d/html5/thumbnails/41.jpg)
Video Playback
• Looping
player.IsLooped = true;
![Page 42: XNA L06–Input, Audio and Video Playback](https://reader034.fdocuments.us/reader034/viewer/2022052505/5553b46cb4c905d9448b4c6d/html5/thumbnails/42.jpg)
Video Playback
• Drawing the Video, Draw()
Texture2D videoTexture = null;
if (player.State!= MediaState.Stopped)
videoTexture = player.GetTexture();
![Page 43: XNA L06–Input, Audio and Video Playback](https://reader034.fdocuments.us/reader034/viewer/2022052505/5553b46cb4c905d9448b4c6d/html5/thumbnails/43.jpg)
Video Playback
• Drawing the Video, Draw()
Texture2D videoTexture = null;
if (player.State!= MediaState.Stopped)
videoTexture = player.GetTexture();
![Page 44: XNA L06–Input, Audio and Video Playback](https://reader034.fdocuments.us/reader034/viewer/2022052505/5553b46cb4c905d9448b4c6d/html5/thumbnails/44.jpg)
Video Playback
• Drawing the Video, Draw()
Texture2D videoTexture = null;
if (player.State!= MediaState.Stopped)
videoTexture = player.GetTexture();
![Page 45: XNA L06–Input, Audio and Video Playback](https://reader034.fdocuments.us/reader034/viewer/2022052505/5553b46cb4c905d9448b4c6d/html5/thumbnails/45.jpg)
Video Playback
• Drawing the Video, Draw()
Texture2D videoTexture = null;
if (player.State!= MediaState.Stopped)
videoTexture = player.GetTexture();
![Page 46: XNA L06–Input, Audio and Video Playback](https://reader034.fdocuments.us/reader034/viewer/2022052505/5553b46cb4c905d9448b4c6d/html5/thumbnails/46.jpg)
Video Playback
• Drawing the Video, Draw()
Texture2D videoTexture = null;
if (player.State!= MediaState.Stopped)
videoTexture = player.GetTexture();
if (videoTexture!= null)
{
spriteBatch.Begin();
spriteBatch.Draw(videoTexture, new Rectangle(0, 0, 400, 300), Color.White); spriteBatch.End();
}