Java Media Control API: Using a MediaGroup
Friday, April 29th, 2011Upon the completion of SDP negotiation, the media server essentially gets a media pipe to the client, represented as the NetworkConnection. Now, to have some fun, we need to connect that pipe to a multimedia studio that can play, record, and recognize different sound.
MediaGroup
This multimedia studio is called MediaGroup in Java Media Control API. MediaGroup contains four Resources — Player, Recorder, SignalDetector, and SignalGenerator. Once a MediaGroup is connected to a NetworkConnection, you can use these Resources to, e.g. play music to the client.
MediaGroup can be created from a MediaSession based on a Configuration and, optionally, Parameters.
MediaSession.createMediaGroup(Configuration);
The Configuration indicates to the media server what Resources that the application might be using (or not using). Thus the media server can allocate appropriate resources for this MediaGroup. Typically you will use one of the pre-defined Configurations
PLAYER: Use this when the application will only use Player in this MediaGroup.PLAYER_SIGNALDETECTOR: Use this when the application will only use Player and SignalDetector in this MediaGroup.PLAYER_RECORDER_SIGNALDETECTOR: Use this when the application will use all Resources but SignalGenerator in this MediaGroup.PLAYER_RECORDER_SIGNALDETECTOR_SIGNALGENERATOR: Use this when the application will use all Resources in this MediaGroup.
To use the MediaGroup, simply joining the NetworkConnection with the MediaGroup. (See Media Object Composition about join concept.) Now you can provide multimedia functions for the client by using different Resource in the MediaGroup.
NetworkConnection nc = ...
MediaGroup mg = session.createMediaGroup(MediaGroup.PLAYER_RECORDER_SIGNALDETECTOR);
nc.join(mg);
mg.getPlayer().play(URI.create("http://myserver.com/mysong.wav"), RTC.NO_RTC, Parameters.NO_PARAMETER);
Let’s take a look what you can do with each Resource in the MediaGroup.
Player
Player can be used to play media streams from a URI (or a set of URIs). For example, the following code snippet shows how to play a song from a Web server.
mg.getPlayer().play(URI.create("http://myserver.com/mysong.wav"), RTC.NO_RTC, Parameters.NO_PARAMETER);
If the media server supports text-to-speech, you can use SSML to render text into synthesized speech. For example,
final static String HELLO_WORLD_SSML = URLEncoder.encode("application/ssml+xml, <?xml version=\"1.0\"?><speak><voice>Hello World!</voice></speak>", "UTF-8");
mg.getPlayer().play(URI.create("data:" + HELLO_WORLD_SSML), RTC.NO_RTC, Parameters.NO_PARAMETER);
You can ask the Player to play multiple items at once by giving an array of URIs.
To stop playing, simply call stop operation to stop either the current item being played or all the items in the queue.
You can also pause and resume the play, as well as adjusting speed and volume.
Recorder
Recorder can be used to record media to URI. For example, the following code snippet shows how to record your voice into a file on a Web server.
mg.getRecorder().record(URI.create("http://myserver.com/mysong.wav"), RTC.NO_RTC, Parameters.NO_PARAMETER);
To stop recording, simply call stop operation.
You can also pause and resume the recording.
SignalDetector
SignalDetector can be used to collect user input based on a set of patterns. For example, the following code snippet tries to receive one DTMF input from the user.
mg.getSignalDetector().receiveSignals(1, SignalDetector.NO_PATTERN, RTC.NO_RTC, Parameters.NO_PARAMETER);
The use of Parameter and pattern label in Java Media Control API is a bit akward. I will explain more in the future blog.
Similarly, you can call stop() to stop the receiveSignal operation.
SignalGenerator
SignalGenerator can be used to generate signal into the media server. This is typically used when the DTMF input are received via SIP INFO.
Sample Application
Here is a sample application that will announce “Hello World” after you dial into it. Then it will ask you to input “1″ for replay. Any other key input will determinate the call.
To run this sample, assuming you have Voxeo Prism downloaded and installed, simply drop the Tutorial.3.war to <prism>/apps directory. Once you have Voxeo Prism (both Application Server and Media Server) started, simply dial “sip:user@localhost” from your SIP phone such as SJPhone or XLite.
Source code is included in the sample .
Want to learn how Voxeo can help unlock your communications and deliver a better customer experience? Please contact us!
If you found this post interesting or helpful, please consider either subscribing via RSS, becoming a fan on Facebook, or following us on Twitter.









As I’ve continued to work with IPv6 on my home network (which I set up
Carrying on a fine tradition, the IETF today released
RSS Feed



