AVFoundation-iOS can';我听不见声音
我正在尝试使用AVFoundation framework播放mp3文件。它无法检测该文件,并出现以下错误:AVFoundation-iOS can';我听不见声音,ios,xcode,audio,avfoundation,Ios,Xcode,Audio,Avfoundation,我正在尝试使用AVFoundation framework播放mp3文件。它无法检测该文件,并出现以下错误: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSURL initFileURLWithPath:]: nil string parameter' 实施的代码: NSString *path = [[NSBundle mainBundle] path
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:@“sound”更改为pathForResource:@“sound”(空间)时,它可以检测到文件,但听不到声音。我也在.h文件中声明了AVdelegate。有人知道问题出在哪里吗?要播放音频,你需要先准备播放。。检查此代码段。。它对我有用 NSArray*Path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,是)
}您的设备是否已静音?这比你想象的要多。这两个环节解决了问题。它有助于减少声音开始播放所需的时间,但实际上并不需要。此外,OP在播放应用程序包中的声音时遇到问题,而不是
NSDocumentDirectory
。也许你可以编辑这个来帮助OP:)你好,你说OP是什么意思?
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];