Adding sound in Game Assignment Help

Assignment Help: >> Sprites and sounds in Java >> Adding sound in Game

Adding sound in Game:

The class javax.microedition.media. Manager, which provides facilities for adding musical notes to a J2ME application. These can be built up into simple tunes, which can be played in the background while users are browsing menus or playing games, or simply provide a signature tune when the application is launched. We can also make up short tunes to play in response to particular game events.

Playing a note

The Manager class has only static methods. The one we are interested in here is playTone(int note, int duration, int volume). Its parameters are as follows:

  • note is the musical pitch in the range 0-127;
  • duration is the length in milliseconds;
  • volume is the loudness on a scale of 0-100.

Using this couldn't be simpler. All we need to do is import javax.microedition. media.Manager, then call the method whenever we want to play a note.

For example:

Manager.playTone(72, 125, 50);

will play note 72 for 125 milliseconds at half volume.

Playing a tune

Playing a single note is quite straightforward but how do we play a tune, which is a sequence of notesfi At first sight all we have to do is use several playTone calls, one after another, like this for instance:

Manager.playTone(60, 125, 50); // line 1
Manager.playTone(70, 125, 50); // line 2
Manager.playTone(80, 125, 50); // line 3

Unfortunately this doesn't work because playTone is a non-blocking call.

Usually when a thread calls a method it can't do anything else while the method is executing. Only when the method finishes can the thread execute other code. This is a blocking call.

With a non-blocking call the thread isn't delayed and can go on immediately to execute other code. Meanwhile the method executes in parallel.

So when line 1 above is executed the first note starts playing; however, the program does not wait until the 125 milliseconds are up but proceeds at once to line 2. This sets the second note sounding, and then the program executes line 3 still without any delay. Now all three notes are sounding at once - not at all what we wanted!

To play the three notes separately as intended, we need to make the thread wait until each note has finished before playing the next, like this:

Manager.playTone(60, noteLength1, 50); // line 1
Thread.sleep(noteLength1);
Manager.playTone(70, noteLength2, 50); // line 2
...

You can see an example in the static method endTune in the case study class Game. The method plays a three-note tune several times in succession. The idea is that this jingle gets played if the tick count reaches zero and the user runs out of time!

Background music

The technique we have outlined above works well when we want to play notes in response to things that happen in a game. But what if we want background music that will play independentlyfi In that case we define a runnable class to take care of the music, and launch it in a separate thread. This music thread will need methods for pausing and stopping it.

Non-musical sounds

If we want non-musical sounds we have to use audio clips. Here's the code that produces a suitable sound effect if the robot reaches the portal and is whisked off to another dimension:

InputStream is = getClass().getResourceAsStream("/res/whoosh. wav");
Player player = Manager.createPlayer(is, "audio/X-wav");
player.start();

The first step is to create an input stream for reading the audio file. The stream is then passed to a static method in class Manager, with a second argument specifying what media format is used, in this case a wav file. The method returns an instance of class Player. Invoking start() on this object plays the audio clip.

 

Java Assignment Help - Java Homework Help

Struggling with java programming language? Are you not finding solution for your Adding sound in Game homework and assignments? Live Adding sound in Game experts are working for students by solving their doubts & questions during their course studies and training program. We at Expertsmind.com offer Adding sound in Game homework help, java assignment help and Adding sound in Game projects help anytime from anywhere for 24x7 hours. Computer science programming assignments help making life easy for students.

Why Expertsmind for assignment help

  1. Higher degree holder and experienced experts network
  2. Punctuality and responsibility of work
  3. Quality solution with 100% plagiarism free answers
  4. Time on Delivery
  5. Privacy of information and details
  6. Excellence in solving java programming language queries in excels and word format.
  7. Best tutoring assistance 24x7 hours

 

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd