ANDROID 视频播放代码

activity_main.xml

 

<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="fill_parent"
    android:layout_height="360px"
    />

<LinearLayout android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    >
   
   <Button android:id="@+id/start"
       android:text="开始"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       />
  
   <Button android:id="@+id/pause"
       android:text="暂停"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       />
  
   <Button android:id="@+id/stop"
       android:text="停止"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       />
   
   
</LinearLayout>

</LinearLayout>

 

 

MainActivity.java


import java.io.IOException;

import javax.security.auth.callback.Callback;

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.support.v4.app.NavUtils;

public class MainActivity extends Activity implements OnClickListener{

 private Button buttonStart;
 private Button buttonPause;
 private Button buttonStop;
 SurfaceView surfaceView;
 MediaPlayer mediaPlayer;
 int position;
 
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
        this.buttonStart = (Button) this.findViewById(R.id.start);
        this.buttonPause = (Button) this.findViewById(R.id.pause);
        this.buttonStop  = (Button) this.findViewById(R.id.stop);
        this.buttonStart.setOnClickListener(this);
        this.buttonPause.setOnClickListener(this);
        this.buttonStop.setOnClickListener(this);
       
       
       
        this.mediaPlayer = new MediaPlayer();
        this.surfaceView = (SurfaceView) this.findViewById(R.id.surfaceView);
        //设置SurfaceView自己不管理的缓冲区
        this.surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        this.surfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
   
   @Override
   public void surfaceDestroyed(SurfaceHolder holder) {
    // TODO Auto-generated method stub
    
   }
   
   @Override
   public void surfaceCreated(SurfaceHolder holder) {
    // TODO Auto-generated method stub
    if(position>0){
     try {
      play();
      //并直接从指定位置开始播放
     } catch (IllegalArgumentException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     } catch (IllegalStateException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }
     mediaPlayer.seekTo(position);
     position=0;
     
    }
   }
   
   @Override
   public void surfaceChanged(SurfaceHolder holder, int format, int width,
     int height) {
    // TODO Auto-generated method stub
    
   }
  });

       
    }


 @Override
 public void onClick(View arg0) {
  // TODO Auto-generated method stub
  switch(arg0.getId()){
  case R.id.start:
   try {
    play();
   } catch (IllegalArgumentException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } catch (IllegalStateException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   break;
   
  case R.id.pause:
   if(mediaPlayer.isPlaying()){
    mediaPlayer.pause();
   }else{
    mediaPlayer.start();
   }
   break;
   
  case R.id.stop:
   if(mediaPlayer.isPlaying()){
    mediaPlayer.stop();
   }
   
   break;
   
  default:
   break;
  
  } 
 }


 @Override
 protected void onPause() {
  // TODO Auto-generated method stub
  if(mediaPlayer.isPlaying()){
   this.position = this.mediaPlayer.getCurrentPosition();
   this.mediaPlayer.stop();
  }
  super.onPause();
 }

 void play() throws IllegalArgumentException, IllegalStateException, IOException{
  
  this.mediaPlayer.reset();
  this.mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
  
  this.mediaPlayer.setDataSource("/mnt/sdcard/DCIM/Camera/123.3gp");
  
  //把视频画面输出到SurfaceView
  this.mediaPlayer.setDisplay(this.surfaceView.getHolder());
  
  this.mediaPlayer.prepare();
  this.mediaPlayer.start();
  
 }
 
 
}


 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值