VideoActivity1

package com.media;


import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
public class VideoActivity1 extends Activity implements MediaPlayer.OnPreparedListener, SurfaceHolder.Callback{
 
   private TextView textView;
   private static final String TAG = "MediaDemo";
  
   //建立MediaPlayer对象
   private MediaPlayer mediaPlayer;
  
   //SurfaceView对象作为Layout上的输出接口
   private SurfaceView surfaceView;
  
   //以SurfaceHolder来控制SurfaceView
   private SurfaceHolder surfaceHolder;
  
   //4个ImageButton控制MediaPlayer事件
   private ImageButton play;
   private ImageButton pause;
   private ImageButton reset;
   private ImageButton stop;
  
   private Button button;
  
   //MediaPlayer暂停的flag
   private boolean isPaused = false;
  
   //MediaPlayer被释放的flag
   private boolean isReleased = false;
  
   //将视频文件存放在SD卡的路径
   private String strVideoPath = "";
  
   @Override
   public void onCreate(Bundle savedInstanceState){
     super.onCreate(savedInstanceState);
    
     //隐藏Activity的标题栏
     //必须加入到 setContentView(R.layout.video1)之前
     requestWindowFeature(Window.FEATURE_NO_TITLE);
     setContentView(R.layout.video1);
    
     if(!checkSDCard()){
       //提醒未安装SD存储卡
       createToast("没有发现安装的SD卡!",true);
     }
    
    
     textView = (TextView)findViewById(R.id.myTextView1);
    
     //绑定Layout的SurfaceView
     surfaceView = (SurfaceView) findViewById(R.id.mSurfaceView1);
    
     //设定SurfaceHolder为Layout SurfaceView全屏幕
     surfaceHolder = surfaceView.getHolder();
     surfaceHolder.addCallback(this);
    
     //设置影片的播放大小为176x144
     //如果取消  按照视频文件的原始大小播放
//     surfaceHolder.setFixedSize(176,144);
     surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    
     //4个ImageButton
     play = (ImageButton) findViewById(R.id.play);
     pause = (ImageButton) findViewById(R.id.pause);
     reset = (ImageButton) findViewById(R.id.reset);
     stop = (ImageButton) findViewById(R.id.stop);
     //设置全屏的按钮
     button=(Button)findViewById(R.id.myButton);
    
    
     //设定视频文件的位置
     strVideoPath = "/sdcard/maidou.3gp";
    
     //播放按钮
     play.setOnClickListener(new ImageButton.OnClickListener(){
  @Override
  public void onClick(View arg0) {
         if(checkSDCard()){
           playVideo(strVideoPath);
         }
  }
     });
    
     //暂停按钮
     pause.setOnClickListener(new ImageButton.OnClickListener(){
       public void onClick(View view){
         if(checkSDCard()){
           if (mediaPlayer != null){
             if(isReleased == false){
               if(isPaused==false){
                mediaPlayer.pause();
                isPaused = true;
                   textView.setText("影片暂停");
               }
               else if(isPaused==true){
                mediaPlayer.start();
                isPaused = false;
                   textView.setText("视频播放中");
               }
             }
           }
         }
       }
     });
    
     //重新播放按钮
     reset.setOnClickListener(new ImageButton.OnClickListener(){
       public void onClick(View view){
         if(checkSDCard()){
           if(isReleased == false){
             if (mediaPlayer != null){
              mediaPlayer.seekTo(0);
             }
           }
         }
       }
     });
    
     //停止按钮
     stop.setOnClickListener(new ImageButton.OnClickListener(){
       public void onClick(View view){
         if(checkSDCard()){
           if (mediaPlayer != null){
             if(isReleased==false){
              mediaPlayer.stop();
              mediaPlayer.release();
              isReleased = true;
                 textView.setText("播放器停止");
             }         
           }
         }
       }
     });
    
     //全屏按钮
     button.setOnClickListener(new OnClickListener(){
   @Override
   public void onClick(View arg0) {
            //设置全屏
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
            button.setText("还  原");
   }
      });
   }
  
   //自定义播放影片
   private void playVideo(String strPath){
    mediaPlayer = new MediaPlayer();
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    
       //设定Video影片以SurfaceHolder播放
    mediaPlayer.setDisplay(surfaceHolder); 
     try{
      mediaPlayer.setDataSource(strPath);
     }
     catch (Exception e){
      textView.setText("setDataSource Exceeption:"+e.toString());
     }
    
     try{
      mediaPlayer.prepare();
     }
     catch (Exception e){
      textView.setText("prepare Exceeption:"+e.toString());
     }
     mediaPlayer.start();
     isReleased = false;
     textView.setText("视频播放中");
    
     mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener(){
       @Override
       public void onCompletion(MediaPlayer arg0){
        mediaPlayer.stop();
             mediaPlayer.release();
             isReleased = true;
        textView.setText("播放器停止");
       }
     });
   }
  
   private boolean checkSDCard(){
     //检查SD卡是否安装
     if(android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)){
       return true;
     }
     else{
       return false;
     }
   }
  
   //创建Toast对象
   public void createToast(String str, boolean isLong){
     if(isLong==true){
       Toast.makeText(VideoActivity1.this, str, Toast.LENGTH_LONG).show();
     }
     else{
       Toast.makeText(VideoActivity1.this, str, Toast.LENGTH_SHORT).show();
     }
   }
  
   @Override
   public void surfaceChanged(SurfaceHolder surfaceholder, int format, int w, int h){
     Log.i(TAG, "Surface Changed");
   }
  
   @Override
   public void surfaceCreated(SurfaceHolder surfaceholder){
     Log.i(TAG, "Surface Created");
   }
  
   @Override
   public void surfaceDestroyed(SurfaceHolder surfaceholder){
     Log.i(TAG, "Surface Destroyed");
   }


 @Override
 public void onPrepared(MediaPlayer arg0) {
  Log.i(TAG, "onPrepared");
 }
}

你可能感兴趣的:(VideoActivity1)