在iPhone應用程序中播放循環聲音的最簡單方法是什麼?播放循環聲音最簡單的方法是什麼?
回答
可能最簡單的解決方案是使用AVAudioPlayer,而numberOfLoops:
設置爲負整數。
// *** In your interface... ***
#import <AVFoundation/AVFoundation.h>
...
AVAudioPlayer *testAudioPlayer;
// *** Implementation... ***
// Load the audio data
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"sample_name" ofType:@"wav"];
NSData *sampleData = [[NSData alloc] initWithContentsOfFile:soundFilePath];
NSError *audioError = nil;
// Set up the audio player
testAudioPlayer = [[AVAudioPlayer alloc] initWithData:sampleData error:&audioError];
[sampleData release];
if(audioError != nil) {
NSLog(@"An audio error occurred: \"%@\"", audioError);
}
else {
[testAudioPlayer setNumberOfLoops: -1];
[testAudioPlayer play];
}
// *** In your dealloc... ***
[testAudioPlayer release];
您還應該記得設置適當的音頻類別。 (請參閱AVAudioSessionsetCategory:error:
方法。)
最後,您需要將AVFoundation庫添加到您的項目中。爲此,請在Xcode的Groups & Files列中單擊您的項目目標,然後選擇「Get Info」。然後選擇常規選項卡,單擊底部「鏈接庫」窗格中的+並選擇「AVFoundation.framework」。
這仍然適用於弧? – 2015-09-28 08:37:18
最簡單的方法是使用AVAudioPlayer設置爲無限數量的循環(或有限的,如果這是你所需要的)。
喜歡的東西:
NSString *path = [[NSBundle mainBundle] pathForResource:@"yourAudioFileName" ofType:@"mp3"];
NSURL *file = [[NSURL alloc] initFileURLWithPath:path];
AVAudioPlayer *_player = [[AVAudioPlayer alloc] initWithContentsOfURL:file error:nil];
[file release];
_player.numberOfLoops = -1;
[_player prepareToPlay];
[_player play];
任何音頻文件已無限期指定這會簡單循環。 如果您想要音頻文件循環的次數有限,請將循環數設置爲任意正整數。
希望這會有所幫助。
乾杯。
我得到錯誤:「使用未聲明的標識符'AVAudioPlayer'」 – Linuxmint 2011-03-08 19:12:08
請確保您的文件頂部或頭文件中有#import
@Brenton不是在'#import
- 1. 在iPhone上播放聲音的最簡單方法是什麼?
- 2. 從delphi的數組數據播放聲音最簡單的方法是什麼
- 3. 在Haskell中播放聲音樣本的最簡單方法是什麼?
- 4. WP7:在XNA中播放系統聲音的最簡單方法是什麼
- 5. 播放單音音頻文件的最佳方法是什麼?
- 6. 同時播放兩個聲音的最簡單方法(C++ winapi)
- 7. 從C/C++播放聲音最簡單的方法
- 8. 播放ogg循環聲音
- 9. 聲音隨機化最簡單的方法是什麼
- 10. 在UWP中播放背景音頻最簡單的方法是什麼?
- 11. Objective-c/IOS:向後播放音頻文件最簡單的方法是什麼
- 12. 在WPF中播放MP3最簡單的方法是什麼?
- 13. 什麼是自動播放聲音最「兼容」的方式?
- 14. SKAction播放暫停的聲音循環
- 15. 什麼是最流暢的音頻API循環iPhone的聲音
- 16. 在iPhone應用程序中播放聲音剪輯的最簡單方法是什麼?
- 17. 播放聲音循環或使用音頻服務播放
- 18. 在java中每x秒播放聲音的簡單方法
- 19. 簡單的方法來播放聲音文件(.aif)
- 20. 簡單播放iOS中的聲音
- 21. FMOD中的簡單聲音播放
- 22. MediaPlay無法播放聲音,爲什麼?
- 23. 播放背景聲音循環
- 24. 在WP7中播放聲音循環
- 25. 在PHP循環中播放聲音onclick
- 26. 構建angularJS音頻播放器的最佳方法是什麼?
- 27. 在Qt中播放通知(頻率x)聲音 - 最簡單的方法?
- 28. 什麼是最低級別的Windows功能播放聲音?
- 29. 在Android中播放音頻RTMP流最簡單的方法
- 30. 在iOS中以更高音調播放音樂的最簡單方法是什麼?
@Federico謝謝你的見解。 :-) – 2011-03-08 19:21:15
從現在起稱我爲Captain Obvious。我希望這很明顯,我在開玩笑:-) – 2011-03-08 20:00:46
@Federico(或者應該是@Captain?)是的,我會說這很明顯,你是在開玩笑。 :-) – 2011-03-08 20:03:07