您好,登錄后才能下訂單哦!
本文實例講述了Android開發實現的簡單媒體播放器功能。分享給大家供大家參考,具體如下:
一、概述:
因為播放視頻需要很大的內存,所以必須使用surfaceview ,
surfaceview 里實現了雙緩沖的功能,
二、全部代碼:
/** * @描述 使用surfaceview 創建視頻 * @項目名稱 App_Basic * @包名 com.example.basic.media * @類名 MediaPlayerActivity * @author chenlin * @date 2013-3-25 上午7:36:32 * @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) { super.onCreate(savedInstanceState); mSurfaceView = new SurfaceView(this); seekBar = new SeekBar(this); seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { @Override public void onStopTrackingTouch(SeekBar seekBar) { int progress = seekBar.getProgress(); if (mPlayer != null && isPlaying) { mPlayer.seekTo(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 } }); mSurfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); mSurfaceView.getHolder().addCallback(new Callback() { @Override public void surfaceDestroyed(SurfaceHolder holder) { if (mPlayer!= null && mPlayer.isPlaying()) { mCurrentPosition = mPlayer.getCurrentPosition(); 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(); mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); Uri myUri = Uri.fromFile(new File("sdcard/mp3/1.mp3")); mPlayer.setDataSource(getApplicationContext(), myUri); mPlayer.setLooping(true); mPlayer.setDisplay(mSurfaceView.getHolder()); mPlayer.prepareAsync(); //mPlayer.prepare(); mBtnPlay.setEnabled(false); mPlayer.setOnCompletionListener(new OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { mBtnPlay.setEnabled(true); mPlayer.release(); } }); mPlayer.setOnErrorListener(new OnErrorListener() { @Override public boolean onError(MediaPlayer mp, int what, int extra) { mBtnPlay.setEnabled(true); mPlayer.release(); isPlaying = false; return false; } }); mPlayer.setOnPreparedListener(new OnPreparedListener() { public void onPrepared(MediaPlayer mp) { mPlayer.start(); mPlayer.seekTo(currentPosition); seekBar.setMax(mPlayer.getDuration()); new Thread(new Runnable() { @Override public void run() { isPlaying = true; while(isPlaying){ seekBar.setProgress(mPlayer.getCurrentPosition()); SystemClock.sleep(500); } } }); } }); } catch (Exception e) { e.printStackTrace(); } } public void pause(){ if ("接續".equals(mBtnPause.getText().toString())) { mBtnPause.setText("暫停"); mPlayer.start(); return; } if (mPlayer != null && mPlayer.isPlaying()) { mPlayer.pause(); mBtnPause.setText("繼續"); return; } } public void reset(){ if (mPlayer != null && mPlayer.isPlaying()) { mPlayer.seekTo(0); return; } play(0); } public void stop(){ if (mPlayer != null && mPlayer.isPlaying()) { mPlayer.stop(); mPlayer.release(); mPlayer = null; mBtnPlay.setEnabled(true); isPlaying = false; } } }
更多關于Android相關內容感興趣的讀者可查看本站專題:《Android多媒體操作技巧匯總(音頻,視頻,錄音等)》、《Android開發入門與進階教程》、《Android視圖View技巧總結》、《Android編程之activity操作技巧總結》、《Android文件操作技巧匯總》、《Android資源操作技巧匯總》及《Android控件用法總結》
希望本文所述對大家Android程序設計有所幫助。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。