This control inherits from Surfaceview and plays video using MediaPlayer.
Small video Playback interface
Movieplayerview.java
Import java.io.IOException;
Import Android.content.Context;
Import Android.media.AudioManager;
Import Android.media.MediaPlayer;
Import Android.media.MediaPlayer.OnCompletionListener;
Import Android.util.AttributeSet;
Import Android.view.SurfaceHolder;
Import Android.view.SurfaceHolder.Callback;
Import Android.view.SurfaceView; /** * Video Playback control * @author Liuyinjun * @date 2015-2-8/public class Movieplayerview extends Surfaceview {private Sur
Faceholder Msurfaceholder;
Private MediaPlayer MPlayer;
Public Movieplayerview {This (context, NULL);
Public Movieplayerview (context, AttributeSet attrs) {This (context, attrs, 0);
Public Movieplayerview (context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle);
Msurfaceholder = This.getholder (); Msurfaceholder.addcallback (Msurfaceholdercallback); Holder join the callback interface Msurfaceholder.settype (surfaceholder.surface_type_push_buffers);//SETtype must be set, or there will be an error. /** * Play video * @param filename File path * * public void Plays (String fileName, final Onplaycompletionlistener C
Ompletionlistener) {MPlayer = new MediaPlayer ();
Mplayer.setaudiostreamtype (Audiomanager.stream_music); Mplayer.setdisplay (Msurfaceholder); Define a Surfaceview Play it Mplayer.setoncompletionlistener (new Oncompletionlistener () {@Override public vo
ID oncompletion (MediaPlayer arg0) {stop ();
if (Completionlistener!= null) completionlistener.onplaycompletion ();
Canvas.drawcolor (color.transparent,//PorterDuff.Mode.CLEAR);
}
});
try {mplayer.setdatasource (fileName);
Mplayer.prepare ();
catch (IllegalStateException e) {//TODO auto-generated catch block E.printstacktrace ();
catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();
} mplayer.start (); private void Stop () {if (MPlayer!= null)
{mplayer.release ();
MPlayer = null;
}/** * Free resources/public void release () {if (MPlayer!= null) {mplayer.release ();
MPlayer = null; } private Callback Msurfaceholdercallback = new Callback () {@Override public void surfacedestroyed (Su
Rfaceholder arg0) {msurfaceholder = null; @Override public void surfacecreated (Surfaceholder arg0) {//TODO auto-generated a stub Msur
Faceholder = arg0; @Override public void surfacechanged (Surfaceholder arg0, int arg1, int arg2, int arg3) {//TODO
auto-generated method Stub msurfaceholder = arg0;
}
}; /** * Playback Successful CALLBACK * @author Liuyinjun * @date 2015-2-9/public interface onplaycompletionlistener{Public
void Onplaycompletion (); }
}
The above is the entire contents of this article, I hope you can enjoy.