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

Android--VideoPlay--視頻播放器

編輯:關於Android編程

代碼跟音樂播放器有大部分是相同的,各位可以將mp4格式的
視頻文件放到sdcard卡中,在輸入框中輸入文件名即可,在
模擬器上運行請使用android2.0版本,據說其他版本的模擬器
不支持視頻播放,如有錯誤之處請大家指出
 
java代碼如下:
 
package cn.mrzhu;
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 VideoPlayDemoActivity extends Activity {
 private EditText nameText;
 private String path;//文件路徑
 private MediaPlayer mediaPlayer;//media對象
 private SurfaceView surfaceView;
 private boolean pause;//暫停標記
 private int position;//播放的進度
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
 
  nameText = (EditText)findViewById(R.id.filename);
  surfaceView = (SurfaceView)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());
  mediaPlayer = new MediaPlayer();//實例化一個播放器對象
 }
 private final class SurfaceCallback implements Callback{
  public void surfaceChanged(SurfaceHolder holder, int format, int width,
    int height) {
  
  }
  /*
   * 當surfaceView被創建時
   */
  public void surfaceCreated(SurfaceHolder holder) {
   if(position > 0 && path != null){
    play(position);
    position = 0;
   }
  }
  /*
   * 當surfaceView被銷毀時
   */
 
  public void surfaceDestroyed(SurfaceHolder holder) {
   mediaPlayer.stop();
  }
 
 }
 
 
 /*
  * 按鈕事件,在main.xml中,每個按鈕的click_on = mediaplay
  */
 public void mediaplay(View v){
  switch(v.getId()){
 
  case R.id.playbutton://播放按鈕
   String filename = nameText.getText().toString();//獲取文件名
   File file = new File(Environment.getExternalStorageDirectory(), filename);
   if(file.exists()){
    path = file.getAbsolutePath();
    play(0);
   }else{
    //若路徑不存在,彈出Toast提示
    Toast.makeText(getApplicationContext(), R.string.filenoexsit, Toast.LENGTH_SHORT).show();
   }
   break;
  
  case R.id.pausebutton://暫停按鈕
   if(mediaPlayer.isPlaying()){
    mediaPlayer.pause();
    pause = true;
   }else{
    mediaPlayer.start();
    pause = false;
   }
   break;
  
  case R.id.resetbutton://重播按鈕
   if(mediaPlayer.isPlaying()){
    mediaPlayer.seekTo(0);
   }else{
    if(path != null){
     play(0);//將播放進度置為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) {
  }
 }
 
 private final class PrepareListener implements OnPreparedListener{
  private int position;
  public PrepareListener(int position) {
   super();
   this.position = position;
  }
  public void onPrepared(MediaPlayer mp) {
   mediaPlayer.start();//播放視頻
   if(position > 0) mediaPlayer.seekTo(position);
  }
 
 }
 @Override
 protected void onDestroy() {
  mediaPlayer.release();
  mediaPlayer = null;
  super.onDestroy();
 }
}
 
 
 
 
main.xml代碼如下:
 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="#FFFFFF" >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/filename" />
    <EditText
        android:id="@+id/filename"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/file"/>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
       
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/playbutton"
            android:onClick="mediaplay"
            android:text="@string/playbutton"
            />
       
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/pausebutton"
            android:onClick="mediaplay"
            android:text="@string/pausebutton"
            />
       
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/resetbutton"
            android:onClick="mediaplay"
            android:text="@string/resetbutton"
            />
       
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/stopbutton"
            android:onClick="mediaplay"
            android:text="@string/stopbutton"
            />
    </LinearLayout>
   
    <SurfaceView
       
        android:layout_width="fill_parent"
        android:layout_height="240dp"
        android:id="@+id/surfaceview"
        />
</LinearLayout>
 
 
 
string.xml代碼如下:
 
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="filenoexsit">視頻文件不存在</string>
    <string name="app_name">視頻播放器</string>
    <string name="filename">視頻文件名稱</string>
    <string name="playbutton">播放</string>
    <string name="pausebutton">暫停</string>
    <string name="resetbutton">重播</string>
    <string name="stopbutton">停止</string>
    <string name="file">OPPO.mp4</string>
</resources>
 
 
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved