Java Reference
In-Depth Information
Listing 16-3.
Using a Custom DataSource with the MMAPI Manager
Player player;
try {
DataSource ds = (DataSource)new CleverStreamingDataSource(
"proto://noplace.com/audio.mp3" );
player = Manager.createPlayer(ds);
player.realize();
player.prefetch();
player.start();
} catch (MediaException e) {}
catch (IOException e) {}
Implementing
CleverStreamingDataSource
as a subclass of
DataSource
permits the
Manager
to create a player that connects to your data source for its data.
■
Tip
Writing a custom
DataSource
is no easy task and is beyond the scope of this topic. For more details,
I suggest you begin with Vikram Goyal's article on Java ME content streaming, “Experiments in Streaming
Content in Java ME,” at
http://today.java.net/pub/a/today/2006/08/22/experiments-in-
streaming-java-me.html
. In particular, take a look at the “Create a custom DataSource” section.
Regardless of how you create and start your
Player
instance, you will want to do so in
a separate thread from the main application thread, because it's a time-consuming affair.
I show you how to do this in practice later in this chapter, in the “Putting the MMAPI and
the SVGAPI to Work” section.
Once you're done rendering media, at some point you will want to stop playback
and reclaim the resources the
Player
instance has consumed. Listing 16-4 shows the
usual process.
Listing 16-4.
Stopping Media Playback and Reclaiming Resources
if (player != null) {
player.stop();
player.close();
player = null;
}
Interestingly, the
Player
's
stop
method doesn't actually stop playback; it pauses it.
That means that you can implement a user-initiated pause by invoking a
Player
instance's
stop
method, and you can restore playback where it was paused by invoking
its
play
method again.