开发者

Creating a global array of Audioplayers

How does one create a global array of audioplayers so that I can call the players from other classes.

The Java equivalent to 开发者_如何转开发static?

I want to play multiple sounds. Is a different player needed for each, or can one player, just play random sounds throughout?

Thanks


You dont need an array of players. one player is enough

Declare this in your 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];
}

Now wherever you want to play any file create a object to appDelegate

yourAppDelegate  *yourAppDelegateObject= (yourAppDelegate *)[[UIApplication sharedApplication] delegate];

Use that object to call that function

 [yourAppDelegateObject playAudio:filenameOftheAudioFile];


You can create an array(NSMutableArray) in the AppDelegate class and you can add different players objects to that array, and you can access that array from any class

in App Delegate file, in .h file

NSMutableArray *playersArray;

@property (nonatomic,retain)NSMutableArray *playersArray;

in .m file initialize that array

playersArray = [[NSMutableArray alloc] init];

in some other file you can access that array as follows

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

[appDelegate.playersArray addObject:onePlayerObject];

similarly you can access that player object by reading from that array.

Regards,

Satya.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜