Fix bad voice note duration and listener breakage.
This commit is contained in:
parent
81889d8130
commit
9279a54d28
3 changed files with 53 additions and 31 deletions
|
@ -17,7 +17,6 @@ import android.widget.TextView;
|
|||
import androidx.annotation.ColorInt;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.core.util.Consumer;
|
||||
import androidx.lifecycle.Observer;
|
||||
|
||||
import com.airbnb.lottie.LottieAnimationView;
|
||||
|
@ -210,15 +209,22 @@ public final class AudioView extends FrameLayout {
|
|||
}
|
||||
|
||||
private void onPlaybackState(@NonNull VoiceNotePlaybackState voiceNotePlaybackState) {
|
||||
onDuration(voiceNotePlaybackState.getUri(), voiceNotePlaybackState.getTrackDuration());
|
||||
onStart(voiceNotePlaybackState.getUri(), voiceNotePlaybackState.isAutoReset());
|
||||
onProgress(voiceNotePlaybackState.getUri(),
|
||||
(double) voiceNotePlaybackState.getPlayheadPositionMillis() / voiceNotePlaybackState.getTrackDuration(),
|
||||
voiceNotePlaybackState.getPlayheadPositionMillis());
|
||||
}
|
||||
|
||||
private void onDuration(@NonNull Uri uri, long durationMillis) {
|
||||
if (isTarget(uri)) {
|
||||
this.durationMillis = durationMillis;
|
||||
}
|
||||
}
|
||||
|
||||
private void onStart(@NonNull Uri uri, boolean autoReset) {
|
||||
if (!Objects.equals(uri, audioSlide.getUri())) {
|
||||
if (audioSlide != null && audioSlide.getUri() != null) {
|
||||
if (!isTarget(uri)) {
|
||||
if (hasAudioUri()) {
|
||||
onStop(audioSlide.getUri(), autoReset);
|
||||
}
|
||||
|
||||
|
@ -234,7 +240,7 @@ public final class AudioView extends FrameLayout {
|
|||
}
|
||||
|
||||
private void onStop(@NonNull Uri uri, boolean autoReset) {
|
||||
if (!Objects.equals(uri, audioSlide.getUri())) {
|
||||
if (!isTarget(uri)) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -251,6 +257,30 @@ public final class AudioView extends FrameLayout {
|
|||
}
|
||||
}
|
||||
|
||||
private void onProgress(@NonNull Uri uri, double progress, long millis) {
|
||||
if (!isTarget(uri)) {
|
||||
return;
|
||||
}
|
||||
|
||||
int seekProgress = (int) Math.floor(progress * seekBar.getMax());
|
||||
|
||||
if (seekProgress > seekBar.getProgress() || backwardsCounter > 3) {
|
||||
backwardsCounter = 0;
|
||||
seekBar.setProgress(seekProgress);
|
||||
updateProgress((float) progress, millis);
|
||||
} else {
|
||||
backwardsCounter++;
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isTarget(@NonNull Uri uri) {
|
||||
return hasAudioUri() && Objects.equals(uri, audioSlide.getUri());
|
||||
}
|
||||
|
||||
private boolean hasAudioUri() {
|
||||
return audioSlide != null && audioSlide.getUri() != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setFocusable(boolean focusable) {
|
||||
super.setFocusable(focusable);
|
||||
|
@ -277,22 +307,6 @@ public final class AudioView extends FrameLayout {
|
|||
this.downloadButton.setEnabled(enabled);
|
||||
}
|
||||
|
||||
private void onProgress(@NonNull Uri uri, double progress, long millis) {
|
||||
if (audioSlide == null || !Objects.equals(uri, audioSlide.getUri())) {
|
||||
return;
|
||||
}
|
||||
|
||||
int seekProgress = (int) Math.floor(progress * seekBar.getMax());
|
||||
|
||||
if (seekProgress > seekBar.getProgress() || backwardsCounter > 3) {
|
||||
backwardsCounter = 0;
|
||||
seekBar.setProgress(seekProgress);
|
||||
updateProgress((float) progress, millis);
|
||||
} else {
|
||||
backwardsCounter++;
|
||||
}
|
||||
}
|
||||
|
||||
private void updateProgress(float progress, long millis) {
|
||||
if (callbacks != null) {
|
||||
callbacks.onProgressUpdated(durationMillis, millis);
|
||||
|
@ -386,7 +400,7 @@ public final class AudioView extends FrameLayout {
|
|||
} else {
|
||||
callbacks.onPause(audioSlide.getUri());
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -225,13 +225,25 @@ public class VoiceNoteMediaController implements DefaultLifecycleObserver {
|
|||
mediaMetadataCompat.getDescription() != null)
|
||||
{
|
||||
|
||||
Uri mediaUri = Objects.requireNonNull(mediaMetadataCompat.getDescription().getMediaUri());
|
||||
boolean autoReset = Objects.equals(mediaUri, VoiceNotePlaybackPreparer.NEXT_URI) || Objects.equals(mediaUri, VoiceNotePlaybackPreparer.END_URI);
|
||||
Uri mediaUri = Objects.requireNonNull(mediaMetadataCompat.getDescription().getMediaUri());
|
||||
boolean autoReset = Objects.equals(mediaUri, VoiceNotePlaybackPreparer.NEXT_URI) || Objects.equals(mediaUri, VoiceNotePlaybackPreparer.END_URI);
|
||||
VoiceNotePlaybackState previousState = voiceNotePlaybackState.getValue();
|
||||
long position = mediaController.getPlaybackState().getPosition();
|
||||
long duration = mediaMetadataCompat.getLong(MediaMetadataCompat.METADATA_KEY_DURATION);
|
||||
|
||||
voiceNotePlaybackState.postValue(new VoiceNotePlaybackState(mediaUri,
|
||||
mediaController.getPlaybackState().getPosition(),
|
||||
mediaMetadataCompat.getLong(MediaMetadataCompat.METADATA_KEY_DURATION),
|
||||
autoReset));
|
||||
if (previousState != null && Objects.equals(mediaUri, previousState.getUri())) {
|
||||
if (position < 0 && previousState.getPlayheadPositionMillis() >= 0) {
|
||||
position = previousState.getPlayheadPositionMillis();
|
||||
}
|
||||
|
||||
if (duration <= 0 && previousState.getTrackDuration() > 0) {
|
||||
duration = previousState.getTrackDuration();
|
||||
}
|
||||
}
|
||||
|
||||
if (duration > 0 && position >= 0 && position <= duration) {
|
||||
voiceNotePlaybackState.postValue(new VoiceNotePlaybackState(mediaUri, position, duration, autoReset));
|
||||
}
|
||||
|
||||
sendEmptyMessageDelayed(0, 50);
|
||||
} else {
|
||||
|
|
|
@ -410,10 +410,6 @@ public class ConversationItem extends LinearLayout implements BindableConversati
|
|||
conversationRecipient.removeForeverObserver(this);
|
||||
}
|
||||
cancelPulseOutlinerAnimation();
|
||||
if (eventListener != null && audioViewStub.resolved()) {
|
||||
Log.d(TAG, "unbind: unregistering voice note callbacks for audio slide " + audioViewStub.get().getAudioSlideUri());
|
||||
eventListener.onUnregisterVoiceNoteCallbacks(audioViewStub.get().getPlaybackStateObserver());
|
||||
}
|
||||
}
|
||||
|
||||
public ConversationMessage getConversationMessage() {
|
||||
|
|
Loading…
Add table
Reference in a new issue