如何在所有viewcontrollers中访问AVAudioPlayer? [英] How to access the AVAudioPlayer in all the viewcontrollers?

查看:85
本文介绍了如何在所有viewcontrollers中访问AVAudioPlayer?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是我的想法。一个 TableViewController 。当我点击其中一个单元格时,会出现另一个 viewcontroller ,其中包含 AVAudioPlayer 的实例,并有一个搜索栏和停止和暂停按钮。然后,当我关闭播放器视图控制器时,音乐播放并且不会停止(我已成功完成整个事情,直到此处)。现在我想创建一个正在播放按钮。当我点击它时,它会将我带回音频播放器ViewController,我可以看到歌曲的进度。关于如何做到这一点的任何想法?我的问题是我不知道如何在后台存储歌曲的信息并不断更新这些信息。

This is what i have in mind. A TableViewController. When i tap on one of the cells, another viewcontroller is presented that contains instances of AVAudioPlayer and have a seek bar and stop and pause button. Then when i close the player viewcontroller, the music plays on and is not stopped (i have successfully completed the whole thing untill here). Now i wanna create a "Now Playing" button. When i tap on it, it takes me back to the audio player ViewController and i can see the progress of the song. Any ideas on how to do this? My problem is that i do not know how to store the information of the song playing in the background and constantly update these information.

推荐答案

您可以通过使用类方法创建单例类来实现此目的,该方法将返回其AVAudioPlayer类的实例。所有AVAudioPlayer操作都应该在这个单例类上进行管理。通过这种方式,您可以使用单个对象实现AVAudioPlayer,该对象可在所有视图控制器类中以其当前状态访问。方法将被称为:

You can achieve this with creating singleton class with class method that will return the instance of its AVAudioPlayer class. All the AVAudioPlayer operation should be manage on this singleton class. With this you can implement AVAudioPlayer with single object that will accessible in all view controller class with its current state. Method will be called as:

[[MyAVAudioPlayer getInstance] getStatus];
[[MyAVAudioPlayer getInstance] play];

类结构可以是:

@interface MyAVAudioPlayer : AVAudioPlayer{

}

+(MyAVAudioPlayer *)getInstance;
-(void)playWithURL:(NSURL *)url;
-(void)play;
-(float)status; 
@end

这篇关于如何在所有viewcontrollers中访问AVAudioPlayer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆