Iphone 创建全球音频播放器阵列

Iphone 创建全球音频播放器阵列,iphone,objective-c,Iphone,Objective C,如何创建一个全球音频播放器阵列,以便我可以调用其他类的播放器 Java等价于静态 我想播放多种声音。是每个播放器需要不同的播放器,还是一个播放器只需在整个过程中播放随机声音 谢谢您可以在AppDelegate类中创建一个数组(NSMutableArray),您可以向该数组添加不同的玩家对象,并且您可以从任何类访问该数组 应用程序内委托文件, 在.h文件中 NSMutableArray *playersArray; @property (nonatomic,retain)NSMutableArr

如何创建一个全球音频播放器阵列,以便我可以调用其他类的播放器

Java等价于静态

我想播放多种声音。是每个播放器需要不同的播放器,还是一个播放器只需在整个过程中播放随机声音

谢谢

您可以在AppDelegate类中创建一个数组(NSMutableArray),您可以向该数组添加不同的玩家对象,并且您可以从任何类访问该数组

应用程序内委托文件, 在.h文件中

NSMutableArray *playersArray;

@property (nonatomic,retain)NSMutableArray *playersArray;
在.m文件中初始化该数组

playersArray = [[NSMutableArray alloc] init];
在其他一些文件中,您可以访问该数组,如下所示

 AppDelegateClassName *appDelegate = (AppDelegateClassName *)[[UIApplication sharedApplication] delegate];

[appDelegate.playersArray addObject:onePlayerObject];
类似地,您可以通过读取该数组来访问该播放器对象

问候,


萨蒂亚。

你不需要一系列玩家。一个球员就够了

在appDelegate中声明此项

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{
   player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:resourcePath] error:&err];
}

-(void)playAudio:(NSString *)fileName
{
    NSString* resourcePath = [[NSBundle mainBundle] resourcePath];
    NSString* file=[NSString stringWithFormat:@"/%@",fileName];
    resourcePath = [resourcePath stringByAppendingString:file];
    NSLog(@"Path : %@",resourcePath);
    NSError* err;
    //Declare the player in your didFinishLaunching Dont declare here
    player.delegate = self;
    [player play];
}
yourAppDelegate  *yourAppDelegateObject= (yourAppDelegate *)[[UIApplication sharedApplication] delegate];
现在,无论您想在何处播放任何文件,都要为appDelegate创建一个对象

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{
   player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:resourcePath] error:&err];
}

-(void)playAudio:(NSString *)fileName
{
    NSString* resourcePath = [[NSBundle mainBundle] resourcePath];
    NSString* file=[NSString stringWithFormat:@"/%@",fileName];
    resourcePath = [resourcePath stringByAppendingString:file];
    NSLog(@"Path : %@",resourcePath);
    NSError* err;
    //Declare the player in your didFinishLaunching Dont declare here
    player.delegate = self;
    [player play];
}
yourAppDelegate  *yourAppDelegateObject= (yourAppDelegate *)[[UIApplication sharedApplication] delegate];
使用该对象调用该函数

 [yourAppDelegateObject playAudio:filenameOftheAudioFile];

数组是如何创建的?它应该设置为全局(什么语法)还是自动设置为全局?