简体   繁体   English

多个AVPlayers在单独的UIViewControllers上

[英]Multiple AVPlayers on Separate UIViewControllers

I have multiple AVPlayers , each on separate UIViewControllers playing different songs. 我有多个AVPlayers ,每个都在单独的UIViewControllers播放不同的歌曲。
I need to pause one AVPlayer whenever I play another one ( otherwise the audio overlaps ). 每当我播放另一个AVPlayer时,我都需要暂停它( 否则音频会重叠 )。
I would like to let the user traverse through the app while the music plays in the background, so pausing the AVPlayer on viewDidDisappear:(BOOL)animated , would not work. 我想让用户在音乐在后台播放时遍历应用程序,因此在viewDidDisappear:(BOOL)animated上暂停AVPlayer无效。
What is the best way to access the controls of each separate AVPlayer ? 访问每个单独的AVPlayer控件的最佳方法是什么?

In my opinion a singleton with only 1 AVPlayer solves this issue well. 我认为只有1个AVPlayer的单例可以很好地解决此问题。 This way you guarantee that to play another song you have to stop the previous. 这样,您可以保证播放另一首歌曲之前必须停止播放。 Then, in that AVPLayerSingleton you have a private property called avPlayer. 然后,在该AVPLayerSingleton中,您有一个称为avPlayer的私有属性。 You can define two methods: 您可以定义两种方法:

- (void)createPlayerWithSong:(NSString *)currentSong;
- (void)destroyPlayer

Then, in your createPlayerWithSong you can check if avPlayer is already created and destroy it and create a new one for each new song. 然后,在您的createPlayerWithSong中,您可以检查是否已创建avPlayer并将其销毁,并为每首新歌曲创建一个新的avPlayer。

Couple of ways you could do it: 可以通过几种方法做到这一点:

Create a shared singleton object that have weak properties that reference each AVPlayer. 创建一个共享的单例对象,该对象具有引用每个AVPlayer的弱属性。 That way you can control it from anywhere. 这样,您可以从任何地方进行控制。

OR 要么

Use NSNotificationCenter to send notifications when you want to control an AVPlayer on a different view controller. 当您想在其他视图控制器上控制AVPlayer时,请使用NSNotificationCenter发送通知。 This might get cumbersome if you have a lot of AVPlayers you want to control. 如果您有很多要控制的AVPlayer,这可能会很麻烦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM