难以播放音频文件

时间:2011-08-29 15:38:41

标签: iphone objective-c

嗨,我点击“下一步”按钮将我带到下一页,但是当我按下“播放”按钮时,我正试图播放翻页声音音乐,播放音乐,请有人帮忙。 这是代码:

- (IBAction)next {

    // This part plays the next page turn noise
    NSURL *this = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/next.mp3", [[NSBundle mainBundle] resourcePath]]];

    NSError *error;
    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:this error:&error];
    [audioPlayer setDelegate:self];
    audioPlayer.numberOfLoops = 0;
    [audioPlayer play];

    // This part takes us to the next view
    Rabbana2 *rab = [[Rabbana2 alloc] initWithNibName:@"Rabbana2" bundle:nil];
    [UIView beginAnimations:@"flipView" context:Nil];
    [UIView setAnimationDuration:2];
    [UIView setAnimationCurve:UIViewAnimationOptionCurveEaseInOut];
    [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];

    [self.view addSubview:rab.view];
    [UIView commitAnimations];

}

// This button plays the audio
- (IBAction)play {

    if(clicked == 0){
        clicked = 1;
        NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/rabbana1.wav", [[NSBundle mainBundle] resourcePath]]];

        NSError *error;
        audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
        [audioPlayer setDelegate:self];
        audioPlayer.numberOfLoops = 0;


        [audioPlayer play];
        [start setImage:[UIImage imageNamed:@"Sstop.png"] forState:UIControlStateNormal];

    } 
    else{
        [audioPlayer release];
        clicked = 0;
        [start setImage:[UIImage imageNamed:@"Pplay.png"] forState:UIControlStateNormal];
    } 

}

//If user does not do anything by the end of the sound set the button to start
- (void) audioPlayerDidFinishPlaying: (AVAudioPlayer *) player successfully: (BOOL) flag {
    if (flag==YES) {
        clicked = 0;
        [start setImage:[UIImage imageNamed:@"Pplay.png"] forState:UIControlStateNormal];
    }

1 个答案:

答案 0 :(得分:0)

我会确保您的音频文件已正确编码。您的代码可能正在执行播放,但由于您的音频文件编码不正确,因此无法播放。

这是一个命令行转换,用于将wav文件转换为caf(iphone喜欢)

afconvert -f caff -d LEI16 @ 44100 -c 1 in.wav out.caf