1. 程式人生 > >Android的播放視訊功能

Android的播放視訊功能

複製程式碼
public class VodeoPlayActivity extends Activity {
    /** Called when the activity is first created. */
    private EditText filenamEditText;
    private MediaPlayer mediaPlayer;
    private String filename;
    private SurfaceView surfaceView;
    private final static String TAG="VodeoPlayActivity";
    private
int prosition=0; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); filenamEditText=(EditText) this.findViewById(R.id.filename); surfaceView=(SurfaceView)this.findViewById(R.id.surfaceview); surfaceView.getHolder().setFixedSize(176, 144);//
設定解析度 surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);//設定surfaceview不維護自己的緩衝區,而是等待螢幕的渲染引擎將內容推送到使用者面前 surfaceView.getHolder().addCallback(new SurceCallBack());//對surface物件的狀態進行監聽 mediaPlayer=new MediaPlayer(); ButtonOnClikListiner buttonOnClikListinero=new
ButtonOnClikListiner(); ImageButton start=(ImageButton) this.findViewById(R.id.play); ImageButton pause=(ImageButton) this.findViewById(R.id.pause); ImageButton stop=(ImageButton) this.findViewById(R.id.stop); ImageButton replay=(ImageButton) this.findViewById(R.id.reset); start.setOnClickListener(buttonOnClikListinero); pause.setOnClickListener(buttonOnClikListinero); stop.setOnClickListener(buttonOnClikListinero); replay.setOnClickListener(buttonOnClikListinero); } private final class ButtonOnClikListiner implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            if(Environment.getExternalStorageState()==Environment.MEDIA_UNMOUNTED){
                Toast.makeText(VodeoPlayActivity.this, "sd卡不存在", Toast.LENGTH_SHORT).show();
                return;
            }
            filename=filenamEditText.getText().toString();
            switch (v.getId()) {
            case R.id.play:
                play();
                break;
            case R.id.pause:
                if(mediaPlayer.isPlaying()){
                    mediaPlayer.pause();
                }else{
                    mediaPlayer.start();
                }
                break;
            case R.id.reset:
                if(mediaPlayer.isPlaying()){
                    mediaPlayer.seekTo(0);
                }else{
                    play();
                }
                break;
            case R.id.stop:
                if(mediaPlayer.isPlaying()){
                    mediaPlayer.stop();
                }
                break;
            }
        }  
    }
    
    private void play() {
        try {
                File file=new File(Environment.getExternalStorageDirectory(),filename);
                mediaPlayer.reset();//重置為初始狀態                mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);//設定音樂流的型別                mediaPlayer.setDisplay(surfaceView.getHolder());//設定video影片以surfaceviewholder播放                mediaPlayer.setDataSource(file.getAbsolutePath());//設定路徑                mediaPlayer.prepare();//緩衝                mediaPlayer.start();//播放            } catch (Exception e) {
                Log.e(TAG, e.toString());
                e.printStackTrace();
            }
    }
    
    private final class SurceCallBack implements SurfaceHolder.Callback{
        /**
         * 畫面修改
         */
        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width,
                int height) {
            // TODO Auto-generated method stub            
        }

        /**
         * 畫面建立
         */
        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            if(prosition>0&&filename!=null){
                play();
                mediaPlayer.seekTo(prosition);
                prosition=0;
            }
            
        }

        /**
         * 畫面銷燬
         */
        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            if(mediaPlayer.isPlaying()){
                prosition=mediaPlayer.getCurrentPosition();
                mediaPlayer.stop();
            }
        }
    }
}
複製程式碼

VideoView

在main.xml中加入:

<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        android:layout_width="fill_parent" android:layout_height="fill_parent"> 
        <VideoView android:id="@+id/videoView" android:layout_width="fill_parent" 
            android:layout_height="fill_parent" android:layout_x="10px" 
            android:layout_y="10px" /> 
    </AbsoluteLayout>

在類中,加入以下程式碼

super.onCreate(savedInstanceState); 
        requestWindowFeature(Window.FEATURE_NO_TITLE); 
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//設定成全屏模式 
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//強制為橫屏 
        setContentView(R.layout.main); 
       videoView = (VideoView) findViewById(R.id.videoView); 
//       videoView.setVideoPath("/sdcard/xyx.3gp"); 
       videoView.setVideoURI(Uri.parse("/sdcard/beijinghuanyingni.mp4")); 
       MediaController mediaController = new MediaController(this); 
       videoView.setMediaController(mediaController); 
       videoView.start(); 
       //videoView.requestFocus();

首先我使用的是全屏,強制橫屏播放視訊。.setVideoURI是設定視訊的路徑,這裡我用的是sdcard上的視訊,如果用http的,則需寫完整路徑。最重要的一點,我看網上有用videoView.requestFocus();來載入視屏播放,這裡我沒有能播放成功,改用了videoView.start();來播放。 
這種方式是使用Android自帶的按鈕,無需自定義暫停、播放等按鈕控制元件。