AppDelegate.m:我使用此代码启动AVAudioPlayer,它适用于所有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:在标题中我添加了框架和AVAudioPlayer。
#import <AVFoundation/AVFoundation.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate, AVAudioPlayerDelegate> {
AVAudioPlayer *_backgroundMusic; }
Prefix.pch:在前缀中我添加以下行:
#import "AppDelegate.h"
#define MyAppDelegate ((AppDelegate *)[[UIApplication sharedApplication] delegate])
我需要在GameScene中停止AVAudioPlayer并继续在MenuScene中播放。谁能帮我找到这个问题的解决方案?:)
答案 0 :(得分:0)
您可以将backgroundMusic设为公开(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
然后您可以在场景中访问它(假设您的.pch文件设置为correctly):
[[MyAppDelegate backgroundMusic] stop];