package com.cn;
import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
/ **
* @ Description achieve their players
by SurfaceView / SurfaceHolder* @ Description here about the supplement, we can add OnPreparedListener
by mediaplayer* And OnCompletionListener and other events to play and ready to play after the completion of the operation control.
* When using SurfaceView and SurfaceHolder video playback, the structure is such that:
* 1, first of all, we get a surfaceView
from layout file* 2, to obtain the corresponding surfaceHolder
container think through surfaceView.getHolder () method* 3, srufaceHolder some default settings, such as addCallback () and setType ()
* 4 through mediaPlayer.setDisplay () method to play the video playback and container linked
* /
public class TestInternetActivity extends Activity {
MediaPlayer mediaPlayer; / / player's internal implementation is through MediaPlayer
SurfaceView surfaceView ;/ / video in a container
SurfaceHolder surfaceHolder ;/ / control surfaceView attributes (size, format, etc.) object
boolean isPause; / / if already suspended
@ Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.main);
surfaceView = (SurfaceView) findViewById (R.id.surfaceView1);
/ **
* Get the surefaceHolder
that is associated with the current surfaceView* /
surfaceHolder = surfaceView.getHolder ();
/ **
* Registration When surfaceView create, change, and should be performed when the destruction method
* /
surfaceHolder.addCallback (new SurfaceHolder.Callback () {
@ Override
public void surfaceDestroyed (SurfaceHolder holder) {
Log.i ("notice", "surfaceHolder been destroyed");
if (mediaPlayer! = null)
mediaPlayer.release ();
}
@ Override
public void surfaceCreated (SurfaceHolder holder) {
Log.i ("notice", "surfaceHolder is create a");
}
@ Override
public void surfaceChanged (SurfaceHolder holder, int format,
int width, int height) {
Log.i ("notice", "surfaceHolder was changed");
}
});
/ **
* This must be set to SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS oh, meaning
* Create a push in the 'surface', the main feature is not buffered
* /
surfaceHolder.setType (SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
/ ***
* @ Param targetButton
* User button is clicked
* /
public void buttonClick (View targetButton) {
int buttonId = targetButton.getId ();
switch (buttonId) {
case R.id.button_play:
play ();
break;
case R.id.button_pause:
pause ();
break;
case R.id.button_reset:
reset ();
break;
case R.id.button_stop:
stop ();
break;
default:
break;
}
}
/ **
* Play
* /
private void play () {
mediaPlayer = new MediaPlayer ();
/ / Set the multimedia stream type
mediaPlayer.setAudioStreamType (AudioManager.STREAM_MUSIC);
Container / / set for the show mediaPlayermediaPlayer.setDisplay (surfaceHolder);
try {
/ / MediaPlayer.setDataSource ("/ sdcard/001.mp4");
mediaPlayer
. SetDataSource ("http://192.168.1.114:8080/webdav/china.3gp");
mediaPlayer.prepareAsync ();
mediaPlayer.start ();
isPause = false;
} Catch (Exception e) {
/ / Log.i ("notice", "An error occurred during playback oh");
e.printStackTrace ();
}
}
/ **
* Pause
* /
private void pause () {
Log.i ("notice", "click on the pause button");
if (isPause == false) {
mediaPlayer.pause ();
isPause = true;
} Else {
mediaPlayer.start ();
isPause = false;
}
}
/ **
* Reset
* /
private void reset () {
Log.i ("notice", "click on the reset button");
/ / Jump to the beginning
videomediaPlayer.seekTo (0);
mediaPlayer.start ();
}
/ **
* Stop
* /
private void stop () {
Log.i ("notice", "click the stop button");
mediaPlayer.stop ();
mediaPlayer.release ();
}
}
Why when playing online video will be reported
05-10 03:14:44.555: ERROR / gralloc (52): [unregister] handle 0x274308 still locked (state = 40000001)
05-10 03:14:47.408: ERROR / MediaPlayer (2060): start called in state 4
05-10 03:14:47.408: ERROR / MediaPlayer (2060): error (-38, 0)
05-10 03:14:47.425: ERROR / MediaPlayer (2060): Error (-38,0)
05-10 03:14:48.095: WARN / MediaPlayer (2060): info / warning (1, 26)
05-10 03:14:48.107: INFO / MediaPlayer (2060): Info (1,26)
05-10 03:14:48.136: ERROR / PlayerDriver (31): Command PLAYER_INIT completed with an error or info PVMFFailure
05-10 03:14:48.136: ERROR / MediaPlayer (2060): error (1, -1)
05-10 03:14:48.146: ERROR / MediaPlayer (2060): Error (1, -1)
05-10 03:14:48.146: WARN / PlayerDriver (31): PVMFInfoErrorHandlingComplete
05-10 03:14:49.336: DEBUG / dalvikvm (225): GC freed 43 objects / 1968 bytes in 90ms
05-10 03:14:54.385: DEBUG / dalvikvm (106): GC freed 2514 objects / 142304 bytes in 132ms
Such nice? However, when playing local video can play normally? Hope to have experts to answer! ! ! !
Reply:
Network video is not http protocol is rtsp
This is a streaming media
You play your local files that are already playing
operationNetwork can not be so engaged
Reply:
Here I want to play online video should be how to get ah?
Tell me achieve program do? Thank you!
Reply:
This is the main thing that you need a server to store the network supports RTSP video resources
Client connections corresponding address on
http://blog.csdn.net/kepoon/article/details/6772060
Reply:
Reply:
1. Ensure that you can open http streaming media address on a computer browser
2 There http streaming and rtsp, setDataSource url can be directly transmitted, but time-consuming preparation stage of streaming media, you can not call the start function directly after prepare, need to meidplayer registered OnPreparedListener functions. In onPrepared function to call start function.
You also have the class to write the first comment there, but did not.
Reply:
Too, do not understand this stuff, learning
Reply:
i am studying your document
Reply:
mediaPlayer.setDataSource ("http://192.168.1.114:8080/webdav/china.3gp");
Here the usage right.
Should:
Uri uri = Uri.parse ("http://192.168.1.114:8080/webdav/china.3gp");
mediaPlayer.setDataSource (this, uri);
No comments:
Post a Comment