Browse Source

Fix VideoPlayer (#1088)

* fix player's macOS issue

* replace local file path with public URL
pull/1091/head v1.0.0-alpha4-build318
theapache64 3 years ago committed by GitHub
parent
commit
01355cc93f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      components/VideoPlayer/demo/src/jvmMain/kotlin/org/jetbrains/compose/videoplayer/demo/Main.kt
  2. 35
      components/VideoPlayer/library/src/desktopMain/kotlin/org/jetbrains/compose/videoplayer/DesktopVideoPlayer.kt

6
components/VideoPlayer/demo/src/jvmMain/kotlin/org/jetbrains/compose/videoplayer/demo/Main.kt

@ -13,7 +13,11 @@ fun main() {
) {
MaterialTheme {
DesktopTheme {
VideoPlayer("/System/Library/Compositions/Yosemite.mov", 640, 480)
VideoPlayer(
url = "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4",
width = 640,
height = 480
)
}
}
}

35
components/VideoPlayer/library/src/desktopMain/kotlin/org/jetbrains/compose/videoplayer/DesktopVideoPlayer.kt

@ -1,22 +1,31 @@
package org.jetbrains.compose.videoplayer
import androidx.compose.desktop.SwingPanel
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.runtime.SideEffect
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCompositionContext
import androidx.compose.ui.Modifier
import androidx.compose.ui.awt.SwingPanel
import androidx.compose.ui.graphics.Color
import uk.co.caprica.vlcj.factory.discovery.NativeDiscovery
import uk.co.caprica.vlcj.player.base.MediaPlayer
import uk.co.caprica.vlcj.player.component.CallbackMediaPlayerComponent
import uk.co.caprica.vlcj.player.component.EmbeddedMediaPlayerComponent
import java.util.*
@Composable
internal actual fun VideoPlayerImpl(url: String, width: Int, height: Int) {
println("Video player for $url")
NativeDiscovery().discover()
// Doesn't work on macOS, see https://github.com/caprica/vlcj/issues/887 for suggestions.
val mediaPlayerComponent = remember { EmbeddedMediaPlayerComponent() }
val mediaPlayerComponent = remember {
// see https://github.com/caprica/vlcj/issues/887#issuecomment-503288294 for why we're using CallbackMediaPlayerComponent for macOS.
if (isMacOS()) {
CallbackMediaPlayerComponent()
} else {
EmbeddedMediaPlayerComponent()
}
}
SideEffect {
val ok = mediaPlayerComponent.mediaPlayer().media().play(url)
println("play gave $ok")
@ -29,3 +38,21 @@ internal actual fun VideoPlayerImpl(url: String, width: Int, height: Int) {
}
)
}
/**
* To return mediaPlayer from player components.
* The method names are same, but they don't share the same parent/interface.
* That's why need this method.
*/
private fun Any.mediaPlayer(): MediaPlayer {
return when (this) {
is CallbackMediaPlayerComponent -> mediaPlayer()
is EmbeddedMediaPlayerComponent -> mediaPlayer()
else -> throw IllegalArgumentException("You can only call mediaPlayer() on vlcj player component")
}
}
private fun isMacOS(): Boolean {
val os = System.getProperty("os.name", "generic").lowercase(Locale.ENGLISH)
return os.indexOf("mac") >= 0 || os.indexOf("darwin") >= 0
}

Loading…
Cancel
Save