Hello everyone! Has anyone worked with the iOS AVP...
# multiplatform
a
Hello everyone! Has anyone worked with the iOS AVPlayer through Kotlin code inside a Compose Multiplatform project? If so, how did you made it work? Thanks in advance, and I wish you all a great day!
f
they made POC with Mapkit
the solutions are in the branchs of the repo
a
Sorry, but after looking into it, I'm not sure to understand, I haven't seen any implementation of the AVPlayer within the project?
f
you can replace MapKit with AVPlayer, it’s native UI component
a
https://github.com/touchlab/compose-swiftui-codelab/blob/list-solution/composeApp/src/iosMain/kotlin/ui/components/native/MapKitView.kt By simply calling the AVPlayer as in the following?
Copy code
@Composable
internal fun MapKitView(
    modifier: Modifier,
    placeName: String,
    coordinate: MapCoordinates
) {
    val factory = LocalNativeViewFactory.current

    UIKitViewController(
        modifier = modifier,
        factory = { factory.createMapView(placeName, coordinate) },
    )
}
f
So 2 solution, embedding a Swift AVPlayer component like the codelab or implement by yourself with Kotlin. Then a look at this thread : https://kotlinlang.slack.com/archives/C0346LWVBJ4/p1705920277017339
a
It is indeed the approach that I'm currently trying, though I can't make it work on iOS for now. (works well on Android). I currently have the following:
Copy code
actual class SoundController actual constructor(
    private val preferenceRepository: PreferenceRepository
) : KoinComponent {

    private var playerButtonFeedback = createPlayer("sound_menu_button", "wav")
    private var playerWarningFeedback = createPlayer("sound_menu_denied", "wav")
    private var isSoundOn = true

    private val scope = CoroutineScope(<http://Dispatchers.IO|Dispatchers.IO>)
    private val playerScope = CoroutineScope(Dispatchers.Main)
    private var prefsJob: Job? = null

    init {
        prefsJob?.cancel()
        prefsJob = scope.launch {
            preferenceRepository.preferences.collect { isSoundOn = it.sound }
        }
    }

    actual fun playSound(sound: AppSound) {
        println("Play sound: ${sound.name}")
        playerScope.launch {
            if (isSoundOn) when (sound) {
                AppSound.ButtonFeedback -> if (playerButtonFeedback.status == 0L) playerButtonFeedback.play()
                AppSound.DeniedFeedback -> if (playerWarningFeedback.status == 0L) playerWarningFeedback.play()
            }
        }
    }

    private fun createPlayer(resourceName: String, type: String): AVPlayer {
        val path = NSBundle.mainBundle.pathForResource(resourceName, type)
        val url = NSURL.fileURLWithPath(path!!)

        return AVPlayer.playerWithURL(url)
    }

}
But I can't hear any sound, the resources seem to be located correctly.
f
If you really don’t know the issue, write first your code in swift, check it then convert it in Kotlin.
a
I'Il have to try it this way then, but coming from the Android development I was hoping to find a solution without Swift. Thank you for your time and your answers! I'Il try to update this thread if I found something 🙂
f
using iOS API from Kotlin, it’s using the ObjC syntax, not the swift naming