AVAudioPlayer在iPhone 5上无法工作。

3

我正在开发一款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;
    }

在iPhone设备上一切正常,除了iPhone5。当我在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'

那些错误很奇怪。我建议查看播放器的设置字典,确定采样率是否正确。 - Daniel Zhang
我遇到了相同的问题...所有设备都可以工作,除了iPhone5。你解决了吗?如果解决了,请告诉我~~ 谢谢~~ - Steven Jiang
3个回答

0

你需要在你的 .h 文件中添加 AVAudioPlayerDelegate,然后在你的 .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];
}

这段代码对我有效,如果它有效,请接受答案。


0

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];

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

0

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接