From 8fc6099892e433976f1bbea14e50157e50d52e69 Mon Sep 17 00:00:00 2001 From: Stefan Schueller Date: Wed, 26 Dec 2018 20:01:29 +0100 Subject: [PATCH] - Android playback control - Audio focus added --- app/build.gradle | 5 ++- .../net/schueller/peertube/model/Video.java | 40 +++++++++++++++++++ .../peertube/service/VideoPlayerService.java | 30 ++++++++++++++ 3 files changed, 73 insertions(+), 2 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index dfa2701..f9e0058 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -6,8 +6,8 @@ android { applicationId "net.schueller.peertube" minSdkVersion 21 targetSdkVersion 28 - versionCode 1014 - versionName "1.0.14" + versionCode 1015 + versionName "1.0.15" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) @@ -47,6 +47,7 @@ android { implementation 'com.google.android.exoplayer:exoplayer-ui:2.9.2' implementation 'com.google.android.exoplayer:exoplayer-hls:2.9.2' implementation 'com.google.android.exoplayer:exoplayer-smoothstreaming:2.9.2' + implementation 'com.google.android.exoplayer:extension-mediasession:2.9.2' // testing testImplementation 'junit:junit:4.12' diff --git a/app/src/main/java/net/schueller/peertube/model/Video.java b/app/src/main/java/net/schueller/peertube/model/Video.java index a640c73..377f645 100644 --- a/app/src/main/java/net/schueller/peertube/model/Video.java +++ b/app/src/main/java/net/schueller/peertube/model/Video.java @@ -1,8 +1,21 @@ package net.schueller.peertube.model; +import android.content.Context; +import android.graphics.Bitmap; +import android.net.Uri; +import android.os.Bundle; +import android.support.v4.media.MediaDescriptionCompat; + +import com.squareup.picasso.Picasso; + +import net.schueller.peertube.R; +import net.schueller.peertube.helper.APIUrlHelper; + import java.util.ArrayList; import java.util.Date; +import androidx.annotation.DrawableRes; + public class Video { private Integer id; @@ -247,4 +260,31 @@ public class Video { public void setFiles(ArrayList files) { this.files = files; } + + + + public static MediaDescriptionCompat getMediaDescription(Context context, Video video) { + +// String apiBaseURL = APIUrlHelper.getUrlWithVersion(context); + +// Bundle extras = new Bundle(); +// Bitmap bitmap = getBitmap(context, Uri.parse(apiBaseURL + video.thumbnailPath)); +// extras.putParcelable(MediaDescriptionCompat.DESCRIPTION_KEY_MEDIA_URI, bitmap); + + return new MediaDescriptionCompat.Builder() + .setMediaId(video.getUuid()) +// .setIconBitmap(bitmap) +// .setExtras(extras) + .setTitle(video.getName()) + .setDescription(video.getDescription()) + .build(); + } + +// TODO: add support for the thumbnail +// public static Bitmap getBitmap(Context context, Uri fullThumbnailUrl) { +// +// return Picasso.with(context).load(fullThumbnailUrl) +// .placeholder(R.drawable.ic_peertube) +// .error(R.drawable.ic_peertube).get(); +// } } diff --git a/app/src/main/java/net/schueller/peertube/service/VideoPlayerService.java b/app/src/main/java/net/schueller/peertube/service/VideoPlayerService.java index e50d66e..8eb4f14 100644 --- a/app/src/main/java/net/schueller/peertube/service/VideoPlayerService.java +++ b/app/src/main/java/net/schueller/peertube/service/VideoPlayerService.java @@ -11,14 +11,22 @@ import android.graphics.Bitmap; import android.media.AudioManager; import android.net.Uri; import android.os.Binder; +import android.os.Bundle; import android.os.IBinder; import androidx.annotation.Nullable; + +import android.support.v4.media.MediaDescriptionCompat; +import android.support.v4.media.session.MediaSessionCompat; import android.util.Log; +import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.ExoPlayerFactory; import com.google.android.exoplayer2.PlaybackParameters; import com.google.android.exoplayer2.Player; import com.google.android.exoplayer2.SimpleExoPlayer; +import com.google.android.exoplayer2.audio.AudioAttributes; +import com.google.android.exoplayer2.ext.mediasession.MediaSessionConnector; +import com.google.android.exoplayer2.ext.mediasession.TimelineQueueNavigator; import com.google.android.exoplayer2.source.ExtractorMediaSource; import com.google.android.exoplayer2.trackselection.DefaultTrackSelector; import com.google.android.exoplayer2.ui.PlayerNotificationManager; @@ -38,6 +46,7 @@ import static net.schueller.peertube.activity.VideoListActivity.EXTRA_VIDEOID; public class VideoPlayerService extends Service { private static final String TAG = "VideoPlayerService"; + private static final String MEDIA_SESSION_TAG = "peertube_player"; private final IBinder mBinder = new LocalBinder(); @@ -215,6 +224,26 @@ public class VideoPlayerService extends Service { playerNotificationManager.setPlayer(player); + // external Media control, Android Wear / Google Home etc. + MediaSessionCompat mediaSession = new MediaSessionCompat(context, MEDIA_SESSION_TAG); + mediaSession.setActive(true); + playerNotificationManager.setMediaSessionToken(mediaSession.getSessionToken()); + MediaSessionConnector mediaSessionConnector = new MediaSessionConnector(mediaSession); + mediaSessionConnector.setQueueNavigator(new TimelineQueueNavigator(mediaSession) { + @Override + public MediaDescriptionCompat getMediaDescription(Player player, int windowIndex) { + return Video.getMediaDescription(context, currentVideo); + } + }); + mediaSessionConnector.setPlayer(player, null); + + // Audio Focus + AudioAttributes audioAttributes = new AudioAttributes.Builder() + .setUsage(C.USAGE_MEDIA) + .setContentType(C.CONTENT_TYPE_MOVIE) + .build(); + player.setAudioAttributes(audioAttributes,true); + } // pause playback on audio output change @@ -227,4 +256,5 @@ public class VideoPlayerService extends Service { } } + }