我正在使用AudioPlayer来停止所有播放,因为代码是
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/audiofile.mp3", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = -1;
if (audioPlayer == nil)
NSLog([error description]);
else
[audioPlayer play];
但是我可以用这个停止所有的回放,但是不能再玩停止播放了。我怎么能这样做请帮助
答案 0 :(得分:3)
您可以使用以下三种方法轻松播放暂停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];
}
}
希望它最终能满足您的需求!