如何解决我可以使用我的应用程序停止播放吗
| 我正在使用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];
但是我可以停止使用此功能的所有播放,但是不能再次播放停止的播放。我该怎么做请帮助
解决方法
您可以使用以下三种方法轻松播放暂停停止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];
}
}
希望它最终能满足您的需求!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。