2013-11-01 205 views
0

我想播放mp3文件但不播放。没有错误。只是不玩。AVAudioPlayer不播放音频

我已经导入必需的文件:

#import <AudioToolbox/AudioServices.h> 
#import <AudioToolbox/AudioToolbox.h> 
#import <AVFoundation/AVFoundation.h> 

这是我的代码:

NSError *error; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *soundFilePath = [NSString stringWithFormat:@"%@/music.mp3", documentsDirectory]; 

    NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath]; 

    AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:&error]; 
    player.numberOfLoops = -1; //Infinite 
    player.volume=1; 
    player.delegate=self; 
    [player prepareToPlay]; 
    [player play]; 

没有什么是错的MP3文件。我可以在我的iPhone播放器上播放它,但不能在我的应用程序中播放。

回答

1

AVAudioPlayer必须注意的一件事是,如果播放时必须保持分配状态。如果你让AVAudioPlayer *玩家成为伊娃,我认为你的问题将会消失。

+0

非常感谢@RegularExpression – onivi