0

AppDelegate.m: I use this code to start AVAudioPlayer and it works fine for all SKScene.

#import "AppDelegate.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

{
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"game-sound" ofType:@"mp3"];
NSURL *file = [NSURL fileURLWithPath:soundFilePath];
_backgroundMusic = [[AVAudioPlayer alloc] initWithContentsOfURL:file error:nil];
_backgroundMusic.numberOfLoops = -1;
_backgroundMusic.volume = 0.1;
[_backgroundMusic play];

return YES; }

AppDelegate.h: In the header I add framework and AVAudioPlayer.

#import <AVFoundation/AVFoundation.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate, AVAudioPlayerDelegate> {

AVAudioPlayer *_backgroundMusic; }

Prefix.pch: In Prefix I add this line:

#import "AppDelegate.h"

#define MyAppDelegate ((AppDelegate *)[[UIApplication sharedApplication] delegate])

I need to stop AVAudioPlayer in the GameScene and to continue playing in the MenuScene. Can anyone help me to find a solution of this problem?:)

Whirlwind
  • 14,286
  • 11
  • 68
  • 157
SteV8
  • 79
  • 6

1 Answers1

0

You can make backgroundMusic to be public (AppDelegate.h):

#import <AVFoundation/AVFoundation.h>
#import <UIKit/UIKit.h>

 @interface AppDelegate : UIResponder <UIApplicationDelegate, AVAudioPlayerDelegate>

    @property (nonatomic, strong)AVAudioPlayer *backgroundMusic;

    @property (strong, nonatomic) UIWindow *window;

 @end

Then you can access it in your scene like this(assuming that your .pch file is set correctly):

[[MyAppDelegate backgroundMusic] stop];
Community
  • 1
  • 1
Whirlwind
  • 14,286
  • 11
  • 68
  • 157