1. 程式人生 > >Android 播放MP4檔案

Android 播放MP4檔案

首先,要在AndroidManifest.xml檔案中新增相應的許可權:

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

第二步,佈局檔案:

<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
android:layout_height="match_parent" android:orientation="vertical">
<SurfaceView android:id="@+id/surfaceView" android:layout_width="match_parent" android:layout_height="360dp" /> <LinearLayout android:layout_width="fill_parent" android:layout_height
="wrap_content" android:gravity="center_horizontal" android:orientation="horizontal" >
<ImageButton android:id="@+id/btnplay" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src
="@drawable/play" />
<ImageButton android:id="@+id/btnpause" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/pause" /> <ImageButton android:id="@+id/btnstop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/stop" /> </LinearLayout> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" /> </LinearLayout >

第三步,主程式碼部分:

package com.example.teltplay;

import java.io.File;
import java.io.IOException;

import android.app.Activity;
import android.content.res.AssetFileDescriptor;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.media.MediaPlayer.OnSeekCompleteListener;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;


public class MainActivity extends Activity implements OnClickListener{
     private ImageButton btnplay, btnstop, btnpause;  
     private SurfaceView surfaceView;  
     private MediaPlayer mediaPlayer;
     private int position;  
     TextView textView1;
     private String filename = Environment.getExternalStorageDirectory().getPath()+"/TestLog/test.mp4";;

     public void onCreate(Bundle savedInstanceState) {  
         super.onCreate(savedInstanceState);  
         setContentView(R.layout.main);    
         btnplay=(ImageButton)this.findViewById(R.id.btnplay);  
         btnstop=(ImageButton)this.findViewById(R.id.btnstop);  
         btnpause=(ImageButton)this.findViewById(R.id.btnpause);  
         textView1=(TextView)this.findViewById(R.id.textView1);

         btnstop.setOnClickListener(this);  
         btnplay.setOnClickListener(this);  
         btnpause.setOnClickListener(this);  

         mediaPlayer=new MediaPlayer();  
         surfaceView=(SurfaceView) this.findViewById(R.id.surfaceView);  

         mediaPlayer.setOnCompletionListener(new OnCompletionListener() {

                @Override
                public void onCompletion(MediaPlayer mp) {
                    textView1.setText("播放完畢!...");
                }
         });
         mediaPlayer.setOnErrorListener(new OnErrorListener() {

                @Override
                public boolean onError(MediaPlayer mp, int what, int extra) {
                    textView1.setText("播放出錯!...");
                    return false;
                }
         });
         mediaPlayer.setOnPreparedListener(new OnPreparedListener() {

                @Override
                public void onPrepared(MediaPlayer mp) {
                    textView1.setText("準備就緒!...");
                }
         });
         mediaPlayer.setOnSeekCompleteListener(new OnSeekCompleteListener() {

                @Override
                public void onSeekComplete(MediaPlayer mp) {
                    textView1.setText("進度拖放完畢!...");
                }
         });


         //設定SurfaceView自己不管理的緩衝區  
         surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);         
         surfaceView.getHolder().addCallback(new Callback() {
             @Override  
             public void surfaceDestroyed(SurfaceHolder holder) {

             }  

             @Override  
             public void surfaceCreated(SurfaceHolder holder) {
                 if (position>0) {
                     try {
                         //開始播放  
                         play();  
                         //並直接從指定位置開始播放  
                         mediaPlayer.seekTo(position);  
                         position=0;
                     } catch (Exception e) {  
                            // TODO: handle exception  
                     }  
                 }  
             }             
             @Override  
             public void surfaceChanged(SurfaceHolder holder, int format, int width,
                     int height) {  
             }  
        });       
     }  
     @Override  
     public void onClick(View v) {
         switch (v.getId()) {
         case R.id.btnplay:
             play();  
             break;      
         case R.id.btnpause:  
             if (mediaPlayer.isPlaying()) {  
                 mediaPlayer.pause();  
             }else{  
                 mediaPlayer.start();  
             }  
             break;  
         case R.id.btnstop:  
             if (mediaPlayer.isPlaying()) {  
                 mediaPlayer.stop();  
             }
             break;
         default:
             break;  
            }  
        }  
     @Override  
     protected void onPause() {
         //先判斷是否正在播放  
         if (mediaPlayer.isPlaying()) {  
             //如果正在播放我們就先儲存這個播放位置  
             position=mediaPlayer.getCurrentPosition();  
             mediaPlayer.stop();  
         }
         super.onPause();
     }

     private void play() {
         try {  
            mediaPlayer.reset();
            mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);  
            //設定需要播放的視訊  
            mediaPlayer.setDataSource(filename);  
            //把視訊畫面輸出到SurfaceView  
            mediaPlayer.setDisplay(surfaceView.getHolder());  
            mediaPlayer.prepare();  
            //播放  
            mediaPlayer.start();                  
         } catch (Exception e) {  
             // TODO: handle exception   
         } 
     }  

}