0

我是初学者 iPhone 开发人员。我想播放声音,但音频文件返回空值。

这是我的代码:

-(IBAction)sound
{

    path=[[NSBundle mainBundle] pathForResource:@"Animalfile" ofType:@"plist"];
    dict=[NSDictionary dictionaryWithContentsOfFile:path];

    animalaudio=[dict valueForKey:@"audio"];
    NSLog(@"print:%@",dict);
    NSLog(@"printanimal%@",animalaudio);

    audiolist=[animalaudio objectAtIndex:currentsound];
    NSLog(@"audiolist:%@",audiolist);
    NSURL *audiourl=[NSURL fileURLWithPath:audiolist];
    NSLog(@"audiourl:%@",audiourl);

    AVAudioPlayer *audio=[[AVAudioPlayer alloc]initWithContentsOfURL:audiourl error:NULL];
    NSLog(@"audio:%@",audio);
    audio.delegate=self;
    [audio play];
}

音频返回 null... 给出适用于我们代码的任何建议和源代码。

4

1 回答 1

0
-(void)primeAudioPlayerWrong {
    NSError *error;
    AVAudioPlayer *audioPlayer;
    if(!audioPlayerWrong){
        NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/wrong.mp3", [[NSBundle mainBundle] resourcePath]]];
        audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
    }
    [audioPlayer stop];
    [audioPlayer setCurrentTime:0];
    [audioPlayer play];
}

试试这个...但在此之前,请确保 file_url 是正确的并且在提供的 url 中存在一些资源...

于 2012-07-15T13:17:24.370 回答