Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for iOS target compose-plugin 1.7.0-beta02 version #7

Open
kotlin-compose-multiplatform opened this issue Sep 16, 2024 · 0 comments

Comments

@kotlin-compose-multiplatform
Copy link

kotlin-compose-multiplatform commented Sep 16, 2024

If you want use this repository code on new version of compose Multiplatform you must switch to new UIKitView:

import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.onSizeChanged
import androidx.compose.ui.viewinterop.UIKitView
import kotlinx.cinterop.ExperimentalForeignApi
import platform.AVFoundation.AVPlayer
import platform.AVFoundation.play
import platform.AVKit.AVPlayerViewController
import platform.Foundation.NSURL
import platform.AVFoundation.AVPlayerLayer
import platform.UIKit.UIView
import platform.QuartzCore.CATransaction
import platform.QuartzCore.kCATransactionDisableActions
import platform.CoreGraphics.CGRect
import kotlinx.cinterop.CValue
import platform.CoreGraphics.CGRectMake
import platform.CoreGraphics.CGRectZero


@OptIn(ExperimentalForeignApi::class)
@Composable
actual fun VideoPlayer(modifier: Modifier, url: String) {
    val player = remember { AVPlayer(uRL = NSURL.URLWithString(url)!!) }
    val playerLayer = remember { AVPlayerLayer() }
    val avPlayerViewController = remember { AVPlayerViewController() }
    avPlayerViewController.player = player
    avPlayerViewController.showsPlaybackControls = true

    playerLayer.player = player
    // Use a UIKitView to integrate with your existing UIKit views
    UIKitView(
        factory = {
            // Create a UIView to hold the AVPlayerLayer
            val playerContainer = UIView()
            playerContainer.addSubview(avPlayerViewController.view)
            // Return the playerContainer as the root UIView
            playerContainer
        },
        update = { view ->
            val frame = view.superview?.bounds ?: CGRectZero
            CATransaction.begin()
            CATransaction.setValue(true, kCATransactionDisableActions)
            view.layer.frame = frame as CValue<CGRect>
            playerLayer.frame = frame
            avPlayerViewController.view.layer.frame = frame
            CATransaction.commit()

            player.play()
            avPlayerViewController.player!!.play()
        },
        modifier = modifier)

}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant