Develop Mobile Multimedia Application Technology Using j2mmmapi

Source: Internet
Author: User

I. Introduction

There are a large number of different media formats in the current world, and many new media formats will be created soon. To store and transmit these different media formats, there are many storage devices and transmission protocols in different formats, such as media storage devices (such as CD, VCD, and DVD) that are commonly used ), wired transmission protocols (such as UDP and HTTP) and wireless transmission protocols (such as WAP ).

To enable mobile devices to access media data in different formats, a standardized, powerful, and scalable application interface must be designed for them. The mobile media API (mmapi) provides a set of standard APIs for playing and recording audio or video ).

Ii. mmapi Architecture

Generally, the media processing process can be divided into two processes:

◆ The process of processing the transmission protocol of media data.
◆ Process of processing media data content.

1. Process of media data transmission protocol

The transfer protocol processing process refers to the process of reading media data from a data source (such as a file, a capturing device or a stream service) and transmitting it to media data content processing.

Mmapi uses data source to process the transmission protocol of media data. A Data Source knows how to read media data from its original location and send it to the Media Processing (player ). Media data can be stored in different locations, from remote servers to resource files or rms databases. Media data can be transmitted from the original location through HTTP, stream transmission protocol like RTP, or other mechanisms to Media Processing (player ). Figure 1 shows how data source works.

498) This. style. width = 498; ">
Figure 1: Data Source Workflow

Javax. microedition. Media. Protocol. datasource provides mmapi data source support.

2. Process media data content

The process of processing media data usually requires the interpretation and decoding of media data, and you also need to identify the type of output settings of the media, such as audio settings or video settings. For example, when datasource obtains an MP3 media data from a media data source and transmits it to the media data processing process, this process first parses and decodes the MP3 media data, detects that the MP3 media is audio data, and enables the device's audio device, the parsed and decoded media data is directly transmitted to the device's audio device buffer, which generates audio signals based on the data in the buffer.

Mmapi uses player to process media data. A player is javax. microedition. media. an Implementation instance of the player interface, which reads media data from the data source, parses and decodes data, recognizes media output devices, and transfers media data to output devices. Player provides a set of methods to control media replay and synchronization.

Mmapi also provides one or more controls to adjust the player's behavior. It can be obtained from a player instance when the player converts data from the media and uses controls. 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.

3. Manager

To effectively manage datasource and player, the factory mechanism is used by the manager to create player and datasource. This mechanism is everywhere in Java, such as drivermanager in JDBC. I will not introduce this mechanism here. Manager can not only create a player from datasource, but also create a player from a local device or inputstream. Figure 2 shows the overall mmapi structure.

498) This. style. width = 498; ">
Figure 2: Overall mmapi Structure

3. Use mmapi

The classes and interfaces provided by mmapi are in javax. microedition. Media.ProgramWhen using mmapi, you should first reference these packages, otherwise the program cannot be compiled.

Each mmapi program needs to create a Player Object. We have already introduced that mmapi uses the createplayer function of manager to create a Player Object. This function has three versions, in the following format:

Public static player createplayer (string Locator)
Throws ioexception, mediaexception
Public static player createplayer (datasource source)
Throws ioexception, mediaexception
Public static player createplayer (inputstream stream, string type)
Throws ioexception, mediaexception

In the first version, a player object is created based on the protocol and data location specified by the URL string. The locator format is as follows:

Manager analyzes the URL string parameters provided by the createplayer function and creates a data source object, which transfers media data, and obtain the data content type of the media from the data. The manager creates the corresponding Player Object Based on the media data type. If the manager cannot determine the content type of the datasource, it will throw a mediaexception.

For example, create a player object that controls the MP3 audio of a website.

Player pmp3 = manager. createplayer ("http://www.XXX.com/111.mp3 ");

The second version creates a player object through a known datasource object.

In version 3, the player object is created through the inputstream stream.

We select the version to use to create the Player Object Based on the actual situation of the application. With a player object, you can use the method provided by the Player Object to control the media stream. The following describes common methods:

◆ Player. Start (): replay a media stream
◆ Player. Stop (): stops media streams.
◆ 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.

A state variable exists in each player object to indicate the lifecycle of the Player Object. When a player is created for the first time, it is in the unrealized state. When the media data location is set for the player, it is in the realized State (for example, when the player is downloading and interpreting data from an HTTP connection to a server or when the player has sent an HTTP request to the server, after receiving an HTTP response, and the datasource is ready to receive media data). When the player has read enough data and starts interpretation and computation, the player is in the prefetched state. After the computation, the player status changes to started. When we use the Player Object method to control the media stream, we should note that they may affect the change of the player status. You can use the getstate function to obtain the status of the current player. Figure 3 shows the State Transition of the Player Object.

498) This. style. width = 498; ">
Figure 3: Status transition

Iv. Methods for playing common media

1. Play single and sequential sounds

If you want to play a single sound once, you can use:

Manager. playtone (note, duration, volume );

Tonecontrol is required if you want to play the audio sequence. Use a special locator to create a Player Object, get tonecontrol, set its command sequence, and then start the player, such:

Player P = manager. createplayer (manger. tone_device_locator );
P. Realize ();
Tonecontrol Tc = (tonecontrol) (player. getcontrol ("tonecontrol "));
TC. setsequence (New byte [] {tonecontrol. C4, 8
Tonecontrol. C4 + 2,8 });
P. Start ();

2. play audio and Midi

Voice refers to the audio format such as WAV. in this format, data is a stream of various voice samples, which represents the fragments of the voice every second. MIDI is a series of commands used as a "virtual synthesizer" for multiple instruments ".

To play a sound file that can be accessed through HTTP, use:

Player P = manager. createplayer ("http://something.com/somefile.wav ");
P. Start ();

To play a sound file that has been placed in a jar file of the MIDlet, first understand its MIME type (such as "audio/X-WAV"), and then use:

Inputstream is = getclass (). getresourceasstream ("/somefile.wav ");
Player P = manager. createplayer (is, "audio/X-WAV ");
P. Start ();

To play a sound file in RMS, use:

Recordstore rs = recordstore. Open ("name ");
Byte [] DATA = Rs. getrecord (ID );
Bytearrayinputstream is = new bytearrayinputstream (data );
Player P = manager. createplayer (is, "audio/X-WAV ");
P. Start ();

3. Play a video

Playing a video is similar to playing an audio video. However, you need to tell the video player where to display the video signal. Therefore, you need to get a "video control" from the video player and then display the video content in form or canvas. The following is an example of displaying a video from a canvas:

Inputstream is = getclass (). getresourceasstream ("/somefile. Avi ");
Player P = manager. createplayer (is, "Video/Avi ");
P. Realize ();
Videocontrol Vc = (videocontrol) p. getcontrol ("videocontrol );
If (VC! = NULL)
{
VC. initdisplaymode (videocontrol. use_direct_video, CAV); // CAV is the canvas object.
VC. setvisible (true );
P. Start ();
}

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.