Mobile media API (mmapi)

Source: Internet
Author: User
Tags pngimage prefetch
Mmapi is a set of standard playback and recording audio or video interfaces provided by JSR 135: mobile media API.

Mmapi framework: (from: mongos.sun.com)

The manager class (javax. microedition. Media. Manager) is used to create various types of players, obtain various supported protocols and content formats, and play simple tunes.

The manager creates a data source object based on the parameters of the createplayer function. The object transfers media data and obtains the data content type of the media from the data, then, create the corresponding Player Object Based on the media data type. If the manager cannot determine the content type of datasource, it will throw a mediaexception exception:

Reference text

// Public static player manager. createplayer (string Locator) throws ioexception, mediaexception
// Public static player manager. createplayer (datasource source) throws ioexception, mediaexception
// Public static player manager. createplayer (inputstream stream, string type) throws ioexception, mediaexception
Try {
Player P = manager. createplayer ("http: // webserver/musicloud ");
P. setloopcount (5 );
P. Start ();
} Catch (ioexception IOE ){
} Catch (mediaexception me ){
}

Obtain the protocols and media types supported by mobile phones:

Reference text

// Static java. Lang. String [] manager. getsupportedprotocols (Java. Lang. String content_type)
// Static java. Lang. String [] manager. getsupportedcontenttypes (Java. Lang. String Protocol)

Simple playing tone:

Reference text

// Static void playtone (INT note, int duration, int volume)
Try {
Manager. playtone (tonecontrol. C4, 5000/* millisec */, 100/* max vol */);
// Tonecontrol. C4 audio C
// Tonecontrol. silence does not sound
} Catch (mediaexception e ){
}

Mmapi uses player to process media data. Player is javax. microedition. media. player interface implementation instance, which reads media data from data source, parses and decodes data, and recognizes media output devices and transmits media data to output devices.

Player provides a set of methods to control media replay and synchronization:
Player. Start (): replay a media stream.
Player. Stop (): stops a media stream.
Player. setmediatime (Long Now): sets the media time.
Player. Close (): closes media streams and releases resources.
Player. getstate (): gets the current state of the player:

Detailed replay control:

Reference text

Static final long secs_to_microsecs = 000000l;
Player P;
Volumecontrol VC;
Try {
P = manager. createplayer ("http: // webserver/musicloud ");
P. Realize ();
// Set a listener.
P. addplayerlistener (new listener ());
// Grab volume control for the player.
// Set volume to Max.
VC = (volumecontrol) p. getcontrol ("volumecontrol ");
If (VC! = NULL ){
VC. setlevel (100 );
}
// Set a start time.
P. setmediatime (5 * secs_to_microsecs );
// Guarantee that the player can start with the smallest latency.
P. prefetch ();
// Non-blocking start
P. Start ();
} Catch (ioexception IOE ){
} Catch (mediaexception me ){
}

Class listener implements playerlistener {
Public void playerupdate (player P, string event, object eventdata ){
If (event = end_of_media | event = stop_at_time ){
System. Out. println ("done processing ");
Try {
P. setmediatime (5 * secs_to_microsecs );
P. Start ();
} Catch (mediaexception me ){
}
}
}
}

Play back the data stored in rms:

Reference text

Recordstore RS;
Int recordid;
// Code to set up the record store.
Try {
Inputstream is = new
Bytearrayinputstream (Rs. getrecord (recordid ));
Player P = manager. createplayer (is, "audio/X-WAV ");
P. start ();
} Catch (IOException ioe ){
} Catch (MediaException me ){
}

Play the media stored in the jar file:

Reference text

/** Notice that in MIDP 2.0, the wav format is mandatory only */
/** In the case that the device supports sampled audio .*/
Try {
InputStream is = getClass (). getResourceAsStream ("audio.wav ");
Player p = Manager. createPlayer (is, "audio/X-wav ");
P. start ();
} Catch (IOException ioe ){
} Catch (MediaException me ){
}

Synchronization of different players:

Reference text

Player p1, p2;

Try {
P1 = Manager. createPlayer ("http: // webserver/tune. mid ");
P1.realize ();
P2 = manager. createplayer ("http: // webserver/movie. mpg ");
P2.realize ();
P2.settimebase (p1.gettimebase ());
P1.prefetch ();
P2.prefetch ();
P1.start ();
P2.start ();
} Catch (ioexception IOE ){
} Catch (mediaexception me ){
}

Mmapi also provides one or more controls to adjust the player's behavior, which can be obtained from the player instance when the player converts data from the media and the controls is used. We can use some special controls provided in player to access some special media types. Controls is implemented by the javax. microedition. Media. Control Interface.

MIDI replay control:

Reference text

Player P;
Tempocontrol TC;

Try {
P = manager. createplayer ("http: // webserver/tune. Mid ");
P. Realize ();
// Grab the tempo control.
TC = (tempocontrol) p. getcontrol ("tempocontrol ");
TC. settempo (120000); // 120 beats/min
P. Start ();
} Catch (ioexception IOE ){
} Catch (mediaexception me ){
}

Video replay function implementation:

Reference text

Player P;
Videocontrol VC;

Try {
P = manager. createplayer ("http: // webserver/movie. mpg ");
P. Realize ();
// Grab the video control and set it to the current display.
VC = (videocontrol) p. getcontrol ("videocontrol ");
If (VC! = NULL ){
Form = new form ("video ");
Form. append (item) VC. initdisplaymode (VC. use_gui_primitive, null ));
Display. getdisplay (MIDlet). setcurrent (form );
}
P. Start ();
} Catch (ioexception IOE ){
} Catch (mediaexception me ){
}

Generate single-tone sequence:

Reference text

Byte tempo = 30; // set tempo to 120 bpm
Byte d = 8; // eighth-Note

Byte C4 = tonecontrol. C4 ;;
Byte D4 = (byte) (C4 + 2); // a whole step
Byte E4 = (byte) (C4 + 4); // a major third
Byte G4 = (byte) (C4 + 7); // a th
Byte rest = tonecontrol. Silence; // rest

Byte [] mysequence = {
Tonecontrol. Version, 1, // version 1
Tonecontrol. Tempo, tempo, // set tempo
Tonecontrol. block_start, 0, // start define "A" section
E4, D, D4, D, C4, D, E4, D, // content of "A" section
E4, D, E4, D, E4, D, rest, D,
Tonecontrol. block_end, 0, // end define "A" section
Tonecontrol. play_block, 0, // play "A" section
D4, D, D4, D, D4, D, rest, D, // play "B" section
E4, D, G4, D, G4, D, rest, D,
Tonecontrol. play_block, 0, // repeat "A" section
D4, D, D4, D, E4, D, D4, D, C4, d // play "C" section
};

Try {
Player P = manager. createplayer (Manager. tone_device_locator );
P. Realize ();
Tonecontrol c = (tonecontrol) p. getcontrol ("tonecontrol ");
C. setsequence (mysequence );
P. Start ();
} Catch (ioexception IOE ){
} Catch (mediaexception me ){
}

Voice Capture and recording functions:

Reference text

Try {
// Create a datasource that captures live audio.
Player P = manager. createplayer ("Capture: // audio ");
P. Realize ();
// Get the recordcontrol, set the record location, and
// Start the player and record for 5 seconds.
RecordControl rc = (RecordControl) p. getControl ("RecordControl ");
Rc. setRecordLocation ("file:/tmp/audio.wav ");
Rc. startRecord ();
P. start ();
Thread. currentThread (). sleep (5000 );
P. stop ();
Rc. stopRecord ();
Rc. commit ();
} Catch (IOException ioe ){
} Catch (MediaException me ){
} Catch (InterruptedException e ){
}

Camera functions:

Reference text

Player p;
VideoControl vc;

// Initialize camera
Try {
P = Manager. createPlayer ("capture: // video ");
P. realize ();
// Grab the video control and set it to the current display.
VC = (videocontrol) (p. getcontrol ("videocontrol "));
If (videocontrol! = NULL ){
VC. initdisplaymode (videocontrol. use_direct_video, this );
// Show camera
P. Start ();
VC. setvisible (true );
// Now take a picture
Byte [] pngimage = VC. getsnapshot (null );
Image image = image. createimage (pngimage, 0, pngimage. Length );
}
} Catch (ioexception IOE ){
} Catch (mediaexception me ){
} Catch (securityexception SE ){
} Finally {
If (player! = NULL ){
Player. Close ();
Player = NULL;
}
Videocontrol = NULL;
}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.