Closed lewic1987 closed 3 years ago
三、ExoPlayback类 1.解决播放错误后自动切换下一首问题 修改后: override fun onPlayerStateChanged(playWhenReady: Boolean, playbackState: Int) { //lewic修改,播放失败时候exo会回调onPlayerError, // 同时也会回调onPlayerStateChanged playbackState = STATE_IDLE //去除这种情况处理,防止出错后走到完成回调继续播放下一首 if (playbackState == Player.STATE_IDLE && isError){ isError = false return } isError = false
override fun onPlayerError(error: ExoPlaybackException) { error.printStackTrace() isError = true
修改前: 无以上逻辑
四、PlayerControlImpl类 1.解决LiveData postValue 数据丢失的问题 onPlaybackStateUpdated方法修改前: playbackState.postValue(playbackStage)
修改后: //lewic修改 修复LiveData postValue 数据丢失的问题 LiveDataUtils.setValue(playbackState,playbackStage)
其他playbackState.postValue的地方修改同上
LiveDataUtils类: public class LiveDataUtils { private static Handler sMainHandler; /**
用 setValue 更新 MutableLiveData 的数据,如果在子线程,就切换到主线程 */ public static void setValue(MutableLiveData mld, T d) { if (mld == null) { return; } if (Thread.currentThread() == Looper.getMainLooper().getThread()) { mld.setValue(d); } else { postSetValue(mld, d); } }
/**
向主线程的 handler 抛 SetValueRunnable */ private static void postSetValue(MutableLiveData mld, T d) { if (sMainHandler == null) { sMainHandler = new Handler(Looper.getMainLooper()); } sMainHandler.post(SetValueRunnable.create(mld, d)); }
private static class SetValueRunnable implements Runnable { private final MutableLiveData liveData; private final T data;
private SetValueRunnable(@NonNull MutableLiveData<T> liveData, T data) { this.liveData = liveData; this.data = data; } @Override public void run() { liveData.setValue(data); } public static <T> SetValueRunnable<T> create(@NonNull MutableLiveData<T> liveData, T data) { return new SetValueRunnable<>(liveData, data); }
} }
👌
三、ExoPlayback类 1.解决播放错误后自动切换下一首问题 修改后: override fun onPlayerStateChanged(playWhenReady: Boolean, playbackState: Int) { //lewic修改,播放失败时候exo会回调onPlayerError, // 同时也会回调onPlayerStateChanged playbackState = STATE_IDLE //去除这种情况处理,防止出错后走到完成回调继续播放下一首 if (playbackState == Player.STATE_IDLE && isError){ isError = false return } isError = false
override fun onPlayerError(error: ExoPlaybackException) { error.printStackTrace() isError = true
修改前: 无以上逻辑
四、PlayerControlImpl类 1.解决LiveData postValue 数据丢失的问题 onPlaybackStateUpdated方法修改前: playbackState.postValue(playbackStage)
修改后: //lewic修改 修复LiveData postValue 数据丢失的问题 LiveDataUtils.setValue(playbackState,playbackStage)
其他playbackState.postValue的地方修改同上
LiveDataUtils类: public class LiveDataUtils { private static Handler sMainHandler; /**
用 setValue 更新 MutableLiveData 的数据,如果在子线程,就切换到主线程 */ public static void setValue(MutableLiveData mld, T d) {
if (mld == null) {
return;
}
if (Thread.currentThread() == Looper.getMainLooper().getThread()) {
mld.setValue(d);
} else {
postSetValue(mld, d);
}
}
/**
向主线程的 handler 抛 SetValueRunnable */ private static void postSetValue(MutableLiveData mld, T d) {
if (sMainHandler == null) {
sMainHandler = new Handler(Looper.getMainLooper());
}
sMainHandler.post(SetValueRunnable.create(mld, d));
}
private static class SetValueRunnable implements Runnable {
private final MutableLiveData liveData;
private final T data;
} }