-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added player service for future use
Signed-off-by: Gabriel Fontán <[email protected]>
- Loading branch information
Showing
17 changed files
with
601 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
plugins { | ||
alias(libs.plugins.androidLibrary) | ||
alias(libs.plugins.kotlinAndroid) | ||
alias(libs.plugins.kotlin.ksp) | ||
} | ||
|
||
android { | ||
namespace = "com.bobbyesp.mediaplayer" | ||
compileSdk = 34 | ||
|
||
defaultConfig { | ||
minSdk = 24 | ||
|
||
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" | ||
consumerProguardFiles("consumer-rules.pro") | ||
} | ||
|
||
buildTypes { | ||
release { | ||
isMinifyEnabled = false | ||
proguardFiles( | ||
getDefaultProguardFile("proguard-android-optimize.txt"), | ||
"proguard-rules.pro" | ||
) | ||
} | ||
} | ||
compileOptions { | ||
sourceCompatibility = JavaVersion.VERSION_17 | ||
targetCompatibility = JavaVersion.VERSION_17 | ||
} | ||
kotlinOptions { | ||
jvmTarget = "17" | ||
} | ||
} | ||
|
||
dependencies { | ||
implementation(libs.core.ktx) | ||
implementation(libs.core.appcompat) | ||
implementation(libs.androidx.legacy.support.v4) // Needed MediaSessionCompat.Token | ||
|
||
//DI (Dependency Injection - Hilt) | ||
implementation(libs.bundles.hilt) | ||
ksp(libs.hilt.ext.compiler) | ||
ksp(libs.hilt.compiler) | ||
|
||
//Media3 | ||
implementation(libs.bundles.media3) | ||
|
||
//Coil | ||
implementation(libs.coil) | ||
|
||
testImplementation(libs.junit) | ||
androidTestImplementation(libs.androidx.test.ext.junit) | ||
androidTestImplementation(libs.espresso.core) | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Add project specific ProGuard rules here. | ||
# You can control the set of applied configuration files using the | ||
# proguardFiles setting in build.gradle. | ||
# | ||
# For more details, see | ||
# http://developer.android.com/guide/developing/tools/proguard.html | ||
|
||
# If your project uses WebView with JS, uncomment the following | ||
# and specify the fully qualified class name to the JavaScript interface | ||
# class: | ||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview { | ||
# public *; | ||
#} | ||
|
||
# Uncomment this to preserve the line number information for | ||
# debugging stack traces. | ||
#-keepattributes SourceFile,LineNumberTable | ||
|
||
# If you keep the line number information, uncomment this to | ||
# hide the original source file name. | ||
#-renamesourcefileattribute SourceFile |
22 changes: 22 additions & 0 deletions
22
app/mediaplayer/src/androidTest/java/com/bobbyesp/mediaplayer/ExampleInstrumentedTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.bobbyesp.mediaplayer | ||
|
||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import androidx.test.platform.app.InstrumentationRegistry | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
|
||
/** | ||
* Instrumented test, which will execute on an Android device. | ||
* | ||
* See [testing documentation](http://d.android.com/tools/testing). | ||
*/ | ||
@RunWith(AndroidJUnit4::class) | ||
class ExampleInstrumentedTest { | ||
@Test | ||
fun useAppContext() { | ||
// Context of the app under test. | ||
val appContext = InstrumentationRegistry.getInstrumentation().targetContext | ||
assertEquals("com.bobbyesp.mediaplayer.test", appContext.packageName) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest> | ||
|
||
</manifest> |
64 changes: 64 additions & 0 deletions
64
app/mediaplayer/src/main/java/com/bobbyesp/mediaplayer/di/MediaPlayerModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.bobbyesp.mediaplayer.di | ||
|
||
import android.content.Context | ||
import android.os.Build | ||
import androidx.annotation.RequiresApi | ||
import androidx.media3.common.AudioAttributes | ||
import androidx.media3.common.C | ||
import androidx.media3.common.util.UnstableApi | ||
import androidx.media3.exoplayer.ExoPlayer | ||
import androidx.media3.exoplayer.trackselection.DefaultTrackSelector | ||
import androidx.media3.session.MediaSession | ||
import com.bobbyesp.mediaplayer.service.MediaServiceHandler | ||
import com.bobbyesp.mediaplayer.service.notifications.MediaNotificationManager | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.android.qualifiers.ApplicationContext | ||
import dagger.hilt.components.SingletonComponent | ||
import javax.inject.Singleton | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
object MediaPlayerModule { | ||
@Provides | ||
@Singleton | ||
fun provideAudioAttributes(): AudioAttributes = | ||
AudioAttributes.Builder().setContentType(C.AUDIO_CONTENT_TYPE_MOVIE).setUsage(C.USAGE_MEDIA) | ||
.build() | ||
|
||
@Provides | ||
@Singleton | ||
@UnstableApi | ||
fun providePlayer( | ||
@ApplicationContext context: Context, audioAttributes: AudioAttributes | ||
): ExoPlayer = ExoPlayer.Builder(context).setAudioAttributes(audioAttributes, true) | ||
.setHandleAudioBecomingNoisy(true).setTrackSelector(DefaultTrackSelector(context)).build() | ||
|
||
@RequiresApi(Build.VERSION_CODES.O) | ||
@Provides | ||
@Singleton | ||
fun provideNotificationManager( | ||
@ApplicationContext context: Context, player: ExoPlayer | ||
): MediaNotificationManager = MediaNotificationManager( | ||
context = context, player = player | ||
) | ||
|
||
@Provides | ||
@Singleton | ||
fun provideMediaSession( | ||
@ApplicationContext context: Context, | ||
player: ExoPlayer | ||
): MediaSession = | ||
MediaSession.Builder(context, player).build() | ||
|
||
@Provides | ||
@Singleton | ||
fun provideServiceHandler( | ||
player: ExoPlayer | ||
): MediaServiceHandler = | ||
MediaServiceHandler( | ||
player = player | ||
) | ||
|
||
} |
49 changes: 49 additions & 0 deletions
49
app/mediaplayer/src/main/java/com/bobbyesp/mediaplayer/service/MediaService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.bobbyesp.mediaplayer.service | ||
|
||
import android.content.Intent | ||
import android.os.Build | ||
import androidx.annotation.RequiresApi | ||
import androidx.media3.common.Player | ||
import androidx.media3.common.util.UnstableApi | ||
import androidx.media3.session.MediaSession | ||
import androidx.media3.session.MediaSessionService | ||
import com.bobbyesp.mediaplayer.service.notifications.MediaNotificationManager | ||
import dagger.hilt.android.AndroidEntryPoint | ||
import javax.inject.Inject | ||
|
||
@AndroidEntryPoint | ||
class MediaService : MediaSessionService() { | ||
|
||
@Inject | ||
lateinit var mediaSession: MediaSession | ||
|
||
@Inject | ||
lateinit var notificationManager: MediaNotificationManager | ||
|
||
@RequiresApi(Build.VERSION_CODES.O) | ||
@UnstableApi | ||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { | ||
notificationManager.startNotificationService( | ||
mediaSessionService = this, | ||
mediaSession = mediaSession | ||
) | ||
|
||
return super.onStartCommand(intent, flags, startId) | ||
} | ||
|
||
override fun onDestroy() { | ||
super.onDestroy() | ||
mediaSession.run { | ||
release() | ||
if (player.playbackState != Player.STATE_IDLE) { | ||
player.seekTo(0) | ||
player.playWhenReady = false | ||
player.stop() | ||
} | ||
} | ||
} | ||
|
||
override fun onGetSession(controllerInfo: MediaSession.ControllerInfo): MediaSession { | ||
return mediaSession | ||
} | ||
} |
Oops, something went wrong.