Skip to content

Commit

Permalink
Improve errors handling
Browse files Browse the repository at this point in the history
  • Loading branch information
dasdranagon committed Jan 7, 2025
1 parent 44f67b1 commit 1aa4c2a
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@

package com.splendo.kaluga.example.shared.viewmodel.media

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

object SoundsSources {
//TODO: Throw correct error
val beep: MediaSource.Local = mediaSourceFromLocalFile("sound", "mp3") ?: error("")
val beep: MediaSource.Local = mediaSourceFromLocalFile("sound", "mp3") ?: throw MediaSoundError.CannotAccessMediaFile()
}
8 changes: 7 additions & 1 deletion media/src/androidMain/kotlin/DefaultMediaManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ package com.splendo.kaluga.media

import android.media.MediaPlayer
import android.media.PlaybackParams
import android.net.Uri
import android.os.Build
import com.splendo.kaluga.base.ApplicationHolder
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asSharedFlow
Expand Down Expand Up @@ -136,7 +138,11 @@ actual class DefaultMediaManager(mediaSurfaceProvider: MediaSurfaceProvider?, co
} else {
mediaPlayer.setDataSource(source.context, source.uri, source.headers)
}
is MediaSource.Bundle -> TODO()
is MediaSource.Bundle -> {
ApplicationHolder.applicationContext.let { context ->
mediaPlayer.setDataSource(context, Uri.parse("android.resource://" + context.packageName + "/" + source.fileName))
}
}
}
DefaultPlayableMedia(source, mediaPlayer)
} catch (e: Throwable) {
Expand Down
2 changes: 1 addition & 1 deletion media/src/androidMain/kotlin/DefaultSoundPlayer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,6 @@ actual class DefaultSoundPlayer actual constructor(source: MediaSource.Local) :
1,
)
}
else -> throw MediaSoundError.UnexpectedMediaSourceShouldBeId
else -> throw MediaSoundError.UnexpectedMediaSourceShouldBeLocal
}
}
1 change: 1 addition & 0 deletions media/src/androidMain/kotlin/MediaSource.kt
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ actual sealed class MediaSource {
val cookies: List<HttpCookie>? = null,
) : MediaSource()

// TODO: Add kdoc
data class Bundle(val fileName: String, val defType: String = "raw") : Local()
}

Expand Down
4 changes: 1 addition & 3 deletions media/src/commonMain/kotlin/MediaSoundError.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@
package com.splendo.kaluga.media

sealed class MediaSoundError(message: String) : Error(message) {
data object UnexpectedMediaSourceShouldBeURL : MediaSoundError("Unexpected media source type. Should be URL.")
data object UnexpectedMediaSourceShouldBeId : MediaSoundError("Unexpected media source type. Should be Id.")
data object CannotAccessMediaSource : MediaSoundError("Cannot access media source.")
data object UnexpectedMediaSourceShouldBeLocal : MediaSoundError("Unexpected media source type. Should be Local.")
class CannotAccessMediaFile(detailedDescription: String? = null) : MediaSoundError("Cannot access media file. $detailedDescription")
class CannotStartAudioEngine(detailedDescription: String? = null) : MediaSoundError("Cannot start audio engine. $detailedDescription")
class CannotSetAudioSessionConfiguration(detailedDescription: String? = null) : MediaSoundError("Failed to set the audio session configuration. $detailedDescription")
Expand Down
9 changes: 8 additions & 1 deletion media/src/iosMain/kotlin/DefaultMediaManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ import platform.CoreMedia.CMTimeMakeWithSeconds
import platform.CoreMedia.CMTimeSubtract
import platform.CoreMedia.kCMTimeIndefinite
import platform.CoreMedia.kCMTimeZero
import platform.Foundation.NSBundle
import platform.Foundation.NSError
import platform.Foundation.NSKeyValueObservingOptionInitial
import platform.Foundation.NSKeyValueObservingOptionNew
Expand All @@ -100,6 +101,7 @@ import platform.Foundation.NSNotificationCenter
import platform.Foundation.NSNotificationName
import platform.Foundation.NSOperationQueue.Companion.currentQueue
import platform.Foundation.NSOperationQueue.Companion.mainQueue
import platform.Foundation.NSURL
import platform.UIKit.UIApplicationDidEnterBackgroundNotification
import platform.UIKit.UIApplicationWillEnterForegroundNotification
import platform.darwin.NSObjectProtocol
Expand Down Expand Up @@ -288,7 +290,12 @@ 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()
is MediaSource.Bundle -> {
val path = NSBundle.mainBundle.pathForResource(fileName, fileType)
requireNotNull(path)
val url = NSURL.fileURLWithPath(path)
AVPlayerItem(url)
}
}

actual override suspend fun renderVideoOnSurface(surface: MediaSurface?) {
Expand Down
5 changes: 2 additions & 3 deletions media/src/iosMain/kotlin/DefaultSoundPlayer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,9 @@ actual class DefaultSoundPlayer actual constructor(source: MediaSource.Local) :
block = { errorPtr ->
val path = when (source) {
is MediaSource.Bundle -> NSBundle.mainBundle.pathForResource(source.fileName, source.fileType)
else -> error("Should be bundle")
else -> throw MediaSoundError.UnexpectedMediaSourceShouldBeLocal
}

require(path != null) { "Invalid file for sound" }
requireNotNull(path)
val url = NSURL.fileURLWithPath(path)
AVAudioFile(forReading = url, error = errorPtr)
},
Expand Down

0 comments on commit 1aa4c2a

Please sign in to comment.