Я пытаюсь остановить музыкальный плеер, прежде чем моя игра войдет в фоновый режим, но без успеха. Я просмотрел все вопросы с тем же вопросом, что и у меня, но я не могу найти ответ, который мне нужен. Я играю музыку моей игры от главного View Controller:AVAudioSession проблема при вводе фона
XYZViewController.h
@import AVFoundation;
@interface XYZViewController : UIViewController
@property (readwrite)AVAudioPlayer* backgroundMusicPlayer;
EDIT, чтобы добавить этот кусок кода, это был я Alloc и инициализировать музыкальный проигрыватель. Забыл, добавить до, извините:
XYZViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
[self gameCenterLogin];
// Configure the view.
SKView * skView = (SKView *)self.view;
skView.showsFPS = YES;
skView.showsNodeCount = YES;
//AUDIO BACKground
NSError *error;
NSURL * backgroundMusicURL = [[NSBundle mainBundle] URLForResource:@"Astro World music" withExtension:@"mp3"];
self.backgroundMusicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:backgroundMusicURL error:&error];
self.backgroundMusicPlayer.numberOfLoops = -1;
[self.backgroundMusicPlayer prepareToPlay];
[self.backgroundMusicPlayer play];
[skView presentScene:scene];
}
У меня есть эти два общих методов в моей ViewController, чтобы остановить и играть свою музыку:
-(void)stopPlayer{
[self.backgroundMusicPlayer stop];
}
-(void)playMusic{
[self.backgroundMusicPlayer play];
}
Это то, что я делаю из appDelegate.m, прежде чем войти фон , я вызываю метод из моего ViewController, чтобы остановить музыку, прежде чем остановить AVAUdioSession
- (void)applicationWillResignActive:(UIApplication *)application
{
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
XYZViewController* mainController = (XYZViewController*) self.window.rootViewController;
[mainController stopPlayer];
[[AVAudioSession sharedInstance] setActive:NO error:nil];
NSLog(@"pause music");
}
Но все, что я делаю, я всегда получаю ту же ошибку:
RROR: [0x35aa49dc] AVAudioSession.mm:646: -[AVAudioSession setActive:withOptions:error:]: Deactivating an audio session that has running I/O. All I/O should be stopped or paused prior to deactivating the audio session.
2015-01-15 20:09:49.080 Astro World[269:23154] pause music
Любая помощь будет apreciated, спасибо!
Вы проверили, вызывается ли 'stopPlayer'? – 0x141E
Правильно ли вы реализовали методы делегирования AVAudioSession? – sangony
@ 0x141E Да, я проверил это с помощью NSLog внутри метода stopPlayer и да, он вызывается. – user3033437