This article describes the simple media player functions implemented by Android development. Share it for your reference, as follows:
1. Overview:
Because playing video requires a lot of memory, surfaceview must be used.
The surfaceview implements the double buffering function.
2. All codes:
/** * @Description Create video using surfaceview * @Project name App_Basic * @Bank Name * @Class Name MediaPlayerActivity * @author chenlin * @date 2013-3-25 7:36:32 AM * @version 1.0 */ public class MediaPlayerActivity extends BaseActivity { private MediaPlayer mPlayer; private Button mBtnPlay; private Button mBtnPause; private SurfaceView mSurfaceView; private int mCurrentPosition; private SeekBar seekBar; private boolean isPlaying; @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); mSurfaceView = new SurfaceView(this); seekBar = new SeekBar(this); (new OnSeekBarChangeListener() { @Override public void onStopTrackingTouch(SeekBar seekBar) { int progress = (); if (mPlayer != null && isPlaying) { (progress); } } @Override public void onStartTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub } @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { // TODO Auto-generated method stub } }); ().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); ().addCallback(new Callback() { @Override public void surfaceDestroyed(SurfaceHolder holder) { if (mPlayer!= null && ()) { mCurrentPosition = (); stop(); } } @Override public void surfaceCreated(SurfaceHolder holder) { if (mCurrentPosition > 0) { play(mCurrentPosition); } } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { // TODO Auto-generated method stub } }); } public void play(final int currentPosition){ try { mPlayer = new MediaPlayer(); (AudioManager.STREAM_MUSIC); Uri myUri = (new File("sdcard/mp3/1.mp3")); (getApplicationContext(), myUri); (true); (()); (); //(); (false); (new OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { (true); (); } }); (new OnErrorListener() { @Override public boolean onError(MediaPlayer mp, int what, int extra) { (true); (); isPlaying = false; return false; } }); (new OnPreparedListener() { public void onPrepared(MediaPlayer mp) { (); (currentPosition); (()); new Thread(new Runnable() { @Override public void run() { isPlaying = true; while(isPlaying){ (()); (500); } } }); } }); } catch (Exception e) { (); } } public void pause(){ if ("Continuation".equals(().toString())) { ("pause"); (); return; } if (mPlayer != null && ()) { (); ("continue"); return; } } public void reset(){ if (mPlayer != null && ()) { (0); return; } play(0); } public void stop(){ if (mPlayer != null && ()) { (); (); mPlayer = null; (true); isPlaying = false; } } }
For more information about Android related content, please check out the topic of this site:Android multimedia operation skills summary (audio, video, recording, etc.)》、《Android development introduction and advanced tutorial》、《Android View View Tips Summary》、《Android programming activity operation skills summary》、《Android file operation skills summary》、《Android resource operation skills summary"and"Android control usage summary》
I hope this article will be helpful to everyone's Android programming design.