Add exoplayer mediasession extension. Set player auto attributes.

This commit is contained in:
Akinwale Ariwodola 2020-05-22 13:01:28 +01:00
parent b51db6439b
commit 94c3f67fe5
3 changed files with 35 additions and 1 deletions

View file

@ -77,6 +77,7 @@ dependencies {
implementation 'com.google.android.exoplayer:exoplayer-dash:2.11.4'
implementation 'com.google.android.exoplayer:exoplayer-ui:2.11.4'
implementation 'com.google.android.exoplayer:extension-cast:2.11.4'
implementation 'com.google.android.exoplayer:extension-mediasession:2.11.4'
implementation 'com.google.android:flexbox:2.0.1'

View file

@ -7,6 +7,7 @@ import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.PictureInPictureParams;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
@ -23,6 +24,7 @@ import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.provider.MediaStore;
import android.support.v4.media.session.MediaSessionCompat;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Base64;
@ -40,6 +42,7 @@ import android.widget.Toast;
import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.ext.cast.CastPlayer;
import com.google.android.exoplayer2.ext.mediasession.MediaSessionConnector;
import com.google.android.exoplayer2.ui.PlayerView;
import com.google.android.exoplayer2.upstream.cache.Cache;
import com.google.android.gms.cast.framework.CastContext;
@ -67,6 +70,7 @@ import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.media.session.MediaButtonReceiver;
import androidx.preference.PreferenceManager;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.appcompat.app.AppCompatActivity;
@ -282,6 +286,7 @@ public class MainActivity extends AppCompatActivity implements SdkStatusListener
private static boolean appStarted;
private boolean serviceRunning;
private CheckSdkReadyTask checkSdkReadyTask;
private MediaSessionCompat mediaSession;
private boolean receivedStopService;
private ActionBarDrawerToggle toggle;
private SyncSetTask syncSetTask = null;
@ -781,6 +786,9 @@ public class MainActivity extends AppCompatActivity implements SdkStatusListener
if (dbHelper != null) {
dbHelper.close();
}
if (mediaSession != null) {
mediaSession.release();
}
stopExoplayer();
nowPlayingClaim = null;
nowPlayingClaimUrl = null;
@ -1413,6 +1421,14 @@ public class MainActivity extends AppCompatActivity implements SdkStatusListener
initFloatingWalletBalance();
}
public void initMediaSession() {
ComponentName mediaButtonReceiver = new ComponentName(getApplicationContext(), MediaButtonReceiver.class);
mediaSession = new MediaSessionCompat(getApplicationContext(), "LBRYMediaSession", mediaButtonReceiver, null);
MediaSessionConnector connector = new MediaSessionConnector(mediaSession);
connector.setPlayer(MainActivity.appPlayer);
mediaSession.setActive(true);
}
public void showFloatingWalletBalance() {
findViewById(R.id.floating_balance_main_container).setVisibility(View.VISIBLE);
}

View file

@ -3,6 +3,7 @@ package io.lbry.browser.ui.findcontent;
import android.Manifest;
import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
@ -12,6 +13,7 @@ import android.graphics.Color;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.media.session.MediaSessionCompat;
import android.text.format.DateUtils;
import android.view.ContextMenu;
import android.view.LayoutInflater;
@ -32,6 +34,7 @@ import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.content.ContextCompat;
import androidx.media.session.MediaButtonReceiver;
import androidx.preference.PreferenceManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
@ -42,7 +45,9 @@ import com.google.android.exoplayer2.C;
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.database.ExoDatabaseProvider;
import com.google.android.exoplayer2.ext.mediasession.MediaSessionConnector;
import com.google.android.exoplayer2.extractor.DefaultExtractorsFactory;
import com.google.android.exoplayer2.source.MediaSource;
import com.google.android.exoplayer2.source.ProgressiveMediaSource;
@ -1213,8 +1218,20 @@ public class FileViewFragment extends BaseFragment implements
Context context = getContext();
if (MainActivity.appPlayer == null && context != null) {
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setUsage(C.USAGE_MEDIA)
.setContentType(C.CONTENT_TYPE_MOVIE)
.build();
MainActivity.appPlayer = new SimpleExoPlayer.Builder(context).build();
MainActivity.playerCache = new SimpleCache(context.getCacheDir(), new LeastRecentlyUsedCacheEvictor(1024 * 1024 * 256), new ExoDatabaseProvider(context));
MainActivity.appPlayer.setAudioAttributes(audioAttributes, true);
MainActivity.playerCache =
new SimpleCache(context.getCacheDir(),
new LeastRecentlyUsedCacheEvictor(1024 * 1024 * 256), new ExoDatabaseProvider(context));
if (context instanceof MainActivity) {
((MainActivity) context).initMediaSession();
}
newPlayerCreated = true;
}