Skip to content

Commit

Permalink
android: further refactor audio items
Browse files Browse the repository at this point in the history
  • Loading branch information
puckey committed Dec 4, 2024
1 parent 69989cb commit c06874e
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
package com.doublesymmetry.kotlinaudio.models

import android.content.Context
import android.graphics.Bitmap
import android.net.Uri
import android.os.Bundle
import androidx.media3.common.MediaItem
import androidx.media3.common.MediaMetadata
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

open class AudioItem(
open var audioUrl: String,
Expand All @@ -20,7 +16,7 @@ open class AudioItem(
open val options: AudioItemOptions? = null,
open val mediaId: String? = null
) {
fun asMediaItem(): MediaItem {
fun toMediaItem(): MediaItem {
val extras = Bundle().apply {
options?.headers?.let {
putSerializable("headers", HashMap(it))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ abstract class BaseAudioPlayer internal constructor(
* @param item The [AudioItem] to replace the current one.
*/
open fun load(item: AudioItem) {
exoPlayer.addMediaItem(item.asMediaItem())
exoPlayer.addMediaItem(item.toMediaItem())
exoPlayer.prepare()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import androidx.media3.common.C
import androidx.media3.common.IllegalSeekPositionException
import androidx.media3.common.MediaItem
import androidx.media3.common.Player
import androidx.media3.common.util.UnstableApi
import com.doublesymmetry.kotlinaudio.models.*
import java.util.*
import kotlin.math.max
Expand Down Expand Up @@ -92,7 +91,7 @@ class QueuedAudioPlayer(
if (queue.isEmpty()) {
add(item)
} else {
exoPlayer.addMediaItem(currentIndex + 1, item.asMediaItem())
exoPlayer.addMediaItem(currentIndex + 1, item.toMediaItem())
exoPlayer.removeMediaItem(currentIndex)
exoPlayer.seekTo(currentIndex, C.TIME_UNSET)
exoPlayer.prepare()
Expand All @@ -113,7 +112,7 @@ class QueuedAudioPlayer(
* @param item The [AudioItem] to add.
*/
fun add(item: AudioItem) {
val mediaSource = item.asMediaItem()
val mediaSource = item.toMediaItem()
queue.add(mediaSource)
exoPlayer.addMediaItem(mediaSource)
exoPlayer.prepare()
Expand All @@ -134,7 +133,7 @@ class QueuedAudioPlayer(
* @param items The [AudioItem]s to add.
*/
fun add(items: List<AudioItem>) {
val mediaItems = items.map { it.asMediaItem() }
val mediaItems = items.map { it.toMediaItem() }
queue.addAll(mediaItems)
exoPlayer.addMediaItems(mediaItems)
exoPlayer.prepare()
Expand All @@ -147,7 +146,7 @@ class QueuedAudioPlayer(
* @param atIndex Index to insert items at, if no items loaded this will not automatically start playback.
*/
fun add(items: List<AudioItem>, atIndex: Int) {
val mediaItems = items.map { (it).asMediaItem() }
val mediaItems = items.map { (it).toMediaItem() }
queue.addAll(atIndex, mediaItems)
exoPlayer.addMediaItems(atIndex, mediaItems)
exoPlayer.prepare()
Expand Down Expand Up @@ -233,7 +232,7 @@ class QueuedAudioPlayer(
* Replaces item at index in queue.
*/
fun replaceItem(index: Int, item: AudioItem) {
val mediaItem = item.asMediaItem()
val mediaItem = item.toMediaItem()
queue[index] = mediaItem
exoPlayer.replaceMediaItem(index, mediaItem)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,18 @@ class Track
}

fun toAudioItem(): TrackAudioItem {
return TrackAudioItem(this, type, uri.toString(), artist, title, album, artwork.toString(), duration,
AudioItemOptions(headers, userAgent, resourceId), mediaId)
return TrackAudioItem(
track = this,
type = type,
audioUrl = uri.toString(),
artist = artist,
title = title,
albumTitle = album,
artwork = artwork.toString(),
duration = duration,
options = AudioItemOptions(headers, userAgent, resourceId),
mediaId = mediaId
)
}

init {
Expand Down

0 comments on commit c06874e

Please sign in to comment.