tcking / GiraffePlayer2

out of the box android video player(support lazy load, ListView/RecyclerView and hight performance)
Apache License 2.0
377 stars 110 forks source link

播放本地视频黑屏 #89

Open uelook opened 6 years ago

uelook commented 6 years ago

第一次播放的时候、会黑屏一段时间,以后再播放就没有了

`public class AdsView extends LinearLayout {

ImageView ivAds;//图片
VideoView videoView;//视频

private GiraffePlayer player;

private List<MediaData> dataList;//数据源

int index = 0;
private int singleShowTime = 5;
private final CompositeDisposable disposables = new CompositeDisposable();

private PlayerListener playerListener = new DefaultPlayerListener() {//example of using playerListener
    @Override
    public void onPreparing(GiraffePlayer giraffePlayer) {

    }

    @Override
    public void onCompletion(GiraffePlayer giraffePlayer) {

        if (index < dataList.size() - 1) {
            index++;
        } else {
            index = 0;
        }
        initData();
    }
};

public AdsView(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    initView(context);
}

private void initView(final Context context) {
    View view = LayoutInflater.from(context).inflate(R.layout.view_ads, this);
    ivAds = view.findViewById(R.id.iv_ads);
    videoView = view.findViewById(R.id.video_ads);
    videoView.setPlayerListener(playerListener);

}

//5秒后切换海报
private Observable<? extends Long> getObservable() {
    singleShowTime = SPUtils.getInstance().getInt(Constant.S_SINGLE_TIME, 5);
    return Observable.timer(singleShowTime, TimeUnit.SECONDS);
}

private DisposableObserver<Long> getObserver() {
    return new DisposableObserver<Long>() {

        @Override
        public void onNext(Long value) {

        }

        @Override
        public void onError(Throwable e) {
        }

        @Override
        public void onComplete() {

            if (index < dataList.size() - 1) {
                index++;
            } else {
                index = 0;
            }
            initData();

        }
    };
}

/**
 * 设置数据源
 */
public void setDataList(List<MediaData> dataList) {

    this.dataList = dataList;

    initData();
}

private void initData() {
    disposables.clear();
    MediaData mbean = dataList.get(index);
    switch (mbean.getType()) {

        case 1://图片
            showImage();
            disposables.add(getObservable()
                    // Run on a background thread
                    .subscribeOn(Schedulers.io())
                    // Be notified on the main thread
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribeWith(getObserver()));

            Glide.with(this.getContext())
                    .load(Uri.fromFile(new File(mbean.getLocalFilePath())))
                    .into(ivAds);
            break;

        case 2://视频
            showVideo();
            String videoUrl = mbean.getLocalFilePath();
            videoView.getVideoInfo().setBgColor(Color.TRANSPARENT).setPortraitWhenFullScreen(false).setAspectRatio(VideoInfo.AR_MATCH_PARENT);
            videoView.setVideoPath(videoUrl).getPlayer().start();
            player = videoView.getPlayer();
            break;
    }

}

private void showVideo() {
    videoView.setVisibility(VISIBLE);
    ivAds.setVisibility(GONE);

}

private void showImage() {
    ivAds.setVisibility(VISIBLE);
    videoView.setVisibility(GONE);

}

@Override
protected void onDetachedFromWindow() {
    super.onDetachedFromWindow();
    if (disposables != null)
        disposables.clear();
    if (player != null) {
        player.stop();
        player.release();
    }
}

public void stopPlayer() {
    if (disposables != null)
        disposables.clear();
    if (player != null) {
        player.stop();
        player.release();
    }
}

}`

tcking commented 6 years ago

请问有视频地址吗?是任何视频都这样还是某个才会这样