i rely on: Stream Added event
Code: Select all
...
private var playerStateChangedNotification: NSObjectProtocol?
override init(frame: CGRect)
{
super.init(frame: frame)
mediaPlayer.media = VLCMedia(url: URL(string: "http://some.video.URL")!)
mediaPlayer.delegate = self
mediaPlayer.drawable = self
playerStateChangedNotification = NotificationCenter.default.addObserver(forName: Notification.Name(rawValue: VLCMediaPlayerStateChanged), object: mediaPlayer, queue: nil,
using: self.playerStateChanged)
mediaPlayer.play()
}
func playerStateChanged(_ notification: Notification)
{
if(mediaPlayer.state == VLCMediaPlayerState.esAdded)
{
...Do you Stuff here...
}
}
you could also do a work around and instead of immediately call to play action (mediaPlayer.play()) you could call play, pause, delayed play (0.5 sec delay or even 0.3 sec delay), the effect is barely noticeable:
Code: Select all
override init(frame: CGRect)
{
super.init(frame: frame)
mediaPlayer.media = VLCMedia(url: URL(string: "http://some.video.URL")!)
mediaPlayer.delegate = self
mediaPlayer.drawable = self
playerStateChangedNotification = NotificationCenter.default.addObserver(forName: Notification.Name(rawValue: VLCMediaPlayerStateChanged), object: mediaPlayer, queue: nil,
using: self.playerStateChanged)
mediaPlayer.play()
mediaPlayer.pause()
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5)
{ [unowned self] in
self.mediaPlayer.play()
}
}
func playerStateChanged(_ notification: Notification)
{
if(mediaPlayer.state == VLCMediaPlayerState.playing)
{
...Do you Stuff here...
}
}
...
and lastly you could use a simple flag:
Code: Select all
...
private var isFirstPlay: Bool = true // this trick is to overcome VLC bug that first play event never received
override init(frame: CGRect)
{
super.init(frame: frame)
mediaPlayer.media = VLCMedia(url: URL(string: "http://some.video.URL")!)
mediaPlayer.delegate = self
mediaPlayer.drawable = self
playerStateChangedNotification = NotificationCenter.default.addObserver(forName: Notification.Name(rawValue: VLCMediaPlayerStateChanged), object: mediaPlayer, queue: nil,
using: self.playerStateChanged)
mediaPlayer.play()
}
func playerStateChanged(_ notification: Notification)
{
if(mediaPlayer.state == VLCMediaPlayerState.buffering && isFirstPlay)
{
isFirstPlay = false
...Do you Stuff here...
}
}
...
side not: do not forger to call unregister to the NotificationCenter Observer before deleting the player var (do not rely on the deinit - do it before deinit is called, otherwise the player is never released and a memory leak will occur)
i add these functions:
Code: Select all
...
func unregisterObservers()
{
NotificationCenter.default.removeObserver(playerStateChangedNotification as Any)
}
deinit
{
print("deinit()")
}
public func stopPlayer()
{
print("stopPlayer()")
mediaPlayer.stop()
unregisterObservers()
}
...