2014-01-18 119 views
0

使用AVAudioPlayer播放我的聲音時,背景中的音樂播放器停止播放,當我的聲音播放完畢時音樂播放器無法恢復,如何恢復音樂播放器?如何在我的應用程序中播放聲音後重新啓動音樂播放器xcode

+0

您是否擁有自己的音樂播放器或本機音樂播放器? – Retro

+0

本地音樂播放器應用程序 – user1899125

+2

這可能會幫助你 http://stackoverflow.com/questions/5549756/how-to-pause-and-resume-same-song-in-iphone-sdk-using-avaudioplayer –

回答

1
#import "ViewController.h" 
#import <AVFoundation/AVFoundation.h> 

@interface ViewController() <AVAudioPlayerDelegate> 

@property (nonatomic, strong) AVAudioPlayer *audioPlayer; 

@end 

@implementation ViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    _audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"ttt" ofType:@"mp3"]] error:nil]; 
_audioPlayer.delegate = self; 
} 

- (void)play:(id)sender 
{ 
    [_audioPlayer play]; 
} 

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag 
{ 
    AVAudioSession *audioSession = [AVAudioSession sharedInstance]; 
    NSError *error = nil; 
    if (![audioSession setActive:NO withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:&error]) { 
     NSLog(@"resume music player failed, error=%@", error); 
    } 
} 

@end 
+0

我知道這已經很久了,但是非常感謝,真的幫助了我! – PaulRBerg