【问题标题】:Audio overlap issue音频重叠问题
【发布时间】:2016-06-09 01:03:57
【问题描述】:

我正在使用 Objective-c 开发一个 iOS 应用程序。当应用程序启动时,会播放背景音乐。当用户单击帮助按钮时,背景音乐应继续播放。此外,当用户从帮助屏幕返回主屏幕时,背景音乐应该会持续播放。

对我来说,当我从帮助导航到主菜单时,会随着旧背景音乐播放新的背景音乐。所以,我现在听到两首背景音乐。

谁能帮我解决这个问题?

问候, 婆罗地。

【问题讨论】:

  • 听起来你不是停止旧的后台播放器,而是在返回主屏幕时创建一个新的。但是没有代码可以调试或查看您当前在做什么,我们无法为您提供任何帮助......抱歉。
  • - (void)viewDidLoad { [super viewDidLoad]; NSString *path = [NSString stringWithFormat:@"music.mp3", [[NSBundle mainBundle] resourcePath]]; NSURL *soundUrl = [NSURL fileURLWithPath:path]; _audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:nil]; [_audioPlayer 播放]; }

标签: ios objective-c


【解决方案1】:

如果您在 UIApplicationDelegate(或其他一些保留的单例)中保留对音频播放器的引用,您的问题可能会得到解决。

//in the .h
@property (nonatomic, strong) AVAudioPlayer *player;

//in the .m

- (void) playMusic
{
    if (self.player == nil) {
        NSString *path = [NSString stringWithFormat:@"%@/music.mp3", [[NSBundle mainBundle] resourcePath]];
        NSURL *soundUrl = [NSURL fileURLWithPath:path];
        self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:nil];
    }
    if (!self.player.isPlaying) {
        [self.player play];
    }
}

这样你就可以在任何你需要的地方调用它:

[(MyAppDelegate*)[UIApplication sharedApplication].delegate playMusic];

尽管将 SoundsManager 类作为一个单例来处理可能对您有利,以便处理您需要跟踪的所有声音,如果您需要的不仅仅是这个。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-30
    • 1970-01-01
    • 2017-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多