[Android, 16] android Video Player

Source: Internet
Author: User

Video Player Development:

 

1. SurfaceView:

1. Display in unit time.

2. Internal dual-buffer mechanism:

3. directly operate the video memory of the video card, which is faster.

 

 

2. Specific steps for video playback development:

1. layout interface:

 

Android: layout_width = "fill_parent"

>

Android: orientation = "vertical">

 

Android: layout_width = "fill_parent"

Android: layout_height = "wrap_content"

>

Android: textSize = "20sp"/>

 

Android: id = "@ + id/et_path"

Android: layout_width = "fill_parent"

Android: layout_height = "wrap_content"

>

 

 

 

Android: layout_width = "fill_parent"

Android: layout_height = "wrap_content"

Android: gravity = "center_horizontal"

Android: orientation = "horizontal">

 

Android: id = "@ + id/play"

Android: layout_width = "0dip"

>

Android: layout_weight = "1"

Android: text = "play"/>

 

Android: id = "@ + id/pause"

Android: layout_width = "0dip"

Android: layout_height = "wrap_content"

>

Android: text = "paused"/>

 

Android: id = "@ + id/replay"

Android: layout_width = "0dip"

Android: layout_height = "wrap_content"

Android: layout_weight = "1"

>

 

Android: id = "@ + id/stop"

Android: layout_width = "0dip"

Android: layout_height = "wrap_content"

Android: layout_weight = "1"

Android: text = "stop"/>

 

// Controls for displaying videos:

Android: id = "@ + id/sv"

Android: layout_width = "fill_parent"

Android: layout_height = "fill_parent"/>

 

 

2. Video Player operations:

① Playback:
 

MediaPlayer>

MediaPlayer. setAudioStreamType (AudioManager. STREAM_MUSIC );

// Obtain the SurfaceView object:

SurfaceView>

// Obtain the SurfaceHolder object through SurfaceView

SurfaceHolder>

// Specify the control that displays the video to be played.

MediaPlayer. setDisplay (holder );

// Set the playback resource path, for example,/sdcard/oppo.3gp

MediaPlayer. setDataSource (path );

// Initialize the player:

Method 1: Synchronous:

MediaPlayer. prepare ();

Method 2: asynchronous:

MediaPlayer. prepareAsync (); // asynchronous preparation

// Set the listener for asynchronous preparation. After asynchronous preparation is complete, call the following method:

MediaPlayer. setOnPreparedListener (new OnPreparedListener (){

 

Public>

>

>

>

 

>

>

}

 

③ Stop:

Private>

// Obtain the current playback position

>

>

>

>

// Obtain the SurfaceHolder object through SurfaceView

SurfaceHolder>

// Specify the control that displays the video to be played.

MediaPlayer. setDisplay (holder );

②. Solution: Set the callback function for the SurfaceHolder object:

Surfaceview>

// Set the callback method of SurfaceHolder;

Sv. getHolder (). addCallback (new Callback (){

 

// Call this method when SurfaceHolder is destroyed:

>

Mediaplater. reset (); // return to the Idle status

 

 

2. Use setDataSource (); To set the playback data source

 

3. The underlying c code initializes the player.

Method 1: mediaplayer. prepare ();

Synchronization method.

If no preparation is complete, the following code will not be executed

Mediaplayer. start ();

 

Method 2: mediaplayer. prepareAsync ();

Asynchronous Method,

To know when to register the onPreparedListener listener after preparation, the internal onPrepared () method is called:

Play the video in the onPrepared () method.

 

5. Play online video files:

1. If we want to play a Streaming Media Resource online:

The requirement must be a resource file that supports progressive download, that is, the video is divided into several frames of resources, each frame is a picture that can be independently played.

2. resources that support progressive download:

3gp supports http: // protocol download

Rtsp: //>

 

 

 

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.