android surfaceView+mediaPlayer 自定义视频播放器

播放M3U8为什么不回调setOnPrepareListener

2个回答

你可以参考我的写法,注意点就是你自定义的MyCallBack()里面要回调,也就是我写的SurfaceCallBack()里面的SurfaceCreated()函数要实现你写的setOnPreparedListener()的监听
public class Video implements MediaPlayer.OnCompletionListener,MediaPlayer.OnPreparedListener,MediaPlayer.OnErrorListener,MediaPlayer.OnBufferingUpdateListener,VideoClickListener{

private Context context;
private SurfaceView surfaceView;
private SurfaceHolder surfaceHolder;
private MediaPlayer mediaPlayer;
private ProgressBar progressBar;
private SeekBar seekBar;
private VerticalSeekBar seekBar_sound;
private ImageView pause,full_screen,silent;
private TextView textView;
private boolean seekBarAutoFlag;
private String videoTimeString;


//判断值
private boolean isFullScreen=false;  //是否全屏
private boolean isPause=false; //是否暂停
private boolean isSilent=false;//是否静音

private int screenWidth;  //获取到设备的宽高
private int screenHeight;
@Override
public MediaPlayer getMediaPlayer() {
    return mediaPlayer;
}

@Override
public SurfaceView getSurfaveView() {
    return surfaceView;
}

@Override
public Thread getThread() {
    return thread;
}

public Video(Context context, MediaPlayer mediaPlayereekBar, SurfaceView surfaceView, ProgressBar progressBar, SeekBar seekBar, TextView textView, ImageView pause, ImageView full_screen) {
    this.context = context;
    this.mediaPlayer=mediaPlayereekBar;
    this.surfaceView=surfaceView;
    this.progressBar=progressBar;
    this.seekBar=seekBar;
    this.textView=textView;
    this.pause=pause;
    this.full_screen=full_screen;

    init();
}



/**
 * 初始化控件
 */
public void init(){


    // 设置surfaceHolder
    surfaceHolder = surfaceView.getHolder();
    // 设置Holder类型,该类型表示surfaceView自己不管理缓存区,虽然提示过时,但最好还是要设置
    surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    // 设置surface回调
    surfaceHolder.addCallback(new SurfaceCallback());

 //   mAudioManager= (AudioManager) context.getSystemService(AUDIO_SERVICE);
    //获取屏幕的宽高
    WindowManager wm = (WindowManager) context
            .getSystemService(Context.WINDOW_SERVICE);
    screenWidth = wm.getDefaultDisplay().getWidth();
    screenHeight = wm.getDefaultDisplay().getHeight();

}

/**
 * 回调 对SurfaceView的生命周期的实现。
 */
private class SurfaceCallback implements SurfaceHolder.Callback {
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        // SurfaceView的大小改变
        mediaPlayer.setDisplay(surfaceHolder);
    }

    public void surfaceCreated(SurfaceHolder holder) {
        // surfaceView被创建
        // 设置播放资源
        playVideo();
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        // surfaceView销毁
        // 如果MediaPlayer没被销毁,则销毁mediaPlayer
        if (null != mediaPlayer) {
            mediaPlayer.release();
            mediaPlayer = null;
        }
    }
}


/**
 * 播放视频
 */
public void playVideo() {
    // 初始化MediaPlayer
    mediaPlayer = new MediaPlayer();
    // 重置mediaPaly,建议在初始滑mediaplay立即调用。
    mediaPlayer.reset();
    // 设置声音效果
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    // 设置播放完成监听
    mediaPlayer.setOnCompletionListener(Video.this);

    // 设置媒体加载完成以后回调函数。
    mediaPlayer.setOnPreparedListener(this);
    // 错误监听回调函数
    mediaPlayer.setOnErrorListener(this);
    // 设置缓存变化监听
    mediaPlayer.setOnBufferingUpdateListener(this);
    File file = new File(Environment.getExternalStorageDirectory(), "震撼宇宙10秒倒计时.mp4");
    String a=file.getPath();
    Uri uri = Uri
            .parse("http://video.eastday.com/a/180320180216447158758.html?qid=01360");
    try {
        // mediaPlayer.reset();
       //mediaPlayer.setDataSource(media.this, uri);
       mediaPlayer.setDataSource(a);
        // 设置异步加载视频,包括两种方式 prepare()同步,prepareAsync()异步
        mediaPlayer.prepareAsync();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

图片说明

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问