Skip to content

Commit

Permalink
ExoPlayer module migrated to androidx.media3
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreyPavlenko committed Jan 10, 2024
1 parent f929ade commit 27530ea
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 186 deletions.
6 changes: 2 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
ext {
def abi = project.properties['ABI']
VERSION_CODE = 234
VERSION_NAME = "1.9.6"
VERSION_CODE = 235
VERSION_NAME = "1.9.7"
SDK_MIN_VERSION = 23
SDK_TARGET_VERSION = 34
SDK_COMPILE_VERSION = 34
Expand Down Expand Up @@ -95,7 +95,6 @@ subprojects {
compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
if (name == "exoplayer") coreLibraryDesugaringEnabled true
}

testOptions {
Expand All @@ -107,7 +106,6 @@ subprojects {
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
if (name == "exoplayer") coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:2.0.4'
}
}

Expand Down
1 change: 0 additions & 1 deletion depends/.gitignore

This file was deleted.

118 changes: 0 additions & 118 deletions depends/build.sh

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,10 @@ public MediaSessionCallback(FermataMediaService service, MediaSessionCompat sess
ctx.getString(R.string.repeat), R.drawable.repeat).build();
customRepeatDisable =
new PlaybackStateCompat.CustomAction.Builder(CUSTOM_ACTION_REPEAT_DISABLE,
ctx.getString(R.string.repeat_disable), R.drawable.repeat_filled).build();
ctx.getString(R.string.repeat_disable), R.drawable.repeat_filled).build();
customShuffleEnable =
new PlaybackStateCompat.CustomAction.Builder(CUSTOM_ACTION_SHUFFLE_ENABLE,
ctx.getString(R.string.shuffle), R.drawable.shuffle).build();
ctx.getString(R.string.shuffle), R.drawable.shuffle).build();
customShuffleDisable =
new PlaybackStateCompat.CustomAction.Builder(CUSTOM_ACTION_SHUFFLE_DISABLE,
ctx.getString(R.string.shuffle_disable), R.drawable.shuffle_filled).build();
Expand Down Expand Up @@ -1234,6 +1234,8 @@ private float getSpeed(PlayableItem i) {
}

private void start(MediaEngine engine, float speed) {
Log.i("Start playing ", engine.getSource().getLocation(), " with ",
engine.getClass().getSimpleName());
engine.setSpeed(speed);
engine.start();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@ protected void buildPlayableMenu(MainActivityDelegate a, OverlayMenu.Builder b,

if (addMediaEngMenu()) {
b.addItem(R.id.preferred_media_engine, R.drawable.media_engine,
R.string.preferred_media_engine).setSubmenu(this::buildVideoEngMenu);
R.string.preferred_media_engine)
.setSubmenu(pi.isVideo() ? this::buildVideoEngMenu : this::buildAudioEngMenu);
}
}

Expand Down
9 changes: 4 additions & 5 deletions modules/exoplayer/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ android {
dependencies {
implementation project(':utils')
implementation project(':fermata')
implementation project(':exoplayer-library-core')
implementation project(':exoplayer-library-hls')
implementation project(':exoplayer-extension-ffmpeg')
implementation project(':exoplayer-extension-flac')
implementation project(':exoplayer-extension-opus')
implementation("androidx.media3:media3-exoplayer:1.2.0")
implementation("androidx.media3:media3-exoplayer-hls:1.2.0")
implementation("androidx.media3:media3-exoplayer-dash:1.2.0")
implementation("androidx.media3:media3-datasource-cronet:1.2.0")
}
Original file line number Diff line number Diff line change
@@ -1,28 +1,39 @@
package me.aap.fermata.engine.exoplayer;

import static com.google.android.exoplayer2.DefaultRenderersFactory.EXTENSION_RENDERER_MODE_PREFER;
import static me.aap.utils.async.Completed.completed;

import android.annotation.SuppressLint;
import android.content.Context;
import android.net.Uri;

import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.DefaultRenderersFactory;
import com.google.android.exoplayer2.ExoPlayer;
import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.MediaItem;
import com.google.android.exoplayer2.PlaybackException;
import com.google.android.exoplayer2.PlaybackParameters;
import com.google.android.exoplayer2.Player;
import com.google.android.exoplayer2.source.ProgressiveMediaSource;
import com.google.android.exoplayer2.source.hls.HlsMediaSource;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
import com.google.android.exoplayer2.util.Util;
import com.google.android.exoplayer2.video.VideoSize;
import androidx.annotation.NonNull;
import androidx.media3.common.C;
import androidx.media3.common.Format;
import androidx.media3.common.MediaItem;
import androidx.media3.common.PlaybackException;
import androidx.media3.common.PlaybackParameters;
import androidx.media3.common.Player;
import androidx.media3.common.VideoSize;
import androidx.media3.common.util.UnstableApi;
import androidx.media3.common.util.Util;
import androidx.media3.datasource.DataSource;
import androidx.media3.datasource.DefaultDataSource;
import androidx.media3.datasource.DefaultHttpDataSource;
import androidx.media3.datasource.cronet.CronetDataSource;
import androidx.media3.datasource.cronet.CronetUtil;
import androidx.media3.exoplayer.ExoPlayer;
import androidx.media3.exoplayer.source.DefaultMediaSourceFactory;
import androidx.media3.exoplayer.source.MediaSource;

import org.chromium.net.CronetEngine;

import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.CookiePolicy;
import java.util.concurrent.Executors;

import me.aap.fermata.BuildConfig;
import me.aap.fermata.FermataApplication;
import me.aap.fermata.media.engine.AudioEffects;
import me.aap.fermata.media.engine.MediaEngineBase;
import me.aap.fermata.media.lib.MediaLib.PlayableItem;
Expand All @@ -33,26 +44,38 @@
/**
* @author Andrey Pavlenko
*/
@UnstableApi
public class ExoPlayerEngine extends MediaEngineBase implements Player.Listener {
private final Context ctx;
private static final DataSource.Factory httpDsFactory;

static {
CronetEngine cre = CronetUtil.buildCronetEngine(FermataApplication.get(),
"Fermata/" + BuildConfig.VERSION_NAME, true);
if (cre != null) {
httpDsFactory = new CronetDataSource.Factory(cre, Executors.newSingleThreadExecutor());
} else {
CookieManager cookieManager = new CookieManager();
cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ORIGINAL_SERVER);
CookieHandler.setDefault(cookieManager);
httpDsFactory = new DefaultHttpDataSource.Factory();
}
}

private final ExoPlayer player;
private final AudioEffects audioEffects;
private final DataSource.Factory dsFactory;
private ProgressiveMediaSource.Factory progressive;
private HlsMediaSource.Factory hls;
private PlayableItem source;
private boolean preparing;
private boolean buffering;
private boolean isHls;

public ExoPlayerEngine(Context ctx, Listener listener) {
super(listener);
this.ctx = ctx;
player = new ExoPlayer.Builder(ctx, new DefaultRenderersFactory(ctx).setExtensionRendererMode(
EXTENSION_RENDERER_MODE_PREFER)).build();
DefaultDataSource.Factory dsFactory = new DefaultDataSource.Factory(ctx, httpDsFactory);
MediaSource.Factory msFactory =
new DefaultMediaSourceFactory(ctx).setDataSourceFactory(dsFactory);
player = new ExoPlayer.Builder(ctx).setMediaSourceFactory(msFactory).build();
player.addListener(this);
audioEffects = AudioEffects.create(0, player.getAudioSessionId());
dsFactory = new DefaultDataSourceFactory(ctx, "Fermata/" + BuildConfig.VERSION_NAME);
}

@Override
Expand All @@ -70,32 +93,11 @@ public void prepare(PlayableItem source) {

Uri uri = source.getLocation();
MediaItem m = MediaItem.fromUri(uri);
int type = Util.inferContentType(uri, null);

switch (type) {
case C.TYPE_HLS -> {
if (hls == null) hls = new HlsMediaSource.Factory(getDsFactory(source));
isHls = true;
player.setMediaSource(hls.createMediaSource(m), true);
}
case C.TYPE_OTHER -> {
if (progressive == null)
progressive = new ProgressiveMediaSource.Factory(getDsFactory(source));
isHls = false;
player.setMediaSource(progressive.createMediaSource(m), true);
}
default ->
listener.onEngineError(this, new IllegalArgumentException("Unsupported type: " + type));
}

isHls = Util.inferContentType(uri) == C.CONTENT_TYPE_HLS;
player.setMediaItem(m);
player.prepare();
}

private DataSource.Factory getDsFactory(PlayableItem source) {
String agent = source.getUserAgent();
return (agent == null) ? dsFactory : new DefaultDataSourceFactory(ctx, agent);
}

@Override
public void start() {
player.setPlayWhenReady(true);
Expand Down Expand Up @@ -230,7 +232,7 @@ public void onVideoSizeChanged(VideoSize videoSize) {
}

@Override
public void onPlayerError(PlaybackException error) {
public void onPlayerError(@NonNull PlaybackException error) {
listener.onEngineError(this, error);
}
}
8 changes: 0 additions & 8 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ gradle.ext.localProps = new Properties()
gradle.ext.enableGoogleServices = (System.getenv('NO_GS') == null)
&& file('fermata/google-services.json').isFile()
def localPropsFile = file('local.properties')
def exoRoot = file('depends/ExoPlayer')

if (localPropsFile.isFile()) {
gradle.ext.localProps.load(localPropsFile.newDataInputStream())
Expand All @@ -15,12 +14,6 @@ if (localPropsFile.isFile()) {
gradle.ext.localProps.setProperty('storePassword', 'fermata')
}

if (exoRoot.isDirectory()) {
gradle.ext.exoplayerRoot = exoRoot
gradle.ext.exoplayerModulePrefix = 'exoplayer-'
apply from: new File(exoRoot, 'core_settings.gradle')
}

rootProject.name = 'Fermata Music Player'
include ':fermata'
include ':control'
Expand All @@ -29,7 +22,6 @@ project(':utils').projectDir = file('depends/utils')

file('modules').eachDir {
def name = ':' + it.name
if ((':exoplayer' == name) && !exoRoot.isDirectory()) return
if ((':gdrive' == name) && !gradle.ext.enableGoogleServices) return
gradle.ext.modules.add(name)
include name
Expand Down

0 comments on commit 27530ea

Please sign in to comment.