Added progress indication to audio player

pull/214/head
M66B 8 months ago
parent 190fc828e1
commit 12253d9846

@ -35,6 +35,7 @@ import android.os.Bundle;
import android.os.ParcelFileDescriptor; import android.os.ParcelFileDescriptor;
import android.provider.MediaStore; import android.provider.MediaStore;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Pair;
import android.util.Size; import android.util.Size;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
@ -94,11 +95,13 @@ public class AdapterMedia extends RecyclerView.Adapter<AdapterMedia.ViewHolder>
view.setOnLongClickListener(null); view.setOnLongClickListener(null);
} }
private void showPlayerState(EntityAttachment attachment) { private void showPlayerState(Uri uri) {
if (MediaPlayerHelper.isPlaying(attachment.getUri(context))) if (MediaPlayerHelper.isPlaying(uri))
ivImage.setImageResource(R.drawable.twotone_stop_48); ivImage.setImageResource(R.drawable.twotone_stop_48);
else else {
ivImage.setImageResource(R.drawable.twotone_play_arrow_48); ivImage.setImageResource(R.drawable.twotone_play_arrow_48);
tvProperties.setVisibility(View.GONE);
}
} }
private void bindTo(EntityAttachment attachment) { private void bindTo(EntityAttachment attachment) {
@ -108,7 +111,7 @@ public class AdapterMedia extends RecyclerView.Adapter<AdapterMedia.ViewHolder>
if (attachment.available) { if (attachment.available) {
if (attachment.isAudio()) { if (attachment.isAudio()) {
showPlayerState(attachment); showPlayerState(attachment.getUri(context));
return; return;
} }
@ -281,16 +284,33 @@ public class AdapterMedia extends RecyclerView.Adapter<AdapterMedia.ViewHolder>
Uri uri = attachment.getUri(context); Uri uri = attachment.getUri(context);
if (MediaPlayerHelper.isPlaying(uri)) if (MediaPlayerHelper.isPlaying(uri))
MediaPlayerHelper.stopMusic(context); MediaPlayerHelper.stopMusic(context);
else else {
Runnable updatePosition = new RunnableEx("updatePosition") {
@Override
protected void delegate() {
Pair<Integer, Integer> pos = MediaPlayerHelper.getPosition(uri);
if (pos != null) {
int at = (int) Math.round(pos.first / 1000.0) * 1000;
tvProperties.setText(
Helper.formatDuration(at, false) + " / " +
Helper.formatDuration(pos.second, true));
view.postDelayed(this, 1000L);
}
tvProperties.setVisibility(pos == null ? View.GONE : View.VISIBLE);
}
};
view.postDelayed(updatePosition, 1000L);
MediaPlayerHelper.startMusic(context, uri, MediaPlayerHelper.startMusic(context, uri,
new RunnableEx("player") { new RunnableEx("onCompleted") {
@Override @Override
public void delegate() { public void delegate() {
showPlayerState(attachment); showPlayerState(uri);
} }
}); });
}
showPlayerState(attachment); showPlayerState(uri);
} catch (Throwable ex) { } catch (Throwable ex) {
ivImage.setImageResource(R.drawable.twotone_warning_24); ivImage.setImageResource(R.drawable.twotone_warning_24);
Log.unexpectedError(parentFragment, ex); Log.unexpectedError(parentFragment, ex);

@ -29,6 +29,7 @@ import android.media.MediaPlayer;
import android.net.Uri; import android.net.Uri;
import android.os.Build; import android.os.Build;
import android.os.PowerManager; import android.os.PowerManager;
import android.util.Pair;
import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationCompat;
import androidx.lifecycle.Lifecycle; import androidx.lifecycle.Lifecycle;
@ -218,6 +219,17 @@ public class MediaPlayerHelper {
} }
} }
static Pair<Integer, Integer> getPosition(Uri uri) {
synchronized (lock) {
if (!isPlaying(uri))
return null;
return new Pair<>(
MediaPlayerHelper.player.getCurrentPosition(),
MediaPlayerHelper.player.getDuration()
);
}
}
static void liveInCall(Context context, LifecycleOwner owner, IInCall intf) { static void liveInCall(Context context, LifecycleOwner owner, IInCall intf) {
AudioManager am = Helper.getSystemService(context, AudioManager.class); AudioManager am = Helper.getSystemService(context, AudioManager.class);
if (am == null || Build.VERSION.SDK_INT < Build.VERSION_CODES.S) { if (am == null || Build.VERSION.SDK_INT < Build.VERSION_CODES.S) {

Loading…
Cancel
Save