开发者

iPhone, AVAudioPlayer and multiplesounds

开发者_Go百科

im new developer and making my firt iPhone app ,and i want to make button to on/off multiplesounds , example when i press button for multiple sounds after when i play sounds it plays all sound at same time and if its off can play only one sounds, what code is to make ON/OFF multiple sounds play?

sorry for my bad english, Tanks!


Sania,

In order to play multiple sounds at once, Apple recommends using the .caf format which is hardware decoded. Also, you basically just create a new AVAudioPlayer object for each sound file instead of re-using the same object in the case where you only want one sound to play at a time.

I won't go into much of the code since there is already a LOT out there if you just search for it... but here are some helpful links to get you started:

http://developer.apple.com/iphone/library/documentation/AVFoundation/Reference/AVAudioPlayerClassReference/Reference/Reference.html

http://www.mobileorchard.com/easy-audio-playback-with-avaudioplayer/

// a function I use to play multiple sounds at once
- (void)playOnce:(NSString *)aSound {

// Gets the file system path to the sound to play.
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:aSound ofType:@"caf"];  

// Converts the sound's file path to an NSURL object
NSURL *soundURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
self.soundFileURL = soundURL;
[soundURL release];

AVAudioPlayer * newAudio=[[AVAudioPlayer alloc] initWithContentsOfURL: soundFileURL error:nil];  
self.theAudio = newAudio; // automatically retain audio and dealloc old file if new file is loaded

[newAudio release]; // release the audio safely

// buffers data and primes to play
[theAudio prepareToPlay];

// set it up and play
[theAudio setNumberOfLoops:0];
[theAudio setVolume: volumeLevel];
[theAudio setDelegate: self];
[theAudio play];

}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜