任何人都可以指導我的教程,或演示我播放背景音樂的實際代碼。我需要它能夠開始和停止。這不是一個任務,所以你知道。我的任務已經完成,我只是想添加音樂,我不知道如何。謝謝!iPhone背景音樂播放器
0
A
回答
2
您可以使用AVAudioPlayer
//You have to import AvFoundation to use AVAudioPlayer
#import <AVFoundation/AVFoundation.h>
-(void) playMusicFile:(NSString *) songName
{
NSString *musicFile = [[NSBundle mainBundle] pathForResource:songName ofType:@"mp3"];
NSError *soundError = nil;
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:musicFile] error:&soundError];
if(self.audioPlayer == nil)
{
NSLog(@"%@",soundError);
}
else
{
//Delegation is optional but it helps you do stuff after song finished playing etc
[self.audioPlayer setDelegate:self];
//Set number of repeats, 0 is default plays once, negative values makes it play infinitely
[self.audioPlayer setNumberOfLoops:0];
//Prepare to play is not always necessary, but otherwise it can take time to play
[self.audioPlayer prepareToPlay];
[self.audioPlayer play];
}
}
//This is the delegate called after the song finished playing, you can use it to play other songs, or do other stuff
-(void) audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
...
}
停止和暫停:
//You can easily stop and pause using:
[self.audioPlayer stop]; //Stop doesn't work as you would expect. It doesn't set the current time to 0 but only undoes the setup you had with the audioplayer.
[self.audioPlayer pause];
//It is possible to call [self.audioPlayer play] after each method and playback will continue from where it left off.
1
AVPlayer
和AVAudioPlayer
應該工作。
相關問題
- 1. WP7背景音樂播放
- 2. WP8背景音樂播放
- 3. 背景音樂播放器和cocos2d SimpleAudioEngine
- 4. iPhone背景音樂
- 5. AVFoundation IOS:播放背景音樂
- 6. 在背景中播放音樂
- 7. avaudioplayer在背景播放音樂
- 8. 暫停/播放背景音樂
- 9. 在循環播放背景音樂Qt
- 10. 停止/只播放背景音樂
- 11. 播放背景音樂:AVAudioSessionInterruptionNotification未解鎖
- 12. Python:在背景中播放音樂?
- 13. 播放選定的音樂作爲遊戲的背景音樂
- 14. 如何在GW-Basic中播放背景音樂(音樂)
- 15. iPhone - 當需要從背景進行背景音樂重播背景音時
- 16. 如何在我的iPhone遊戲中播放背景音樂?
- 17. HTML音樂應用不支持iPhone背景播放
- 18. iPhone - 播放電影后恢復背景音樂
- 19. 如何在iPhone上播放背景音樂時設置標題?
- 20. 播放背景音樂在iPhone Web應用程序
- 21. cocos2d-iphone遊戲有時候會停止播放背景音樂
- 22. 停止背景音樂播放時,歌曲下載的iPhone
- 23. Java聲音。暫時靜音背景音樂並播放聲音
- 24. 如何播放背景音樂並使其循環播放?
- 25. 在播放視頻時播放背景音樂
- 26. iphone - 播放背景中的音頻流
- 27. objective c iphone音樂播放器崩潰
- 28. iOS 5.1停止背景音樂播放器
- 29. 讓我的背景音樂播放器停止? Objective C
- 30. 背景音樂
真棒,謝謝一堆。我看到你的文件類型是@「mp3」,但是我應該在哪裏放置我喜歡的歌曲的實際名稱@「背景音樂」? – 2013-04-25 20:14:47
我已經共享了一個函數playMusicFile,如果你想播放backgroundmusic.mp3就像這樣調用它:[self playMusicFile:@「backgroundmusic」]。 – guenis 2013-04-25 20:16:34