Skip to content

Commit

Permalink
iOS/Android shared sources
Browse files Browse the repository at this point in the history
  • Loading branch information
dasdranagon committed Jan 7, 2025
1 parent 980eeca commit 44f67b1
Show file tree
Hide file tree
Showing 12 changed files with 40 additions and 90 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import kotlin.time.Duration.Companion.minutes

class MediaSoundLoopPlayer(
private val coroutineScope: CoroutineScope,
private val mediaSource: MediaSource,
private val mediaSource: MediaSource.Local,
) {

private var player: DefaultSoundPlayer? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
package com.splendo.kaluga.example.shared.viewmodel.media

import com.splendo.kaluga.media.MediaSource
import com.splendo.kaluga.media.mediaSourceFromLocalFile

expect object SoundsSources {
val beep: MediaSource
object SoundsSources {
//TODO: Throw correct error
val beep: MediaSource.Local = mediaSourceFromLocalFile("sound", "mp3") ?: error("")
}

This file was deleted.

2 changes: 1 addition & 1 deletion media/src/androidMain/kotlin/DefaultMediaManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ actual class DefaultMediaManager(mediaSurfaceProvider: MediaSurfaceProvider?, co
} else {
mediaPlayer.setDataSource(source.context, source.uri, source.headers)
}
is MediaSource.Id -> TODO()
is MediaSource.Bundle -> TODO()
}
DefaultPlayableMedia(source, mediaPlayer)
} catch (e: Throwable) {
Expand Down
25 changes: 11 additions & 14 deletions media/src/androidMain/kotlin/DefaultSoundPlayer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import android.media.AudioAttributes
import android.media.SoundPool
import com.splendo.kaluga.base.ApplicationHolder

actual class DefaultSoundPlayer actual constructor(source: MediaSource) : SoundPlayer {
actual class DefaultSoundPlayer actual constructor(source: MediaSource.Local) : SoundPlayer {

private val soundPool = SoundPool.Builder().apply {
val attributes = AudioAttributes.Builder().apply {
Expand All @@ -40,19 +40,16 @@ actual class DefaultSoundPlayer actual constructor(source: MediaSource) : SoundP
soundPool.release()
}

// private fun SoundPool.load(source: MediaSource): Int = if (source is MediaSource.Url) load(source.url) else throw MediaSoundError.UnexpectedMediaSourceShouldBeURL
private fun SoundPool.load(source: MediaSource): Int = when (source) {
is MediaSource.Url -> TODO()
is MediaSource.Asset -> TODO()
is MediaSource.File -> TODO()
is MediaSource.Content -> TODO()
is MediaSource.Id -> load(
ApplicationHolder.applicationContext,
ApplicationHolder.applicationContext.resources.getIdentifier(source.id, "raw", ApplicationHolder.applicationContext.packageName),
1,
)
private fun SoundPool.load(source: MediaSource.Local): Int = when (source) {
is MediaSource.Asset -> load(source.descriptor, 1)
is MediaSource.File -> load(source.descriptor, source.offset, source.length, 1)
is MediaSource.Bundle -> ApplicationHolder.applicationContext.let { context ->
load(
context,
context.resources.getIdentifier(source.fileName, source.defType, context.packageName),
1,
)
}
else -> throw MediaSoundError.UnexpectedMediaSourceShouldBeId
}

// private fun SoundPool.load(url: URL): Int = if (url.path != null) load(url.path, 1) else throw MediaSoundError.CannotAccessMediaSource
}
8 changes: 5 additions & 3 deletions media/src/androidMain/kotlin/MediaSource.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ actual sealed class MediaSource {
* A [MediaSource] that has an associated [AssetFileDescriptor]
* @property descriptor the [AssetFileDescriptor] associated with the media source
*/
data class Asset(val descriptor: AssetFileDescriptor) : MediaSource()
data class Asset(val descriptor: AssetFileDescriptor) : Local()

/**
* A [MediaSource] that has an associated [FileDescriptor]
* @property descriptor the [FileDescriptor] associated with the media source
*/
data class File(val descriptor: FileDescriptor) : MediaSource()
data class File(val descriptor: FileDescriptor, val offset: Long, val length: Long) : Local()

/**
* A [MediaSource] that is located at a [URL]
Expand All @@ -66,7 +66,7 @@ actual sealed class MediaSource {
val cookies: List<HttpCookie>? = null,
) : MediaSource()

data class Id(val id: String) : MediaSource()
data class Bundle(val fileName: String, val defType: String = "raw") : Local()
}

/**
Expand All @@ -79,3 +79,5 @@ actual fun mediaSourceFromUrl(url: String): MediaSource? = try {
} catch (e: MalformedURLException) {
null
}

actual fun mediaSourceFromLocalFile(fileName: String, fileType: String): MediaSource.Local? = MediaSource.Bundle(fileName)
3 changes: 1 addition & 2 deletions media/src/commonMain/kotlin/MediaSource.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,4 @@ expect sealed class MediaSource {
*/
expect fun mediaSourceFromUrl(url: String): MediaSource?

// TODO: Implement it
// expect fun mediaSourceFromLocalFile(fileName: String, fileType: String): MediaSource.Local?
expect fun mediaSourceFromLocalFile(fileName: String, fileType: String): MediaSource.Local?
2 changes: 1 addition & 1 deletion media/src/commonMain/kotlin/SoundPlayer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ interface SoundPlayer : AutoCloseable {
/**
* A default implementation of [SoundPlayer]
*/
expect class DefaultSoundPlayer(source: MediaSource) : SoundPlayer {
expect class DefaultSoundPlayer(source: MediaSource.Local) : SoundPlayer {
override fun close()
override fun play()
}
1 change: 1 addition & 0 deletions media/src/iosMain/kotlin/DefaultMediaManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ actual class DefaultMediaManager(mediaSurfaceProvider: MediaSurfaceProvider?, pr
private val MediaSource.avPlayerItem: AVPlayerItem get() = when (this) {
is MediaSource.Asset -> AVPlayerItem(asset)
is MediaSource.URL -> AVPlayerItem(AVURLAsset.URLAssetWithURL(url, options.associate { it.entry }))
is MediaSource.Bundle -> TODO()
}

actual override suspend fun renderVideoOnSurface(surface: MediaSurface?) {
Expand Down
15 changes: 11 additions & 4 deletions media/src/iosMain/kotlin/DefaultSoundPlayer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import platform.AVFAudio.AVAudioFile
import platform.AVFAudio.AVAudioSession
import platform.AVFAudio.AVAudioSessionCategoryPlayback
import platform.AVFAudio.AVAudioSessionRouteChangeNotification
import platform.Foundation.NSBundle
import platform.Foundation.NSError
import platform.Foundation.NSNotification
import platform.Foundation.NSNotificationCenter
Expand All @@ -41,9 +42,8 @@ import platform.Foundation.NSURL
import platform.UIKit.UIApplicationDidEnterBackgroundNotification
import platform.UIKit.UIApplicationWillEnterForegroundNotification

actual class DefaultSoundPlayer actual constructor(source: MediaSource) : SoundPlayer {
private val url = if (source is MediaSource.URL) source.url else throw MediaSoundError.UnexpectedMediaSourceShouldBeURL
private val file = accessFile(url)
actual class DefaultSoundPlayer actual constructor(source: MediaSource.Local) : SoundPlayer {
private val file = accessFile(source)

init {
observeNotifications()
Expand Down Expand Up @@ -167,8 +167,15 @@ actual class DefaultSoundPlayer actual constructor(source: MediaSource) : SoundP
onNotification,
)

private fun accessFile(url: NSURL): AVAudioFile = execute(
private fun accessFile(source: MediaSource.Local): AVAudioFile = execute(
block = { errorPtr ->
val path = when (source) {
is MediaSource.Bundle -> NSBundle.mainBundle.pathForResource(source.fileName, source.fileType)
else -> error("Should be bundle")
}

require(path != null) { "Invalid file for sound" }
val url = NSURL.fileURLWithPath(path)
AVAudioFile(forReading = url, error = errorPtr)
},
handleError = { error ->
Expand Down
4 changes: 4 additions & 0 deletions media/src/iosMain/kotlin/MediaSource.kt
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ actual sealed class MediaSource {
}
}
}

data class Bundle(val fileName: String, val fileType: String) : Local()
}

/**
Expand All @@ -144,3 +146,5 @@ actual sealed class MediaSource {
*/
actual fun mediaSourceFromUrl(url: String): MediaSource? =
NSURL.URLWithString(url)?.let { MediaSource.URL(it, options = listOf(MediaSource.URL.Option.PreferPreciseDurationAndTiming(true))) }

actual fun mediaSourceFromLocalFile(fileName: String, fileType: String): MediaSource.Local? = MediaSource.Bundle(fileName, fileType)

0 comments on commit 44f67b1

Please sign in to comment.