您可以輕鬆播放暫停停止你AVAudioPlayer用這三種方法:
- (void)pause
- (void)stop
- (BOOL)play //Returns YES on success, or NO on error.
如果暫停,然後你可以發揮你從何處暫停恢復。
希望這可以幫助,我真的不知道你的問題在哪裏!
在你給的代碼
,你是不是暫停,你只是用numberOfLoops
負播放。 你應該有一個方法來啓動你的音樂是這樣的:
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/audiofile.mp3", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = 0;//play once
if (audioPlayer == nil)
NSLog([error description]);
else
[audioPlayer prepareToPlay];
[audioPlayer play];
,另一個是暫停:
[audioPlayer pause];
,另一個用於恢復:
[audioPlayer play];
要切換的iPod音樂,當應用程序啓動和退出覆蓋這兩種方法:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
MPMusicPlayerController *musicPlayer;
MPMusicPlaybackState playbackState = [musicPlayer playbackState];
if (playbackState == MPMusicPlaybackStatePlaying) { //simple verification
[musicPlayer pause];
}
}
- (void)applicationWillResignActive:(UIApplication *)application
if (playbackState == MPMusicPlaybackStateStopped || playbackState == MPMusicPlaybackStatePaused) {//simple verification
[musicPlayer play];
}
}
希望它終於適合您的需求!
看,我在聽音樂,我運行我的應用程序應該停止所有播放,但是當我點擊屏幕時自動播放開始, – Ali 2011-06-15 18:50:48
您是否暫停所有您開始的播放?或者你的意思是iPod.app音樂? – 2011-06-15 19:00:31
是的,我可以使用上面的技巧暫停所有播放但不能恢復它 – Ali 2011-06-15 19:02:30