Trouble using replaceCurrentItemWithPlayerItem when using MacRuby
-
12-06-2021 - |
Question
I am using AVFoundation and MacRuby to handle video playback. I have no trouble getting a video to play in my AVPlayer initially using the code below:
videoWindow.setFrame(@frame, display:true, animate:true)
@content_view = self.videoWindow.contentView
@content_view.wantsLayer = true
asset = AVAsset.assetWithURL(url)
item = AVPlayerItem.new
item.initWithAsset(asset)
$video = AVPlayer.alloc.initWithPlayerItem(item)
@player_layer = AVPlayerLayer.playerLayerWithPlayer($video)
@player_layer.Frame = @frame
@content_view.layer.addSublayer(@player_layer)
@player_layer.videoGravity = AVLayerVideoGravityResize
$video.seekToTime(CMTimeMake(4, 1))
$video.play
Where I run into trouble is when I am trying to change the playback to a new video. To do this I am trying to use the same AVPlayer object and use the replaceCurrentItemWithPlayerItem command. When I try to run this the application stops responding.
asset2 = AVAsset.assetWithURL(@url2)
item2 = AVPlayerItem.new
item2.initWithAsset(asset2)
$video.pause
$video.replaceCurrentItemWithPlayerItem(item2)
I have also tried performingSelectorOnMainThread but I get the same results.
$video.performSelectorOnMainThread('replaceCurrentItemWithPlayerItem:', withObject:item2, waitUntilDone:true)
I am wondering if there is a way to reuse my AVPlayer object to play new items in?
Solution
This is what ended up working:
item = AVPlayerItem.playerItemWithURL(@url)
$video.replaceCurrentItemWithPlayerItem(item)
There seemed to be some problem with how I was creating assets. I just skipped the asset and created the item directly.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow