我試圖單擊按鈕時播放聲音。首先,我進口AudioToolbox在頭文件,然後添加代碼到火災時,按下按鈕,像這樣的IBAction爲:簡單播放iOS中的聲音
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)button1:(id)sender {
NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"mySong" ofType:@"wav"];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:soundPath], &soundID);
AudioServicesPlaySystemSound (soundID);
}
我甚至在方法拋出的NSLog以確保它的射擊。它是。我也是,但在字符串中的一條路上看它是否會崩潰,它的確如此。我確信我將短音響文件拖放到應用程序中。測試我的揚聲器,通過在模擬器上的YouTube上測試設備模擬器上的音頻。我無法弄清楚我做錯了什麼。
當我改變了代碼,以這樣的:
- (IBAction)button1:(id)sender {
AVAudioPlayer *testAudioPlayer;
// *** Implementation... ***
// Load the audio data
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"3" ofType:@"wav"];
NSData *sampleData = [[NSData alloc] initWithContentsOfFile:soundFilePath];
NSError *audioError = nil;
// Set up the audio player
testAudioPlayer = [[AVAudioPlayer alloc] initWithData:sampleData error:&audioError];
if(audioError != nil) {
NSLog(@"An audio error occurred: \"%@\"", audioError);
}
else {
[testAudioPlayer setNumberOfLoops: -1];
[testAudioPlayer play];
}}
我得到的錯誤:警告:137:錯誤嘗試設置(空)音頻設備的採樣率
嘗試在設備上而不是在模擬器中進行測試。 – matt 2014-11-09 04:52:57