【问题标题】:play 15 second mp3 ios播放 15 秒 mp3 ios
【发布时间】:2013-10-06 07:42:28
【问题描述】:

我已经尽我所能尝试让我的应用播放 mp3。我有一些 2-3 秒的 mp3 可以作为音效播放,但我正在尝试播放 15 秒的 mp3,但没有任何运气。

    NSError *error;
    AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[[NSBundle mainBundle] URLForResource: @"test" withExtension: @"mp3"] error:&error];
    if (error)  {
        NSLog(@"Error creating audio player: %@", [error userInfo]);
    } else {
        if([audioPlayer play] == FALSE) {
            NSLog(@"Fail %@", error);
        }
    }
    NSTimer* myTimer = [NSTimer scheduledTimerWithTimeInterval: 15.0 target: self
                                                      selector: @selector(stopMusic) userInfo: nil repeats: NO];

我怀疑计时器与它有什么关系,但我想我会包括它以防万一。基本上我的错误检查根本没有被触发。它“应该”可以正常播放,但没有声音发出,也没有显示错误。

我尝试了其他一些方法来尝试使其正常工作,并且我尝试过不使用计时器。我找不到像样的指南。我不需要完整的媒体播放器,只需要 UIScreenViewController 的一些背景音乐。

我还尝试了以下代码,它适用于短 MP3,而不适用于我想播放的较长 MP3。

NSString *path  = [[NSBundle mainBundle] pathForResource:@"bgmusic" ofType:@"mp3"];
NSURL *pathURL = [NSURL fileURLWithPath : path];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)pathURL, &audioEffect);
AudioServicesPlaySystemSound(audioEffect);

请帮忙,我已经花了好几个小时试图对此进行排序。似乎苹果每次发布操作系统时都会重新调整音频。

【问题讨论】:

  • 你的播放器认为它正在播放吗?在 if 块之后调用并记录 [audioPlayer playing]。也许与 mp3 文件有关?你的代码对我来说很合适。
  • 它打印的是真的,所以它认为它正在播放。

标签: ios audio mp3


【解决方案1】:

我为你做了一个小测试:事实证明,在 Xcode 5 中,音乐文件默​​认不会添加到目标。因此URLForResource:error: 可能会返回nil。

尝试从您的项目中删除 mp3。然后再次添加它,这次确保将它添加到目标中:

另一件事是:您似乎在方法中定义了AVAudioPlayer。您通常会为您的音频播放器设置一个@property,否则它的生命将受到方法的限制 - 并且在您到达方法结束后音乐停止播放。

【讨论】:

  • 我已经在复制到目标。这是方法中的 AVAudioPlayer - 哇不敢相信我错过了一个谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-09-27
  • 2012-06-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多