Hi
@
svyatoslav.scherbina
, sorry for the late reply, I didn’t have time for a detail explanation.
The main problem is how to override
observeValueForKeyPath
function for catching new values.
Idea warns me that ‘observeValueForKeyPath’ overrides nothing.
Do you know how to do that?
Kotlin code
import kotlinx.cinterop.CValue
import platform.AVFoundation.*
import platform.CoreGraphics.CGRect
import platform.CoreGraphics.CGRectMake
import platform.Foundation.NSKeyValueObservingOptionNew
import platform.Foundation.NSURL
import platform.Foundation.addObserver
import platform.UIKit.UIView
import platform.darwin.NSObject
class PlayerOK {
private var observer = Observer()
private var asset: AVAsset
private var player: AVPlayer
private var playerItem: AVPlayerItem
private var playerLayer: AVPlayerLayer = AVPlayerLayer()
private val gcRect: CValue<CGRect> = CGRectMake(height = 100.0, width = 100.0, x = 20.0, y = 20.0)
val view: UIView = UIView(gcRect)
private val requiredAssetKeys = listOf("playable", "hasProtectedContent")
init {
val url =
NSURL(string = "<https://devstreaming-cdn.apple.com/videos/streaming/examples/bipbop_adv_example_hevc/master.m3u8>")
asset = AVAsset.assetWithURL(URL = url)
// Create a new AVPlayerItem with the asset and an
// array of asset keys to be automatically loaded
playerItem = AVPlayerItem(asset = asset, automaticallyLoadedAssetKeys = requiredAssetKeys)
// Register as an observer of the player item's status property
playerItem.addObserver(observer, forKeyPath = "status", options = NSKeyValueObservingOptionNew, context = null)
// Associate the player item with the player
player = AVPlayer(playerItem = playerItem)
playerLayer.player = player
playerLayer.frame = view.bounds
view.layer.addSublayer(playerLayer)
player.play()
class Observer : NSObject() {
fun observeValueForKeyPath(
keyPath: String?,
ofObject: Any?,
change: Map<Any?, *>?,
context: kotlinx.cinterop.COpaquePointer?
println("keyPath $keyPath")
println("ofObject $ofObject")
println("change $change")
println("context $context")
Using in swift
class ViewController: UIViewController {
var c: PlayerOK?
override func viewDidLoad() {
super.viewDidLoad()
c = PlayerOK()
self.view.addSubview(c!.view)
Xcode error:
iosApp[3203:1352905] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '<sample.Observer: 0x2801446d0>: An -observeValueForKeyPath:ofObject:change:context: message was received but not handled.
Key path: status
Observed object: <AVPlayerItem: 0x28014ba00, asset = <AVURLAsset: 0x280368d00, URL = <https://devstreaming-cdn.apple.com/videos/streaming/examples/bipbop_adv_example_hevc/master.m3u8>>>
Change: {
kind = 1;
new = 1;
Context: 0x0'
*** First throw call stack:
(0x209d7127c 0x208f4b9f8 0x209c7b4b0 0x20a7bc9b4 0x20a7bcb08 0x20a7bee9c 0x20a7bc3bc 0x20fca9f18 0x102cab6f0 0x102cacc74 0x102cba6fc 0x209d02c1c 0x209cfdb54 0x209cfd0b0 0x20befd79c 0x23652c978 0x1027c989c 0x2097c28e0)
libc++abi.dylib: terminating with uncaught exception of type NSException