2012-11-26 36 views
3

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

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.While 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' 
+0

这些都是奇怪的错误。我建议您查看播放器的设置字典,以确定采样率是否正确。 – Daniel

+0

我得到了同样的问题......所有设备的作品除iPhone5.Did你修好了吗?如果(做)请告诉我~~ thx ~~ –

回答

0

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

0

你必须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]; 
} 

此代码为我工作和接受的答案,如果它工作。

0

安布,尝试下面的代码。在我的结束它工作正常。

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

在任何问题,请让我知道。而且,请检查您的控制台的完整路径和文件是否存在。

相关问题