【发布时间】:2012-07-16 13:51:52
【问题描述】:
我正在尝试在我的 iOS 应用中播放音频文件。这是我当前的代码
NSString *soundFilePath = [NSString stringWithFormat:@"%@/test.m4a", [[NSBundle mainBundle] resourcePath]];
NSLog(@"%@",soundFilePath);
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[audioPlayer setVolume:1.0];
audioPlayer.delegate = self;
[audioPlayer stop];
[audioPlayer setCurrentTime:0];
[audioPlayer play];
我正在检查一堆其他帖子,但它们通常都做同样的事情。我没有收到错误消息,但我没有听到模拟器或设备上播放任何音频。
这是我在模拟器上获取音频文件的路径
/Users/username/Library/Application Support/iPhone Simulator/5.1/Applications/long-numbered-thing/BookAdventure.app/test.m4a
感谢任何帮助!
【问题讨论】:
-
尝试另一个音频文件,test.m4a 可能已损坏,这就是它无法播放的原因。
-
audioPlayer是否指向有效对象?静音开关是否打开?设备音量调高了吗? -
我很确定这是一个有效的对象。有没有办法判断路径是否正确?我认为如果它是空对象或无效路径会引发错误。静音开关未打开并且设备音量已打开@Jeremy1026 该文件在 xCode 和我的 MBP 上正常播放,所以我认为这很好
-
你在这个项目中使用 ARC 吗?
-
你明白了吗? @Lagoo87
标签: objective-c ios xcode audio