0
使用AVAudioPlayer播放我的声音时,背景中的音乐播放器停止播放,当我的声音播放完毕时音乐播放器无法恢复,如何恢复音乐播放器?如何在我的应用程序中播放声音后重新启动音乐播放器xcode
使用AVAudioPlayer播放我的声音时,背景中的音乐播放器停止播放,当我的声音播放完毕时音乐播放器无法恢复,如何恢复音乐播放器?如何在我的应用程序中播放声音后重新启动音乐播放器xcode
#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
我知道这已经很久了,但是非常感谢,真的帮助了我! – PaulRBerg
您是否拥有自己的音乐播放器或本机音乐播放器? – Retro
本地音乐播放器应用程序 – user1899125
这可能会帮助你 http://stackoverflow.com/questions/5549756/how-to-pause-and-resume-same-song-in-iphone-sdk-using-avaudioplayer –