1

我正在尝试从我之前保存在那里的目录路径中播放 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];
4

2 回答 2

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

}
于 2013-02-21T15:18:41.357 回答
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");
}

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

于 2013-02-21T15:18:37.860 回答