Playing from a Raw Resource

Perhaps the most common thing to want to do is play back media (notably sound) within your own applications. Doing this is easy:

1. Put the sound (or other media resource) file into the res/raw folder of your project, where the Eclipse plugin (or aapt) will find it and make it into a resource that can be referenced from your R class

2. Create an instance of MediaPlayer, referencing that resource using MediaPlayer.create, and then call startO on the instance:

MediaPlayer mp = MediaPlayer.create(context, R.raw.sound_file_1); mp.start();

To stop playback, call stop). If you wish to later replay the media, then you must reset() and prepared the MediaPlayer object before calling startO again. (create() calls prepare() the first time.)

To pause playback, call pause(). Resume playback from where you paused with startO. Playing from a File or Stream

You can play back media files from the filesystem or a web URL: 1. Create an instance of the MediaPlayer using new

2. Call setDataSourceO with a String containing the path (local filesystem or URL) to the file you want to play

3. First prepareO then start() on the instance:

MediaPlayer mp = new MediaPlayer();


stopO and pause() work the same as discussed above.

Note: It is possible that mp could be null, so good code should null check after the new. Also, IllegalArgumentException and IOException either need to be caught or passed on when using setDataSource(), since the file you are referencing may not exist.

| Note: If you're passing a URL to an online media file, the file must be capable of progressive download. Playing JET content

The Android platform includes a JET engine that lets you add interactive playback of JET audio content in your applications. You can create JET content for interactive playback using the JetCreator authoring application that ships with the SDK. To play and manage JET content from your application, use the JetPlayer class.

For a description of JET concepts and instructions on how to use the JetCreator authoring tool, see the JetCreator User Manual. The tool is available fully-featured on the OS X and Windows platforms and the Linux version supports all the content creation features, but not the auditioning of the imported assets.

Here's an example of how to set up JET playback from a .jet file stored on the SD card:

JetPlayer myJet = JetPlayer.getJetPlayer(); myJet.loadJetFile("/sdcard/level1.jet"); byte segmentId = 0;

// queue segment 5, repeat once, use General MIDI, transpose by -1 octave myJet.queueJetSegment(5, -1, 1, -1, 0, segmentId++); // queue segment 2

myJet.queueJetSegment(2, -1, 0, 0, 0, segmentId++);;

The SDK includes an example application — JetBoy — that shows how to use JetPlayer to create an interactive music soundtrack in your game. It also illustrates how to use JET events to synchronize music and game logic. The application is located at <sdk>/platforms/android-1.5/samples/JetBoy.

Was this article helpful?

0 0

Post a comment