iPhone,AVAudioPlayer和多声道

时间:2010-07-06 14:17:27

标签: iphone avaudioplayer

我是新开发者并制作我的firt iPhone应用程序,我想制作按钮来打开/关闭多声道,例如当我按下多个声音的按钮后,当我播放声音时,它同时播放所有声音,如果它关闭可以只播放一个声音,什么代码可以打开/关闭多个声音播放?

抱歉我的英语不好坦克!

1 个答案:

答案 0 :(得分:1)

人气选手,

为了一次播放多个声音,Apple建议使用硬件解码的.caf格式。此外,您基本上只是为每个声音文件创建一个新的AVAudioPlayer对象,而不是在您一次只想播放一个声音的情况下重新使用同一个对象。

我不会涉及很多代码,因为如果你只是搜索它已经有很多...但是这里有一些有用的链接可以帮助你入门:

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];

}