If I've an AVPlayerItem
its status is never reaching .readyToPlay
on the Travis VM while running a UI test. Everything works fine locally.
I've set up simple repro:
https://travis-ci.org/gsabran/TestAVItemStatus
https://github.com/gsabran/TestAVItemStatus
This makes my tests fail on Travis because some events are only getting triggered once the video item is ready to play.
Here is my application (a single view controller). Basically it just loads a local video and changes the UI when the video starts playing.
override func viewDidLoad() {
super.viewDidLoad()
item = AVPlayerItem(url: URL(fileURLWithPath: Bundle.main.path(forResource: "video", ofType: "mp4")!))
player = AVPlayer(playerItem: item)
item.addObserver(
self,
forKeyPath: #keyPath(AVPlayerItem.status),
options: [.initial, .old, .new],
context: nil)
if player.currentItem?.status == .readyToPlay {
videoDidLoad()
}
player.play()
}
override func observeValue(forKeyPath keyPath: String?,
of object: Any?,
change: [NSKeyValueChangeKey : Any]?,
context: UnsafeMutableRawPointer?) {
guard let item = object as? AVPlayerItem else { return }
if item.status == .readyToPlay {
DispatchQueue.main.async {
self.videoDidLoad()
}
}
}
func videoDidLoad() {
// Not triggered on Travis VM, while working fine on local simulator and device
label.text = "video ready"
}
Here is the test, simply checking for a label after a while:
class TestAVItemStatusUITests: XCTestCase {
override func setUp() {
super.setUp()
continueAfterFailure = false
XCUIApplication().launch()
}
func testExample() {
sleep(5)
if !XCUIApplication().staticTexts["video ready"].exists {
XCTFail()
}
}
}
It's not working on Travis.