如何从目录路径播放wav文件

时间:2013-02-21 15:10:39

标签: objective-c avaudioplayer

我正在尝试从我之前保存的目录路径中播放wav文件,但我无法播放,希望有人可以帮助我

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *directoryPath = [paths objectAtIndex:0];
directoryPath =[NSString stringWithFormat:@"%@/%@",directoryPath,@"path1.wav",nil];
audioPath1 = [[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:directoryPath] error:NULL]; 
[audioPath1 play];

2 个答案:

答案 0 :(得分:2)

应该是那样的东西,

-(void)playFileAdv:(NSString *)audioFile WithVolume:(float)audioLevelIndex{
    [self stopAlert];

    pSound = [[NSSound alloc]initWithContentsOfFile:audioFile byReference:NO];
    playRecursively = NO;
    [pSound setVolume:audioLevelIndex];
    [pSound setDelegate:self];
    [pSound play];

}

答案 1 :(得分:1)

if ([[NSFileManager defaultManager] fileExistsAtPath:directoryPath]) {
    NSError *error = nil;
    audioPath1 = [[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:directoryPath] error:&error];
    if (!error) {
        [audioPath1 play];
    }
    else {
        NSLog(@"Error in creating audio player:%@",[error description]);
    }
}
else {
    NSLog(@"File doesn't exists");
}

使用上面的代码向我们展示控制台日志