ios 单 AVAudioPlayer
我正在开发一个带有 uitabviewcontroller 的 ios 应用程序,可以播放一些音乐。我不希望每个选项卡视图控制器创建自己的音频播放器。我想要一个音频播放器并让所有视图控制器共享它。
所以我创建了一个名为player的类,它将使用歌曲url启动avaudioplayer并播放歌曲,
#import <AVFoundation/AVFoundation.h>
@interface player : NSObject {
AVAudioPlayer *theMainAudio;
}
-(void)playSong:(NSString *)songName;
@end
我只想创建这个类的一个实例,并且我的所有视图控制器共享它。我尝试在我的委托
@interface AppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {
UIWindow *window;
UITabBarController *tabBarController;
player *theMainPlayer;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
@property (nonatomic, retain) player *theMainPlayer;
@end
中的 .m 文件中创建它,
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//some other stuff here....
theMainPlayer = [[player alloc]init];
return YES;
}
然后在我的视图控制器中调用它,
player myPlayer = ((AppDelegate *)[UIApplication sharedApplication].delegate).theMainPlayer;
但这不起作用。 谁能告诉我我所做的有什么问题,或者是否有其他方法可以做我想做的事情,即创建一个播放器对象并在我的所有视图控制器之间共享它。
谢谢
I'm working on an ios app with uitabviewcontroller that plays some music. I don't want each tab viewcontroller to create it's own audio player. I want to have one single audio player and have all the viewcontrollers share it.
so I have created a class called player, which would initiate avaudioplayer with the song url and plays the song,
#import <AVFoundation/AVFoundation.h>
@interface player : NSObject {
AVAudioPlayer *theMainAudio;
}
-(void)playSong:(NSString *)songName;
@end
I want to create only one instance of this class and all my viewcontrollers share it. I've tried creating it in my delegate,
@interface AppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {
UIWindow *window;
UITabBarController *tabBarController;
player *theMainPlayer;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
@property (nonatomic, retain) player *theMainPlayer;
@end
in .m file,
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//some other stuff here....
theMainPlayer = [[player alloc]init];
return YES;
}
and then I called it in my viewcontrollers,
player myPlayer = ((AppDelegate *)[UIApplication sharedApplication].delegate).theMainPlayer;
but this didn't work.
can anyone tell me what's wrong with what I've done or if there is any other way to do what I want to do, which is to create a player object and share it among all of my viewcontrollers.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
创建一个单例,在你的player.m中
使用这个类,只需导入player.h
create a singleton, in your player.m
to use this class, just import the player.h
怎么出错了?对我来说,最后一行似乎会导致编译错误。您应该为类声明添加星号:
除此之外,我没有发现任何内容。
但对于这样的情况,我更喜欢使用单例模式。
参考:
http://developer.apple。 com/library/mac/#documentation/General/Conceptual/DevPedia-CocoaCore/Singleton.html
http://www.galloway.me.uk/tutorials/singleton-classes/
How did it go wrong? To me it seems the last line would cause compile error. You should add asterisk for class declaration :
Other than that, I didn't catch anything.
But I prefer using Singleton pattern for cases like this.
Reference :
http://developer.apple.com/library/mac/#documentation/General/Conceptual/DevPedia-CocoaCore/Singleton.html
http://www.galloway.me.uk/tutorials/singleton-classes/