zoukankan      html  css  js  c++  java
  • 媒体播放自定义界面

    public class VideoActivity extends Activity implements Callback{
    private SurfaceView view;
    private SurfaceHolder holder;
    private MediaPlayer mPlayer;

    private String path;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.video_main);

    initWidgets();
    }

    private void initWidgets() {
    view=(SurfaceView) findViewById(R.id.surfaceView);

    holder=view.getHolder();
    holder.setFixedSize(320, 480);
    holder.setKeepScreenOn(true);
    //设置回调的监听
    holder.addCallback(this);

    path=getIntent().getStringExtra("path");
    }

    @Override
    public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {

    }

    @Override
    public void surfaceCreated(SurfaceHolder arg0) {
    mPlayer=new MediaPlayer();
    try {
    mPlayer.setDataSource(path);
    mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    mPlayer.setDisplay(holder);

    mPlayer.prepare();
    } catch (IllegalArgumentException e) {
    e.printStackTrace();
    } catch (SecurityException e) {
    e.printStackTrace();
    } catch (IllegalStateException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder arg0) {
    mPlayer.release();
    }

    public void play(View v){
    if (!mPlayer.isPlaying()) {
    mPlayer.start();
    }
    }
    }

    public class MainActivity extends Activity implements SurfaceHolder.Callback {//2.实现回调SurfaceHolder.Callback
    private VideoView videoView;

    // 第二种界面
    private SurfaceView view;
    private SurfaceHolder holder;

    private MediaPlayer mPlayer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.vedio_main);

    // String path = Environment.getExternalStorageDirectory()+"/move.mp4";
    // //初始化视频界面
    // videoView = (VideoView)findViewById(R.id.video);
    //
    // //向视频界面加载视频源
    // videoView.setVideoPath(path);
    //
    // MediaController controller = new MediaController(this);//控制中心
    // controller.setMediaPlayer(videoView);//将播放界面用controller来控制
    //
    // videoView.setMediaController(controller);//界面与控制中心绑定
    // videoView.requestFocus();//设置视频有焦点

    // 自定义的界面:视频播放器,SurfaceView是专做界面的容器
    view = (SurfaceView) findViewById(R.id.view);

    // 1.绑定界面并设置参数
    holder = view.getHolder();// 实例化视频Holder对象,此时由holder来设定参数
    holder.setFixedSize(320, 480);// 视频分辨率,不写就是默认
    holder.setKeepScreenOn(true);// 屏幕一直开着

    //设置回调的监听 
    holder.addCallback(this);
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
    int height) {//界面改变时

    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {//创建界面的时候
    mPlayer = new MediaPlayer();
    try {
    // Cursor cursor = getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, null,null,null,null);
    // while(cursor.moveToNext()){
    // Log.i("path", cursor.getString(cursor.getColumnIndex(Media.DATA)));
    // }

    mPlayer.setDataSource(Environment.getExternalStorageDirectory()+"/move.mp4");
    mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);//音源类型
    mPlayer.setDisplay(holder);//加载设定好的自定义界面
    mPlayer.prepare();//准备
    } catch (Exception e) {
    e.printStackTrace();
    }
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {//销毁界面
    if(mPlayer.isPlaying()){
    mPlayer.stop();
    mPlayer.release();
    }
    }

    //播放按钮
    public void play(View v){
    if(!mPlayer.isPlaying()){
    mPlayer.start();
    }
    }
    }

  • 相关阅读:
    java--接口和抽象类
    java-访问权限
    Appium点击掉弹窗的方法小记
    Appium报错及解决小记
    adb获取手机设备蓝牙&热点&wifi状态并操作的笔记
    Linux下安装appium环境搭建杂记
    Appium的使用笔记(自动化测试前的分析)
    Docker学习笔记
    python学习杂记-DDT驱动测试
    python学习杂记-处理CSV文件
  • 原文地址:https://www.cnblogs.com/wangfeng520/p/5123492.html
Copyright © 2011-2022 走看看