Skip to content

Commit

Permalink
Better music service, fixed "collect" bug
Browse files Browse the repository at this point in the history
  • Loading branch information
maxrave-dev committed Oct 8, 2023
1 parent a7bf7d6 commit 972b686
Show file tree
Hide file tree
Showing 12 changed files with 442 additions and 421 deletions.
1 change: 1 addition & 0 deletions app/src/main/java/com/maxrave/simpmusic/common/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ object Config {
const val RADIO_CLICK = "RADIO_CLICK"
const val MINIPLAYER_CLICK = "MINIPLAYER_CLICK"
const val SHARE = "SHARE"
const val RECOVER_TRACK_QUEUE = "RECOVER_TRACK_QUEUE"

const val VN = "VN"

Expand Down
54 changes: 51 additions & 3 deletions app/src/main/java/com/maxrave/simpmusic/di/MusicServiceModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,27 @@ package com.maxrave.simpmusic.di
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import androidx.core.net.toUri
import androidx.media3.common.AudioAttributes
import androidx.media3.common.C
import androidx.media3.common.util.UnstableApi
import androidx.media3.database.DatabaseProvider
import androidx.media3.database.StandaloneDatabaseProvider
import androidx.media3.datasource.DataSource
import androidx.media3.datasource.DataSpec
import androidx.media3.datasource.DefaultHttpDataSource
import androidx.media3.datasource.ResolvingDataSource
import androidx.media3.datasource.cache.CacheDataSource
import androidx.media3.datasource.cache.CacheDataSource.FLAG_IGNORE_CACHE_ON_ERROR
import androidx.media3.datasource.cache.NoOpCacheEvictor
import androidx.media3.datasource.cache.SimpleCache
import androidx.media3.exoplayer.ExoPlayer
import androidx.media3.exoplayer.source.DefaultMediaSourceFactory
import androidx.media3.exoplayer.trackselection.DefaultTrackSelector
import androidx.media3.extractor.mkv.MatroskaExtractor
import androidx.media3.extractor.mp4.FragmentedMp4Extractor
import androidx.media3.session.MediaSession
import com.maxrave.simpmusic.common.QUALITY
import com.maxrave.simpmusic.data.dataStore.DataStoreManager
import com.maxrave.simpmusic.data.repository.MainRepository
import com.maxrave.simpmusic.service.SimpleMediaServiceHandler
Expand All @@ -28,6 +35,8 @@ import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.runBlocking
import javax.inject.Qualifier
import javax.inject.Singleton

Expand Down Expand Up @@ -100,14 +109,53 @@ object MusicServiceModule {
.setFlags(FLAG_IGNORE_CACHE_ON_ERROR)
}

// @Provides
// @Singleton
// @UnstableApi
// fun provideMediaSourceFactory(
// @ApplicationContext context: Context,
// cacheDataSourceFactory: CacheDataSource.Factory
// ): DefaultMediaSourceFactory =
// DefaultMediaSourceFactory(context).setDataSourceFactory(cacheDataSourceFactory)

@Provides
@Singleton
@UnstableApi
fun provideResolvingDataSourceFactory(
cacheDataSourceFactory: CacheDataSource.Factory,
@DownloadCache downloadCache: SimpleCache, @PlayerCache playerCache: SimpleCache, mainRepository: MainRepository, dataStoreManager: DataStoreManager
): DataSource.Factory {
return ResolvingDataSource.Factory(cacheDataSourceFactory) { dataSpec ->
val mediaId = dataSpec.key ?: error("No media id")
val CHUNK_LENGTH = 512 * 1024L
if (downloadCache.isCached(mediaId, dataSpec.position, if (dataSpec.length >= 0) dataSpec.length else 1) || playerCache.isCached(mediaId, dataSpec.position, CHUNK_LENGTH)) {
return@Factory dataSpec
}
var dataSpecReturn: DataSpec? = null
runBlocking {
val itag = dataStoreManager.quality.first()

mainRepository.getStream(mediaId, if (itag == QUALITY.items[0].toString()) QUALITY.itags[0] else QUALITY.itags[1]).collect {
if (it != null) {
dataSpecReturn = dataSpec.withUri(it.toUri())
}
}
}
return@Factory dataSpecReturn!!
}
}

@Provides
@Singleton
@UnstableApi
fun provideMediaSourceFactory(
@ApplicationContext context: Context,
cacheDataSourceFactory: CacheDataSource.Factory
dataSourceFactory: DataSource.Factory
): DefaultMediaSourceFactory =
DefaultMediaSourceFactory(context).setDataSourceFactory(cacheDataSourceFactory)
DefaultMediaSourceFactory(
dataSourceFactory
) {
arrayOf(MatroskaExtractor(), FragmentedMp4Extractor())
}

@Provides
@Singleton
Expand Down
28 changes: 28 additions & 0 deletions app/src/main/java/com/maxrave/simpmusic/extension/AllExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ import android.graphics.Color
import android.view.View
import android.view.ViewGroup
import android.widget.ImageButton
import androidx.core.net.toUri
import androidx.datastore.preferences.preferencesDataStore
import androidx.media3.common.MediaItem
import androidx.media3.common.MediaMetadata
import androidx.media3.common.util.UnstableApi
import androidx.sqlite.db.SimpleSQLiteQuery
import com.maxrave.kotlinytmusicscraper.models.SongItem
import com.maxrave.kotlinytmusicscraper.models.VideoItem
Expand Down Expand Up @@ -277,6 +280,31 @@ fun MediaItem?.toSongEntity(): SongEntity? {
downloadState = 0
) else null
}
@UnstableApi
fun Track.toMediaItem() : MediaItem {
return MediaItem.Builder()
.setMediaId(this.videoId)
.setUri(this.videoId)
.setCustomCacheKey(this.videoId)
.setMediaMetadata(
MediaMetadata.Builder()
.setTitle(this.title)
.setArtist(this.artists.toListName().connectArtists())
.setArtworkUri(this.thumbnails?.lastOrNull()?.url?.toUri())
.setAlbumTitle(this.album?.name)
.build()
)
.build()
}

@UnstableApi
fun List<Track>.toMediaItems(): List<MediaItem> {
val listMediaItem = mutableListOf<MediaItem>()
for (item in this) {
listMediaItem.add(item.toMediaItem())
}
return listMediaItem
}

@JvmName("SongResulttoListTrack")
fun ArrayList<SongsResult>.toListTrack(): ArrayList<Track> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -510,13 +510,14 @@ class SimpleMediaServiceHandler @Inject constructor(
)
}
fun release() {
stopBufferedUpdate()
stopProgressUpdate()
sendCloseEqualizerIntent()
player.removeListener(this)
job?.cancel()
sleepTimerJob?.cancel()
volumeNormalizationJob?.cancel()
updateNotificationJob?.cancel()
GlobalScope.cancel()
}

private fun updateNotification() {
Expand Down

This file was deleted.

Loading

0 comments on commit 972b686

Please sign in to comment.