Log warning instead of throwing NPE for voice note controller.
This commit is contained in:
parent
d06c633dc4
commit
98fc3e5b0b
1 changed files with 54 additions and 1 deletions
|
@ -145,7 +145,7 @@ public class VoiceNoteMediaController implements DefaultLifecycleObserver {
|
|||
return playbackStateCompat.getState() <= PlaybackStateCompat.STATE_STOPPED;
|
||||
}
|
||||
|
||||
private @NonNull MediaControllerCompat getMediaController() {
|
||||
private @Nullable MediaControllerCompat getMediaController() {
|
||||
return MediaControllerCompat.getMediaController(activity);
|
||||
}
|
||||
|
||||
|
@ -172,6 +172,11 @@ public class VoiceNoteMediaController implements DefaultLifecycleObserver {
|
|||
* @param singlePlayback The player will only play back the specified Uri, and not build a playlist.
|
||||
*/
|
||||
private void startPlayback(@NonNull Uri audioSlideUri, long messageId, long threadId, double progress, boolean singlePlayback) {
|
||||
if (getMediaController() == null) {
|
||||
Log.w(TAG, "Called startPlayback before controller was set. (" + getActivityName() + ")");
|
||||
return;
|
||||
}
|
||||
|
||||
if (isCurrentTrack(audioSlideUri)) {
|
||||
long duration = getMediaController().getMetadata().getLong(MediaMetadataCompat.METADATA_KEY_DURATION);
|
||||
|
||||
|
@ -196,6 +201,11 @@ public class VoiceNoteMediaController implements DefaultLifecycleObserver {
|
|||
* @param messageId The Message id of the given audio slide
|
||||
*/
|
||||
public void resumePlayback(@NonNull Uri audioSlideUri, long messageId) {
|
||||
if (getMediaController() == null) {
|
||||
Log.w(TAG, "Called resumePlayback before controller was set. (" + getActivityName() + ")");
|
||||
return;
|
||||
}
|
||||
|
||||
if (isCurrentTrack(audioSlideUri)) {
|
||||
getMediaController().getTransportControls().play();
|
||||
} else {
|
||||
|
@ -215,6 +225,11 @@ public class VoiceNoteMediaController implements DefaultLifecycleObserver {
|
|||
* @param audioSlideUri The Uri of the audio slide to pause.
|
||||
*/
|
||||
public void pausePlayback(@NonNull Uri audioSlideUri) {
|
||||
if (getMediaController() == null) {
|
||||
Log.w(TAG, "Called pausePlayback(uri) before controller was set. (" + getActivityName() + ")");
|
||||
return;
|
||||
}
|
||||
|
||||
if (isCurrentTrack(audioSlideUri)) {
|
||||
getMediaController().getTransportControls().pause();
|
||||
}
|
||||
|
@ -224,6 +239,11 @@ public class VoiceNoteMediaController implements DefaultLifecycleObserver {
|
|||
* Pauses playback regardless of which audio slide is playing.
|
||||
*/
|
||||
public void pausePlayback() {
|
||||
if (getMediaController() == null) {
|
||||
Log.w(TAG, "Called pausePlayback before controller was set. (" + getActivityName() + ")");
|
||||
return;
|
||||
}
|
||||
|
||||
getMediaController().getTransportControls().pause();
|
||||
}
|
||||
|
||||
|
@ -235,6 +255,11 @@ public class VoiceNoteMediaController implements DefaultLifecycleObserver {
|
|||
* @param progress The progress percentage to seek to.
|
||||
*/
|
||||
public void seekToPosition(@NonNull Uri audioSlideUri, double progress) {
|
||||
if (getMediaController() == null) {
|
||||
Log.w(TAG, "Called seekToPosition before controller was set. (" + getActivityName() + ")");
|
||||
return;
|
||||
}
|
||||
|
||||
if (isCurrentTrack(audioSlideUri)) {
|
||||
long duration = getMediaController().getMetadata().getLong(MediaMetadataCompat.METADATA_KEY_DURATION);
|
||||
|
||||
|
@ -250,12 +275,22 @@ public class VoiceNoteMediaController implements DefaultLifecycleObserver {
|
|||
* @param audioSlideUri The Uri of the audio slide to stop
|
||||
*/
|
||||
public void stopPlaybackAndReset(@NonNull Uri audioSlideUri) {
|
||||
if (getMediaController() == null) {
|
||||
Log.w(TAG, "Called stopPlaybackAndReset before controller was set. (" + getActivityName() + ")");
|
||||
return;
|
||||
}
|
||||
|
||||
if (isCurrentTrack(audioSlideUri)) {
|
||||
getMediaController().getTransportControls().stop();
|
||||
}
|
||||
}
|
||||
|
||||
public void setPlaybackSpeed(@NonNull Uri audioSlideUri, float playbackSpeed) {
|
||||
if (getMediaController() == null) {
|
||||
Log.w(TAG, "Called setPlaybackSpeed before controller was set. (" + getActivityName() + ")");
|
||||
return;
|
||||
}
|
||||
|
||||
if (isCurrentTrack(audioSlideUri)) {
|
||||
Bundle bundle = new Bundle();
|
||||
bundle.putFloat(VoiceNotePlaybackService.ACTION_NEXT_PLAYBACK_SPEED, playbackSpeed);
|
||||
|
@ -265,12 +300,22 @@ public class VoiceNoteMediaController implements DefaultLifecycleObserver {
|
|||
}
|
||||
|
||||
private boolean isCurrentTrack(@NonNull Uri uri) {
|
||||
if (getMediaController() == null) {
|
||||
Log.w(TAG, "Called isCurrentTrack before controller was set. (" + getActivityName() + ")");
|
||||
return false;
|
||||
}
|
||||
|
||||
MediaMetadataCompat metadataCompat = getMediaController().getMetadata();
|
||||
|
||||
return metadataCompat != null && Objects.equals(metadataCompat.getDescription().getMediaUri(), uri);
|
||||
}
|
||||
|
||||
private void notifyProgressEventHandler() {
|
||||
if (getMediaController() == null) {
|
||||
Log.w(TAG, "Called notifyProgressEventHandler before controller was set. (" + getActivityName() + ")");
|
||||
return;
|
||||
}
|
||||
|
||||
if (progressEventHandler == null && activity != null) {
|
||||
progressEventHandler = new ProgressEventHandler(getMediaController(), voiceNotePlaybackState);
|
||||
progressEventHandler.sendEmptyMessage(0);
|
||||
|
@ -283,6 +328,14 @@ public class VoiceNoteMediaController implements DefaultLifecycleObserver {
|
|||
}
|
||||
}
|
||||
|
||||
private @NonNull String getActivityName() {
|
||||
if (activity == null) {
|
||||
return "Activity is null";
|
||||
} else {
|
||||
return activity.getLocalClassName();
|
||||
}
|
||||
}
|
||||
|
||||
private final class ConnectionCallback extends MediaBrowserCompat.ConnectionCallback {
|
||||
@Override
|
||||
public void onConnected() {
|
||||
|
|
Loading…
Add table
Reference in a new issue