ios 单 AVAudioPlayer

发布于 2024-12-27 02:40:37 字数 1304 浏览 2 评论 0原文

我正在开发一个带有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谈下烟灰 2025-01-03 02:40:37

创建一个单例,在你的player.m中

#import "player.h"

@implementation player

static player *sharedInstance = nil;

+ (player *)sharedInstance {
    if (sharedInstance == nil) {
        sharedInstance = [[super allocWithZone:NULL] init];
    }

    return sharedInstance;
}

- (id)init
{
    self = [super init];

    if (self) {
        // Work your initialising here as you normally would
    }

    return self;
}

-(void)playSong:(NSString *)songName
{
    // do your stuff here
}

使用这个类,只需导入player.h

[[player sharedInstance] playSong:@"something"];

create a singleton, in your player.m

#import "player.h"

@implementation player

static player *sharedInstance = nil;

+ (player *)sharedInstance {
    if (sharedInstance == nil) {
        sharedInstance = [[super allocWithZone:NULL] init];
    }

    return sharedInstance;
}

- (id)init
{
    self = [super init];

    if (self) {
        // Work your initialising here as you normally would
    }

    return self;
}

-(void)playSong:(NSString *)songName
{
    // do your stuff here
}

to use this class, just import the player.h

[[player sharedInstance] playSong:@"something"];
水溶 2025-01-03 02:40:37

怎么出错了?对我来说,最后一行似乎会导致编译错误。您应该为类声明添加星号:

player * myPlayer = ((AppDelegate *)[UIApplication sharedApplication].delegate).theMainPlayer;

除此之外,我没有发现任何内容。
但对于这样的情况,我更喜欢使用单例模式。

参考:

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 :

player * myPlayer = ((AppDelegate *)[UIApplication sharedApplication].delegate).theMainPlayer;

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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文