Wednesday, December 29, 2010

XNA Game Development Tips and Tricks 3: Windows Phone 7 Playing Music and Sound FX

You are developing a game, and your game is pretty decent but game without sound and music is not good game and does not create excitement that it needs to. For example Xbox 360 has Achievement earning, when you earn achievement sound of achievement earning is so nice to ears that you would love to play game just for the sake of earning achievements. Anyways that's apart coming back to todays tips and tricks. We are going to see how you can play Sound and Music in game using Windows Phone 7, what are challenges, tips and tricks.

XNA provides two ways of playing sound/music:

1 – SoundEffect
2 – Song using MediaPlayer

When you want to play small sounds, like explosion of bomb or sound of coin dropping on floor or similar sound effects you would want to use SoundEffect class. and when you want to play Background Music you would want to use Song class. Difference is that SoundEffect only plays WAV files, so when you use Music as WAV file, your music would be at-least 1 minute long, and 1 minute for WAV file is huge, your wav file will be very large approximate 20-30 MB.

Song class has another issue that at a time we can play only one file, we can not play more than 1 file simultaneously. So if you want to play Human voice, you might want to make them as WAV file and play them using SoundEffect class.

Have a look at following code:

SoundEffect sfx = Content.Load<SoundEffect>(“coinsound”);
sfx.CreateInstance().Play();

Above code will load content which is “coinsound” in wav file format, we create it’s instance and then play. Now you must be wondering why to call CreateInstance? when we are already initializing object. Now take a case when you collect coin you want to play sound so you execute above code, now above code works fine, but what if we collect multiple coins at same time then what to do in that case? In that case above code will produce sound of coin only once, even if you execute Play method multiple times, and will not give kind of effect we are looking for. So to produce multiple sound effect of same sound file, we can use same object but every time we will have to call CreateInstance and then call Play method.

Now, for playing Music we write below code:

Song sng = Content.Load<Song>(“backmusic”);
MediaPlayer.Play(sng);
MediaPlayer.IsRepeating = true;

Notice we are using MediaPlayer class for playing music, it means it will be using Zune Player to play the music. Now when you write above code and run your application you will come across given below error:

“Song playback failed. Please verify that the song is not DRM protected. DRM protected songs are not supported for creator games.”

image

This happens because your Windows Phone’s Zune Library is locked by Zune Software on PC.. Currently possible way of doing is to first run your game through Visual Studio on Device, when you encounter above error, stop debugging, disconnect device from PC and then start game on device through applications, and now you should be able to hear Music. NOTE: When you do SoundEffect you will not have any problem as above, It is only with MediaPlayer.

Sunday, December 26, 2010

XNA Game Development Tips and Tricks 2: Windows Phone 7 Full Screen Game

Whenever you develop a game for Windows Phone 7 using XNA game does not start in Full Screen mode, meaning when game starts it does not occupy the entire screen instead it leaves some space on top if running in Portrait mode or leaves some space on left side if running in Landscape mode.

image    image

This happens because Windows Phone Operating System makes top space or left space available for the battery meter indicator, time, cell signal, wifi signal, etc… now this can make your game look really bad and you will not be able to utilize the entire screen. So what to do if you want to occupy entire screen? Well, very simple you just have to write one single line of code, look at below code snippet:

public Game1()
{
    graphics = new GraphicsDeviceManager(this);
    graphics.IsFullScreen = true;
    Content.RootDirectory = "Content"; 
    TargetElapsedTime = TimeSpan.FromTicks(333333);
}

Writing above code will hide the bar which occupied the space. But this does not mean your game will block calls, notifications, etc.. whenever any kind of notification or call arrives Windows Phone Operating System will alert your game saying User is not able to see screen so that you can pause the game. This happens only when you receive a call or notification which would take up a space of screen.

For the partial-screen Obscured notification and the tombstoning notification to the Game.Deactivated event will be fired. PhoneApplicationService.Deactivated event will fires for both Silverlight and XNA when the app is being tombstoned. Obscured and Unobscured are not binded in App.xaml.cs file is because there will be many non-game applications which will not need to use these events.

Once you done with the above code, compile and run the code and notice the difference.

image    image

Saturday, December 25, 2010

XNA Game Development Tips and Tricks 1: Windows Phone 7 Portrait Mode

Hello, with this blog post I am starting tips and tricks on Developing Games on Windows Phone 7 using XNA.. or in general XNA.

By default whenever you develop game for Windows Phone 7 using XNA, your game will always start in Landscape mode. Your emulator will start in portrait mode, but game is playable in landscape mode.

image

Let’s say you have requirement of developing game for portrait mode and not landscape mode, you have to add 2 lines of code. In your game after you initialize GraphicsDeviceManager write following 2 lines:

 

public Game1()
{
    graphics = new GraphicsDeviceManager(this);
    Content.RootDirectory = "Content";
    graphics.PreferredBackBufferWidth = 480;
    graphics.PreferredBackBufferHeight = 800;

    TargetElapsedTime = TimeSpan.FromTicks(333333);
}

 

after writing above code, compile and run it. .and you will have your game starting in Portrait mode.

image

Friday, December 24, 2010

PuneUserGroup’s DevCon 2010

PUG DevCon 2010 was held on 18-19 December 2010 in ICC Towers, Pune. It was multi track event.

On 1st day we had 2 tracks. In 1st track, sessions were taken by Sanjay Vyaas, Nauzad Kapadia and Sarang Datye. In 2nd track, sessions were taken by Ashiwni Shahane, Saranya Sriram, Dhaval Faria and Ketaki Kode. Topics covered on 1st day were NET 4.0, multithreaded applications with Net 4.0, Office business applications, Azure applications, Cloud, XNA and Silverlight.

On 2nd day we had 3 tracks. 1st track included VS2010 sessions, 2nd track included Silverlight sessions and 3rd track included Sharepoint sessions. Sessions on 2nd day were taken by, Dhananjay Kumar, Subodh Sohoni, Gouri Sohoni, Kunal Chowdhary, Pradnya Naik, Nauzad Kapadia, Mahesh Sabnis, Mayur Tendulkar, Sudhendra Kohlam, Monish Darda, Raj Chaudhari, Sankool Shah and Dhaval Faria.

I was speaker for 1st day 2nd track. I spoke on Developing games using XNA game studio along with Dhaval Faria. I covered theory part of basics on building a game, previous video game consoles. Dhaval Faria covered the coding part for a game. Being a PUG volunteer, I also handled registration counter for both days.

Whole event was a big hit. Almost 140 people attended on Day 1 and 100 people attended on Day 2.