【问题标题】:AVAudioPlayer is not working in iPhone 5AVAudioPlayer 在 iPhone 5 中不起作用
【发布时间】:2012-11-26 03:46:09
【问题描述】:

我正在开发一个 iPhone 应用程序。在这个应用程序中,我使用 AVAudioPlayer 播放音频。 这是我播放声音的代码。

  NSString* resourcePath = [[NSBundle mainBundle] resourcePath];
    resourcePath = [resourcePath stringByAppendingString:@"/sound.mp3"];

    NSError* err;

    player_ = [[AVAudioPlayer alloc] initWithContentsOfURL:
               [NSURL fileURLWithPath:resourcePath] error:&err];

    if( err )
    {
    }
    else
    {
        [player_ prepareToPlay];

        [player_ play];

        int playingLoops = 0;

        player_.numberOfLoops = playingLoops;
    }

在 iPhone5 以外的 iphone 设备上运行良好。当我在 iPhone 5 上运行应用程序时,出现以下错误,然后应用程序崩溃。

2012-11-26 09:02:28.484 test[728:1dd03] <0xb0081000> Error '!obj' trying to fetch default input device's sample rate
2012-11-26 09:02:28.484 test[728:1dd03]  <0xb0081000> Error getting     audio input  device sample rate: '!obj'
2012-11-26 09:02:28.494 test[728:1dd03]  <0xb0081000> AQMEIOManager::FindIOUnit: error '!dev'

【问题讨论】:

  • 那些是奇怪的错误。我建议查看播放器的设置字典,以确定采样率是否正确。
  • 我遇到了同样的问题...除了 iPhone5 之外的所有设备都可以工作。你修好了吗?如果(做过)请告诉我~~谢谢~~

标签: iphone objective-c ios


【解决方案1】:

我也遇到了同样的问题,给出了同样的错误。当尝试解决这个问题时,我注意到代码没有问题。我们需要更新媒体播放器或安装 Flash 播放器,它工作正常。

【讨论】:

    【解决方案2】:

    您必须将 AVAudioPlayerDelegate 添加到您的 .h 文件中,并将以下代码添加到您的 .m 文件中

    NSString* resourcePath =[[NSBundle mainBundle]pathForResource:@"sound" ofType:@"mp3"];
    NSError* err;
    AVAudioPlayer *audioplayer =[[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath: resourcePath]  error:&err];
    audioplayer.delegate=self;
    if( err )
    {
       NSLog(@"%@",err);
    }
    else
    {
       [audioplayer prepareToPlay];
       [audioplayer play];
    }
    

    此代码对我有效,如果有效,请接受答案。

    【讨论】:

      【解决方案3】:

      Anbu,试试下面的代码。在我的最后它工作正常。

      NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/audiofile.mp3", [[NSBundle mainBundle] resourcePath]]];
      
      NSLog(@"File path is:->%@",[NSString stringWithFormat:@"%@/audiofile.mp3", [[NSBundle mainBundle] resourcePath]]);
      
      NSFileManager* manager;
      manager = [NSFileManager defaultManager];
      if([manager fileExistsAtPath:[NSString stringWithFormat:@"%@/audiofile.mp3", [[NSBundle mainBundle] resourcePath]]])
      {
          printf("file Exists...\n\n");
      }
      else
          printf("File not exist...\n\n");
      
      NSError *er;
      audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
      audioPlayer.numberOfLoops = -1;
      
      if (audioPlayer == nil)
          NSLog([er description]);                
      else 
          [audioPlayer play];
      

      如有任何疑问,请告诉我。另外,请检查您的控制台以获取完整路径以及文件是否存在。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-08-26
        • 1970-01-01
        • 2013-10-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多