0
votes

I'm trying to stop music player before my game enters in background but with no success. I looked all the questions with the same issue I have but I can't find the answer I need. I'm playing the music of my game from the main View Controller:

XYZViewController.h

@import AVFoundation;

@interface XYZViewController : UIViewController 
@property (readwrite)AVAudioPlayer* backgroundMusicPlayer;

EDIT to add this piece of code, this is were I alloc and init the music player. Forgot, to add before, sorry:

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];

}

I have those two public methods in my viewController to stop and play my music:

-(void)stopPlayer{
    [self.backgroundMusicPlayer stop];
}
-(void)playMusic{

    [self.backgroundMusicPlayer play];
}

This is what I do from appDelegate.m before enter background, I call the method from my viewController to stop music before stop the 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");

}

But whatever I do I always get the same error:

    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

Any help would be apreciated, thanks!

1
Did you check if stopPlayer is called?0x141E
Did you properly implement the AVAudioSession delegate methods?sangony
@0x141E Yes I checked that with an NSLog inside the stopPlayer method and yes it's being called.Alfro
@sangony I don't have any delegate for AVAudioSession. Are those really important? Do I really need them?Alfro
Won't work properly without delegates.sangony

1 Answers

1
votes

inserting this code in the method of AVAudioPlayerDelegate.

-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
  [audioPlayer stop];
  [audioPlayer prepareToPlay];
}

where audioPlayer is a variable declared in the header of my class.

the instruction prepareToPlay should resolve your problem!

This might helps you :)