Skip to content

Commit 3eeb27f

Browse files
authored
Merge pull request #196 from kdroidFilter/feat/open-asset-api
feat: add openAsset() API for loading bundled media files
2 parents 8d0c619 + ba143bb commit 3eeb27f

3 files changed

Lines changed: 34 additions & 0 deletions

File tree

mediaplayer/src/androidMain/kotlin/io/github/kdroidfilter/composemediaplayer/VideoPlayerState.android.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,13 @@ open class DefaultVideoPlayerState(
634634
openFromMediaItem(mediaItem, initializeplayerState)
635635
}
636636

637+
override fun openAsset(
638+
fileName: String,
639+
initializeplayerState: InitialPlayerState,
640+
) {
641+
openUri("asset:///$fileName", initializeplayerState)
642+
}
643+
637644
private fun openFromMediaItem(
638645
mediaItem: MediaItem,
639646
initializeplayerState: InitialPlayerState,

mediaplayer/src/commonMain/kotlin/io/github/kdroidfilter/composemediaplayer/VideoPlayerState.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,20 @@ interface VideoPlayerState {
162162
initializeplayerState: InitialPlayerState = InitialPlayerState.PLAY,
163163
)
164164

165+
/**
166+
* Opens a media file bundled with the application.
167+
*
168+
* On Android, plays directly from the APK's `assets/` directory via the `asset:///` URI scheme (zero-copy).
169+
* On iOS, resolves the file from the app bundle via `NSBundle.mainBundle`.
170+
*
171+
* @param fileName the file name or relative path (e.g. `"video.mp4"` or `"videos/intro.mp4"`)
172+
* @throws UnsupportedOperationException on platforms where asset loading is not yet supported
173+
*/
174+
fun openAsset(
175+
fileName: String,
176+
initializeplayerState: InitialPlayerState = InitialPlayerState.PLAY,
177+
): Unit = throw UnsupportedOperationException("openAsset is not supported on this platform")
178+
165179
// Error handling
166180
val error: VideoPlayerError?
167181

mediaplayer/src/iosMain/kotlin/io/github/kdroidfilter/composemediaplayer/VideoPlayerState.ios.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,19 @@ open class DefaultVideoPlayerState(
742742
openUri(fileUrl, initializeplayerState)
743743
}
744744

745+
override fun openAsset(
746+
fileName: String,
747+
initializeplayerState: InitialPlayerState,
748+
) {
749+
val name = fileName.substringBeforeLast(".")
750+
val ext = fileName.substringAfterLast(".", "")
751+
val path =
752+
platform.Foundation.NSBundle.mainBundle
753+
.pathForResource(name, ext.ifEmpty { null })
754+
?: throw IllegalArgumentException("Asset not found in app bundle: $fileName")
755+
openUri("file://$path", initializeplayerState)
756+
}
757+
745758
override val metadata: VideoMetadata
746759
get() = _metadata
747760

0 commit comments

Comments
 (0)