2012-07-14 3 views
0

Je suis un développeur iPhone débutant. Je veux jouer du son mais le fichier audio renvoie une valeur nulle.Comment lire un son avec AVAudioPlayer?

Ceci est mon code:

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

retourne Audio null ... chaque suggestion et le code source qui s'appliquent dans notre code.

+2

Ajouter une erreur de vérification! Vous supposez que chaque appel retourne avec succès ... – trojanfoe

Répondre

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

essayer ça ... mais avant cela, assurez-vous que le FILE_URL est correct et il existe une ressource à l'adresse fournie ...

Questions connexes