如何创建一个全局的音频播放器数组,以便我可以调用其他类的播放器。
Java是否等同于静态?
我想播放多种声音。是否每个播放器都需要不同的播放器,或者一个播放器可以始终播放随机的声音?
谢谢
发布于 2010-12-18 23:16:56
您可以在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];类似地,您可以通过读取该数组来访问该播放器对象。
致以敬意,
萨蒂亚。
发布于 2010-12-19 00:45:30
你不需要一组玩家。一个玩家就足够了
在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];
}现在,无论您想在何处播放任何文件,都可以为appDelegate创建一个对象
yourAppDelegate *yourAppDelegateObject= (yourAppDelegate *)[[UIApplication sharedApplication] delegate];使用该对象调用该函数
[yourAppDelegateObject playAudio:filenameOftheAudioFile];https://stackoverflow.com/questions/4478109
复制相似问题