我正在尝试使用AVFoundation框架播放一个mp3文件。它无法检测到该文件,并出现以下错误:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSURL initFileURLWithPath:]: nil string parameter'已实现的代码:
NSString *path = [[NSBundle mainBundle] pathForResource:@"sound" ofType:@"mp3"];
NSLog(@"directory %@",path);
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
[theAudio play];当我将pathForResource:@“声音”更改为pathForResource:@“声音”(空格)时,它可以检测到文件,但听不到声音。我也在.h文件中声明了AVdelegate。有人知道问题出在哪里吗?
发布于 2012-09-20 01:05:50
您需要先使用gige prepareToPlay才能播放音频。检查此代码片段..这对我很管用。
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,是);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.mp3", fileName]];
NSError *err;
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath])
{
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:
[NSURL fileURLWithPath:filePath] error:&err];
player.delegate = self;
[player prepareToPlay];
[player setNumberOfLoops:0];
[player setVolume:0.5];
[player play]; }
https://stackoverflow.com/questions/12498293
复制相似问题