Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android視頻播放器關鍵代碼

android視頻播放器關鍵代碼

編輯:關於Android編程

package cn.itcast.videoplayer;

import java.io.File;

import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.os.Environment;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
    private EditText nameText;
    private String path;
    private MediaPlayer mediaPlayer;
    private SurfaceView surfaceView;
    private boolean pause;
    private int position;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        mediaPlayer = new MediaPlayer();
        nameText = (EditText) this.findViewById(R.id.filename);
        surfaceView = (SurfaceView) this.findViewById(R.id.surfaceView);
        //把輸送給surfaceView的視頻畫面,直接顯示到屏幕上,不要維持它自身的緩沖區
        surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        surfaceView.getHolder().setFixedSize(176, 144);
        surfaceView.getHolder().setKeepScreenOn(true);
        surfaceView.getHolder().addCallback(new SurfaceCallback());
    }
    
    private final class SurfaceCallback implements Callback{
		public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
		}
		public void surfaceCreated(SurfaceHolder holder) {
			if(position>0 && path!=null){
				play(position);
				position = 0;
			}
		}
		public void surfaceDestroyed(SurfaceHolder holder) {
			if(mediaPlayer.isPlaying()){
				position = mediaPlayer.getCurrentPosition();
				mediaPlayer.stop();
			}
		}
    }

	@Override
	protected void onDestroy() {
    	mediaPlayer.release();
    	mediaPlayer = null;
		super.onDestroy();
	}

	public void mediaplay(View v){
    	switch (v.getId()) {
		case R.id.playbutton:
			String filename = nameText.getText().toString();
			if(filename.startsWith("http")){
				path = filename;
				play(0);
			}else{
				File file = new File(Environment.getExternalStorageDirectory(), filename);
				if(file.exists()){
					path = file.getAbsolutePath();
					play(0);
				}else{
					path = null;
					Toast.makeText(this, R.string.filenoexsit, 1).show();
				}
			}
		
			break;

		case R.id.pausebutton:
			if(mediaPlayer.isPlaying()){
				mediaPlayer.pause();
				pause = true;
			}else{
				if(pause){
					mediaPlayer.start();
					pause = false;
				}
			}
			break;
			
		case R.id.resetbutton:
			if(mediaPlayer.isPlaying()){
				mediaPlayer.seekTo(0);
			}else{
				if(path!=null){
					play(0);
				}
			}
			break;
		case R.id.stopbutton:
			if(mediaPlayer.isPlaying()){
				mediaPlayer.stop();
			}
			break;
		}
    }

	private void play(int position) {
		try {
			mediaPlayer.reset();
			mediaPlayer.setDataSource(path);
			mediaPlayer.setDisplay(surfaceView.getHolder());
			mediaPlayer.prepare();//緩沖
			mediaPlayer.setOnPreparedListener(new PrepareListener(position));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private final class PrepareListener implements OnPreparedListener{
		private int position;
		
		public PrepareListener(int position) {
		     this.position = position;
		}

		public void onPrepared(MediaPlayer mp) {
			mediaPlayer.start();//播放視頻
			if(position>0) mediaPlayer.seekTo(position);
		}
	}
}

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved