我可以使用我的应用程序停止播放吗

| 我正在使用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];
}
    }
希望它最终能满足您的需求!     

要回复问题请先登录注册