ios - 在 ViewController 中播放时从 SKScene 静音 AVAudioPlayer

标签 ios avaudioplayer sprite-kit volume

我有一个 AVAudioPlayer 设置为在 ViewController.m 中无限循环。 我的问题是我将如何从 SKScene 中静音 AVAudioPlayer

//ViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Space theme2"  ofType:@"mp3"]];
    _music = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
    [_music prepareToPlay];
    [_music setVolume:0.5];
    _music.numberOfLoops = -1;
    [_music play];

所以 AVAudioPlayer 正在 ViewController.m 中播放,需要在 SKScene 中静音/取消静音。

//SKScene.m

if ([node.name isEqualToString:@"Mute"]) {
    //Mute Music "[_music setVolume:0.0]; isn't doing anything"
    self.Mute.position = CGPointMake(-100, self.Mute.position.y);
    self.Unmute.position = CGPointMake(160, self.Unmute.position.y);
}
if ([node.name isEqualToString:@"Unmute"]) {
    //Unmute Music [_music setVolume:0.5]; isn't doing anything"
    self.Mute.position = CGPointMake(160, self.Mute.position.y);
    self.Unmute.position = CGPointMake(-100, self.Unmute.position.y);
}

感谢任何帮助

最佳答案

通过使用 NSNotificationCenter:

//ViewController.m

- (void)awakeFromNib {
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(stopMusic:)
                                                 name:@"StopMusic"
                                               object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(playMusic:)
                                                 name:@"PlayMusic"
                                               object:nil];
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Space theme2"  ofType:@"mp3"]];
    _music = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
    [_music prepareToPlay];
    [_music setVolume:0.5];
    _music.numberOfLoops = -1;
    [_music play];
}

- (void)stopMusic:(NSNotification*)notification {
    [_music stop];
}

- (void)playMusic:(NSNotification*)notification {
    [_music play];
}

- (void) dealloc
{
    // If you don't remove yourself as an observer, the Notification Center
    // will continue to try and send notification objects to the deallocated
    // object.
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

.

// Scene.m:

if ([node.name isEqualToString:@"Mute"]) {
    [[NSNotificationCenter defaultCenter]
         postNotificationName:@"StopMusic" object:self];
    self.Mute.position = CGPointMake(-100, self.Mute.position.y);
    self.Unmute.position = CGPointMake(160, self.Unmute.position.y);
}
if ([node.name isEqualToString:@"Unmute"]) {
    [[NSNotificationCenter defaultCenter]
         postNotificationName:@"PlayMusic" object:self];
    self.Mute.position = CGPointMake(160, self.Mute.position.y);
    self.Unmute.position = CGPointMake(-100, self.Unmute.position.y);
}

关于ios - 在 ViewController 中播放时从 SKScene 静音 AVAudioPlayer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22008751/

相关文章:

iphone - UIWebView 历史 If 语句

iphone - iOS - UIViews 不响应触摸事件

iphone - 如何在 ios5 中播放 AVAudioPlayer 本地方法?

ios - SKScene 与 UIKit 元素(UIButtons,UILabel,...)的场景转换

ios - 从 SKScene 中删除节点时调用方法

ios - 将 SKSpriteNode 移动到 ios 游戏中的实际触摸点

ios - 重试 URLSession dataTask 的模式?

swift - AVFoundation - 播放 wav 文件(应用程序包外部)Swift 2.1

ios - 快速制作播放列表(开始下一首歌曲)

c# - 子类化 UICollectionView