LoginSignup
8
3

More than 5 years have passed since last update.

AVPlayer#addPeriodicTimeObserverでメモリリークする

Posted at

AVPlayer#addPeriodicTimeObserverは、動画再生位置が更新される度に、ブロック関数へその位置をコールバックしてくれる。そして、監視を止めるときや他の画面に遷移するときなどは、AVPlayer#removeTimeObserverを呼ぶ必要がある。

しかし、それでもメモリリークしてしまう場合がある(iOS10 Simulatorで確認)。addPeriodicTimeObserverのブロック関数内でselfを使うとそうなる。よってブロック関数内ではweak変数を使った方が良い。

weak var this = self
timeObserver = myPlayer.addPeriodicTimeObserver(
  forInterval: CMTime(seconds: 1.0, preferredTimescale: 1000), queue: DispatchQueue.main) { (time) in
    if this != nil {
      this!.~~ 何らかの呼び出し ~~
    }
}

監視を止める時。

myPlayer.removeTimeObserver(timeObserver)
8
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
3