- Introduction to the MMAPI
- Simple Audio Playback
- Advanced Media Playback
- Media Capture
- Summary
Simple Audio Playback
The MMAPI implementation on standard Series 40 Developer Platform (e.g., the Nokia 6230 device) supports MIDI and tones playback. In this section, we walk through the source code of a MIDI player MIDlet. We cover MIDI player Controls that are supported on the Series 40 Developer Platform. This example MIDlet is tested on a Nokia 6230 device. When the MidiPlayer MIDlet is started, it shows a screen of playback options (Figure 9-3). Each UI component corresponds to an option in a player Control. When the MIDI file playback is started, the MIDlet displays a status page showing the playback volume of each MIDI channel (Figure 9-4).
Figure 9-3 Playback options in the MidiPlayer MIDlet UI.
Figure 9-4 The MIDI channel volumes.
The MidiPlayer MIDlet
The MidiPlayer class extends the MIDlet class and implements the CommandAction interface. It manages two Form objects: the playerOptions is for the option screen, and the playerStates is for the MIDI channel volume screen. The MidiPlayer class source code is shown below. The constructor retrieves the paths to the local and remote MIDI files from the JAD attributes. The startApp() method builds and displays the UI for the playback options screen.
public class MidiPlayer extends MIDlet implements CommandListener { private Display display; private Command exit; private Command play; private Command back; private TempoControl tempoControl; private MIDIControl midiControl; private RateControl rateControl; private PitchControl pitchControl; private VolumeControl volumeControl; private StopTimeControl stopControl; private TextField tempoField; private TextField pitchField; private TextField rateField; private Gauge volumeGauge; private Gauge stopGauge; private ChoiceGroup config; private Form playerOptions = null; private Form playerStates = null; // media file location public static String mediaMidi; public static String remotePrefix; public static String localPrefix; public MidiPlayer () { display = Display.getDisplay(this); mediaMidi = getAppProperty ("MediaMidi"); remotePrefix = getAppProperty ("RemotePrefix"); localPrefix = getAppProperty ("LocalPrefix"); exit = new Command ("Done", Command.EXIT, 1); play = new Command ("Play", Command.OK, 1); back = new Command ("Back", Command.BACK, 1); } protected void startApp () { playerOptions = new Form ("Midi player"); playerOptions.addCommand (exit); playerOptions.addCommand (play); playerOptions.setCommandListener (this); tempoField = new TextField ( "Tempo", "100000", 7, TextField.NUMERIC); pitchField = new TextField ( "Pitch", "0", 7, TextField.NUMERIC); rateField = new TextField ( "Rate", "100000", 7, TextField.NUMERIC); volumeGauge = new Gauge ("Vol", true, 10, 5); stopGauge = new Gauge ("Stop time", true, 20, 20); config = new ChoiceGroup ("", Choice.MULTIPLE); config.append ("Play local file?", null); config.append ("Play backwards?", null); playerOptions.append (tempoField); playerOptions.append (pitchField); playerOptions.append (rateField); playerOptions.append (stopGauge); playerOptions.append (volumeGauge); playerOptions.append (config); display.setCurrent (playerOptions); } protected void pauseApp () { // Do nothing } protected void destroyApp (boolean unconditional) { // Do nothing } public void commandAction (Command c, Displayable d) { if (c == exit) { destroyApp (true); notifyDestroyed (); } else if (c == play) { try { playMidi (); showStates (); } catch (Exception e) { e.printStackTrace (); } } else if (c == back) { display.setCurrent (playerOptions); } } // playMidi() and showStates() methods ... ... }
The playMidi() and showStates() methods, which are left out of the above code listing, did the real work related to the MMAPI. Now, let's look at those methods more carefully.
Create the Player
The playMidi() method first creates a media player for the MIDI file. Recall that the first checkbox (index zero) in the config component on the playback options screen is "Play local file?" If it is checked, we create a player from a MIDI file bundled in the MIDlet JAR file and manually pass the audio/midi MIME type string to the createPlayer() method. If it is not checked, we create a player from the remote URL. In that case, the MIME type is returned from the server. After the player is created, we realize it, instantiate the controls, set control values, and start the player.
public void playMidi () throws Exception { Player player; // Check if the first check box in config is selected if (config.isSelected(0)) { InputStream is = this.getClass().getResourceAsStream ( localPrefix + mediaMidi); player = Manager.createPlayer(is, "audio/midi"); } else { player = Manager.createPlayer( remotePrefix + mediaMidi); } player.addPlayerListener(new StopListener ()); player.realize(); // Fix the controls ... ... player.start (); }
Player Events
In the playMidi() method, the player.start() statement returns after the playback begins, and the MIDI file plays in the background. In our application, we would like to be notified when the playback process is actually finished so that we can free up the player resources. It is critically important that we close players as soon as we are finished using them. It frees up system resources for other applications and reduces power consumption. Also, we want to pause the playback when the MIDI device is being interrupted by a high-priority system event such as an incoming phone call. The PlayerListener interface provides mechanisms for such notifications and callbacks.
In the playMidi() method code, we added a PlayerListener object to listen for any events in the audio player. The StopListener object's playerUpdate() method is called whenever a player event or an exception occurs. The StopListener object captures the events that correspond to the player-stopping event and puts the player back to closed state.
public class StopListener implements PlayerListener { public void playerUpdate (Player player, String event, Object eventData) { try { if (event.equals (STOPPED) || event.equals (STOPPED_AT_TIME) || event.equals (ERROR) || event.equals (END_OF_MEDIA)) { // Playback is finished player.deallocate (); player.close (); player = null; } else if (event.equals(DEVICE_UNAVAILABLE)) { // Incoming phone call player.stop(); } else if (event.equals(DEVICE_AVAILABLE)) { // Finished phone call. // Start from where we left off player.start(); } System.out.println(event); } catch (Exception e) { e.printStackTrace (); } } }
Player Controls
In the playMidi() method, we set up the player controls before starting it. The standard controls supported in Series 40 Developer Platform are VolumeControl, StopTimeControl, TempoControl, PitchControl, RateControl, and MIDIControl. The code is listed as follows.
public void playMidi () throws Exception { // ... ... player.realize(); volumeControl = (VolumeControl) player.getControl( "VolumeControl"); // The volume is from 0 to 100 volumeControl.setLevel ( volumeGauge.getValue() * 10); stopControl = (StopTimeControl) player.getControl( "StopTimeControl"); // The argument microsecond NOT millisecond! stopControl.setStopTime ( stopGauge.getValue() * 1000000); tempoControl = (TempoControl) player.getControl ( "TempoControl"); int t = Integer.parseInt (tempoField.getString()); tempoControl.setTempo(t); pitchControl = (PitchControl) player.getControl ( "PitchControl"); int p = Integer.parseInt (pitchField.getString()); pitchControl.setPitch(p); rateControl = (RateControl) player.getControl ( "RateControl"); int r = Integer.parseInt (rateField.getString()); // Check whether to reverse playback direction if (config.isSelected(1)) { r = -1 * r; } rateControl.setRate(r); midiControl = (MIDIControl) player.getControl ( "MIDIControl"); player.start (); }
Now, let's check out those controls one by one.
VolumeControl
The VolumeControl object controls the sound volume of the player. We can mute the player via the setMute() method and set the volume using a linear scale from 1 to 100 via setLevel() method. In the MidiPlayer example, each step in the stopGauge gauge corresponds to a volume level change of 10. If the volume changes during the playback, a VOLUME_CHANGED event is delivered to the registered PlayerListener.
StopTimeControl
The StopTimeControl object allows us to preset a stop time for the player. The argument for the setStopTime() time is a long value for microseconds. In the MidiPlayer example, each step in the stopGauge gauge is one second in media play time.
TempoControl
The TempoControl object controls the tempo, which is often specified by beats per second, of a song. To allow fractional beats per minute, the setTempo() method takes in the milli-beat value, which is the beat per minute value multiplied by 1,000.
RateControl
Specifying the absolute tempo of a song is not always a good idea, since the absolute tempo is often overridden by the MIDI file. The RateControl object is used to set a relative tempo multiplier for all absolute tempo settings in this player. The rate, defined as a milli-percentage, is the ratio of the player's media time and the actual TimeBase. For example, a rate of 200,000 (2 x 100 percent x 1,000 milli) indicates that for every 1 second of TimeBase time (the real time), the player needs to play 2 seconds worth of media content. That makes the playback go faster. If the rate is a negative value, the playback would be backwards. The default playback rate is 100,000, which means that there is no change to the absolute tempo. Custom rate factors can be set via the setRate() method. The getMaxRate() and getMinRate() methods return the maximum and minimum rates supported by this device.
PitchControl
The PitchControl object raises or lowers the playback pitch without changing the playback speed. The argument for the setPitch() method is for milli-semitones. For example, a pitch value of 12,000 raises the pitch by 12 semitones and increases the frequency of perceived sound by a factor of 2.
MIDIControl
The MIDIControl object provides advanced access to the MIDI device. It gets or sets the volumes and assigned programs of each of the 16 MIDI channels. The showStates() method shows the volumes of the MIDI channels in the current player.
public void showStates () { playerStates = new Form ("Channel Vols"); playerStates.addCommand (back); playerStates.setCommandListener (this); for (int i = 0; i <= 15; i++) { playerStates.append ("\n " + i + ": " + Integer.toString( midiControl.getChannelVolume(i))); } display.setCurrent(playerStates); }
The MIDIControl object can also be used to send MIDI events to the device. MIDI events support is currently only available on the Nokia 3300, 6230, and 7600 devices. It is planned for future Series 40 devices.