视频播放Demo

话不多说,直接上代码:xml文件:

 <TextView
        android:id="@+id/file"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="文件名" />
    <EditText
        android:id="@+id/fileedite"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         />
    </LinearLayout>
    
       <LinearLayout 
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:orientation="horizontal"
      >
    <Button
        android:id="@+id/play"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="mediaPlay"
        />
    <Button
        android:id="@+id/pause"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="mediaPlay"
        />
    <Button
        android:id="@+id/reset"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="mediaPlay"
       />
    <Button
        android:id="@+id/stop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="mediaPlay"
        >
        </Button>
        </LinearLayout>
        
       <SurfaceView
        android:layout_width="fill_parent"
        android:layout_height="240dp"
        android:id="@+id/surfaceView"
           />
View Code

activity:

    private EditText nameEdit;
    private MediaPlayer media;
    private String path;
    private SurfaceView surfaceView;
    private Boolean pause=false;
    private int position;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_media_play);
        
        media=new MediaPlayer();
        nameEdit=(EditText) findViewById(R.id.fileedite);
        //把输送给surfaceView的视频画面,直接显示到屏幕上,不要在自己的缓冲区
        surfaceView=(SurfaceView) findViewById(R.id.surfaceView);
        surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        surfaceView.getHolder().setFixedSize(176, 144);
        surfaceView.getHolder().setKeepScreenOn(true);
        surfaceView.getHolder().addCallback(new SurfaceCallBack());
        
    }
    
    public final class SurfaceCallBack implements Callback{

        
        public void surfaceCreated(SurfaceHolder holder) {
            // TODO Auto-generated method stub
            if(position>0 && path!=null){
                play(position);
                position=0;
            }
        }

        
        public void surfaceChanged(SurfaceHolder holder, int format, int width,
                int height) {
            // TODO Auto-generated method stub
            
        }

        
        public void surfaceDestroyed(SurfaceHolder holder) {
            // TODO Auto-generated method stub
            if(media.isPlaying()){
                position=media.getCurrentPosition();
                media.stop();
            }
            
        }

    
    }
    private void mediaPlay(View v){
        switch(v.getId()){
        case R.id.play:
            String filename=nameEdit.getText().toString();
            if(filename.startsWith("http")){
                path=filename;
                play(0);
            }else{
                File file=new File(Environment.getExternalStorageDirectory(),filename);
                
                if(file.exists()){
                    path=file.getAbsolutePath();
                    play(0);
                }else{
                    path=null;
                    Toast.makeText(this, "视频文件不存在", 1).show();
                    }
            }
            
            break;
        case R.id.pause:
            if(media.isPlaying()){
                media.pause();
                pause=true;
            }else{
                if(pause){
                    media.start();
                    pause=false;
                }
            }
            break;
        case R.id.reset:
            if(media.isPlaying()){
                media.seekTo(0);
            }else{
                if(path!=null){
                    play(0);
                }
            }
            break;
        case R.id.stop:
            if(media.isPlaying()){
                media.stop();
            }
            break;
        
            default:
                break;
    
        
        }
    }
    

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        media.release();
        media=null;
        super.onDestroy();
    }
    private void play(int position){
        media.reset();
        try {
            media.setDataSource(path);
            media.setDisplay(surfaceView.getHolder());
            media.prepare();//缓冲
            media.setOnPreparedListener(new PreparedListener(position));
            
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SecurityException 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();
        }
        
        
    }
    
    private final class PreparedListener implements OnPreparedListener{

        private int position;
        
        public PreparedListener(int position){
            this.position=position;
            
        }
        @Override
        public void onPrepared(MediaPlayer mp) {
            // TODO Auto-generated method stub
            media.start();
            if(position>0 && path!=null){
            media.seekTo(position);
            }
        }
        
View Code
原文地址:https://www.cnblogs.com/wei1228565493/p/4187050.html