Skip to content

Commit

Permalink
feat: Added player service for future use
Browse files Browse the repository at this point in the history
Signed-off-by: Gabriel Fontán <[email protected]>
  • Loading branch information
BobbyESP committed Apr 1, 2024
1 parent bb12d5d commit 6105118
Show file tree
Hide file tree
Showing 17 changed files with 601 additions and 1 deletion.
1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ val currentVersion: Version = Version.Beta(
versionMajor = 1,
versionMinor = 0,
versionPatch = 0,
versionBuild = 2
versionBuild = 3
)

android {
Expand Down
1 change: 1 addition & 0 deletions app/mediaplayer/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
55 changes: 55 additions & 0 deletions app/mediaplayer/build.gradle.kts
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.
21 changes: 21 additions & 0 deletions app/mediaplayer/proguard-rules.pro
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
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)
}
}
4 changes: 4 additions & 0 deletions app/mediaplayer/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest>

</manifest>
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
)

}
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
}
}
Loading

0 comments on commit 6105118

Please sign in to comment.