首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AVFoundation - iOS听不到声音

AVFoundation - iOS听不到声音
EN

Stack Overflow用户
提问于 2012-09-19 23:49:25
回答 1查看 1.5K关注 0票数 0

我正在尝试使用AVFoundation框架播放一个mp3文件。它无法检测到该文件,并出现以下错误:

代码语言:javascript
复制
     Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSURL initFileURLWithPath:]: nil string parameter'

已实现的代码:

代码语言:javascript
复制
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。有人知道问题出在哪里吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-09-20 01:05:50

您需要先使用gige prepareToPlay才能播放音频。检查此代码片段..这对我很管用。

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,是);

代码语言:javascript
复制
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];    

}

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12498293

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档