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.ColorInt;
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
import androidx.core.util.Consumer;
|
|
||||||
import androidx.lifecycle.Observer;
|
import androidx.lifecycle.Observer;
|
||||||
|
|
||||||
import com.airbnb.lottie.LottieAnimationView;
|
import com.airbnb.lottie.LottieAnimationView;
|
||||||
|
@ -210,15 +209,22 @@ public final class AudioView extends FrameLayout {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void onPlaybackState(@NonNull VoiceNotePlaybackState voiceNotePlaybackState) {
|
private void onPlaybackState(@NonNull VoiceNotePlaybackState voiceNotePlaybackState) {
|
||||||
|
onDuration(voiceNotePlaybackState.getUri(), voiceNotePlaybackState.getTrackDuration());
|
||||||
onStart(voiceNotePlaybackState.getUri(), voiceNotePlaybackState.isAutoReset());
|
onStart(voiceNotePlaybackState.getUri(), voiceNotePlaybackState.isAutoReset());
|
||||||
onProgress(voiceNotePlaybackState.getUri(),
|
onProgress(voiceNotePlaybackState.getUri(),
|
||||||
(double) voiceNotePlaybackState.getPlayheadPositionMillis() / voiceNotePlaybackState.getTrackDuration(),
|
(double) voiceNotePlaybackState.getPlayheadPositionMillis() / voiceNotePlaybackState.getTrackDuration(),
|
||||||
voiceNotePlaybackState.getPlayheadPositionMillis());
|
voiceNotePlaybackState.getPlayheadPositionMillis());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void onDuration(@NonNull Uri uri, long durationMillis) {
|
||||||
|
if (isTarget(uri)) {
|
||||||
|
this.durationMillis = durationMillis;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void onStart(@NonNull Uri uri, boolean autoReset) {
|
private void onStart(@NonNull Uri uri, boolean autoReset) {
|
||||||
if (!Objects.equals(uri, audioSlide.getUri())) {
|
if (!isTarget(uri)) {
|
||||||
if (audioSlide != null && audioSlide.getUri() != null) {
|
if (hasAudioUri()) {
|
||||||
onStop(audioSlide.getUri(), autoReset);
|
onStop(audioSlide.getUri(), autoReset);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -234,7 +240,7 @@ public final class AudioView extends FrameLayout {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void onStop(@NonNull Uri uri, boolean autoReset) {
|
private void onStop(@NonNull Uri uri, boolean autoReset) {
|
||||||
if (!Objects.equals(uri, audioSlide.getUri())) {
|
if (!isTarget(uri)) {
|
||||||
return;
|
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
|
@Override
|
||||||
public void setFocusable(boolean focusable) {
|
public void setFocusable(boolean focusable) {
|
||||||
super.setFocusable(focusable);
|
super.setFocusable(focusable);
|
||||||
|
@ -277,22 +307,6 @@ public final class AudioView extends FrameLayout {
|
||||||
this.downloadButton.setEnabled(enabled);
|
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) {
|
private void updateProgress(float progress, long millis) {
|
||||||
if (callbacks != null) {
|
if (callbacks != null) {
|
||||||
callbacks.onProgressUpdated(durationMillis, millis);
|
callbacks.onProgressUpdated(durationMillis, millis);
|
||||||
|
@ -386,7 +400,7 @@ public final class AudioView extends FrameLayout {
|
||||||
} else {
|
} else {
|
||||||
callbacks.onPause(audioSlide.getUri());
|
callbacks.onPause(audioSlide.getUri());
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -225,13 +225,25 @@ public class VoiceNoteMediaController implements DefaultLifecycleObserver {
|
||||||
mediaMetadataCompat.getDescription() != null)
|
mediaMetadataCompat.getDescription() != null)
|
||||||
{
|
{
|
||||||
|
|
||||||
Uri mediaUri = Objects.requireNonNull(mediaMetadataCompat.getDescription().getMediaUri());
|
Uri mediaUri = Objects.requireNonNull(mediaMetadataCompat.getDescription().getMediaUri());
|
||||||
boolean autoReset = Objects.equals(mediaUri, VoiceNotePlaybackPreparer.NEXT_URI) || Objects.equals(mediaUri, VoiceNotePlaybackPreparer.END_URI);
|
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,
|
if (previousState != null && Objects.equals(mediaUri, previousState.getUri())) {
|
||||||
mediaController.getPlaybackState().getPosition(),
|
if (position < 0 && previousState.getPlayheadPositionMillis() >= 0) {
|
||||||
mediaMetadataCompat.getLong(MediaMetadataCompat.METADATA_KEY_DURATION),
|
position = previousState.getPlayheadPositionMillis();
|
||||||
autoReset));
|
}
|
||||||
|
|
||||||
|
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);
|
sendEmptyMessageDelayed(0, 50);
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -410,10 +410,6 @@ public class ConversationItem extends LinearLayout implements BindableConversati
|
||||||
conversationRecipient.removeForeverObserver(this);
|
conversationRecipient.removeForeverObserver(this);
|
||||||
}
|
}
|
||||||
cancelPulseOutlinerAnimation();
|
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() {
|
public ConversationMessage getConversationMessage() {
|
||||||
|
|
Loading…
Add table
Reference in a new issue