Android ApiDemos example resolution (87): Media-& gt; MediaPlayer

Source: Internet
Author: User

This example describes how to use the MediaPlayer class to play a sound or video. Three activities are involved:

MediaPlayerDemo: Main Activity. The sample list is displayed.
MediaPlayerDemo_Audio: A sub-Activity used to play a sound. There is no icon in the Launcher.
MediaPlayerDemo_Video: A subactivity used to play a video. It has no icons in Launcher.
MediaPlayerDemo uses four buttons to display the example list:

 

Click the button to trigger the sub-Activity. For example, "Play video from local file" will start MediaPlayerDemo_Audio.

[Java]
Intent intent =
New Intent (MediaPlayerDemo. this. getApplication (),
MediaPlayerDemo_Audio.class );
Intent. putExtra (MEDIA, LOCAL_AUDIO );
StartActivity (intent );

Intent intent =
New Intent (MediaPlayerDemo. this. getApplication (),
MediaPlayerDemo_Audio.class );
Intent. putExtra (MEDIA, LOCAL_AUDIO );
StartActivity (intent); to run this example, you need to prepare sound and video resources. For videos, you can use the recording program that comes with your mobile phone to record a video.


Modify the Path assignment in MediaPlayerDemo_Audio.java and MediaPlayerDemo_Video.java, for example, in case STREAM_VIDEO: Of MediaPlayerDemo_Video.java


Path = "http://www.imobilebbs.com/download/android/boy.3gp ";


Specifies the address for online video playback from a location on the Internet. You can also set the file name for other sounds or video pairs. If you put the video on the SD card, the file name can be: /sd card/dcim/Camera/demo.3gp ".


With these preparations, it is very easy to use MediaPlayer to play sound or video (similar to the Player in Java Me). MediaPlayer supports playing from files or streams. MediaPlayer can be in multiple States, and the functions that can be called in each State are different. The status transition diagram of MediaPlayer is as follows:

 


Corresponds to state transition, MediaPlayer allows Listener to monitor its state, see http://developer.android.com/reference/android/media/MediaPlayer.html for details


The following describes how to use MediaPlayer in combination with this example:


Play local files: sets DataSource to point to a local file.


[Java]
Path = "/sd card/audio/test.pdf ";
MMediaPlayer = new MediaPlayer ();
MMediaPlayer. setDataSource (path );
MMediaPlayer. prepare ();
MMediaPlayer. start ();

Path = "/sd card/audio/test.pdf ";
MMediaPlayer = new MediaPlayer ();
MMediaPlayer. setDataSource (path );
MMediaPlayer. prepare ();
MMediaPlayer. start (); play the sound or video in the resource file:


[Java]
MMediaPlayer = MediaPlayer. create (this, R. raw. test_cbr );
MMediaPlayer. start ();

MMediaPlayer = MediaPlayer. create (this, R. raw. test_cbr );
MMediaPlayer. start ();
Play a video locally or online:

[Java]
Path = "/sd card/dcim/camera/test.3gp ";
// Path = "http://www.imobilebbs.com/download/android/boy.3gp ";
// Create a new media player and set the listeners
MMediaPlayer = new MediaPlayer ();
MMediaPlayer. setDataSource (path );
MMediaPlayer. setDisplay (holder );
MMediaPlayer. prepare ();
MMediaPlayer. setOnBufferingUpdateListener (this );
MMediaPlayer. setOnCompletionListener (this );
MMediaPlayer. setOnPreparedListener (this );
MMediaPlayer. setOnVideoSizeChangedListener (this );
MMediaPlayer. setAudioStreamType (AudioManager. STREAM_MUSIC );

Path = "/sd card/dcim/camera/test.3gp ";
// Path = "http://www.imobilebbs.com/download/android/boy.3gp
// Create a new media player and set the listeners
MMediaPlayer = new MediaPlayer ();
MMediaPlayer. setDataSource (path );
MMediaPlayer. setDisplay (holder );
MMediaPlayer. prepare ();
MMediaPlayer. setOnBufferingUpdateListener (this );
MMediaPlayer. setOnCompletionListener (this );
MMediaPlayer. setOnPreparedListener (this );
MMediaPlayer. setOnVideoSizeChangedListener (this );
MMediaPlayer. setAudioStreamType (AudioManager. STREAM_MUSIC );
 

Path can point to local files or network resources, and can set Media event processors. You must use SurfaceView to display the image when playing a video on the MediaPlayer. For details, see Android ApiDemos example resolution (57): Graphics-> CameraPreview.


 

 
Author: mapdigit

 

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.