can*_*boy 17 iphone objective-c avaudioplayer
我的mp3播放代码是:
NSError *error;
soundObject = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:audioPathString] error:&error];
if (soundObject == nil) NSLog(@"%@", [error description]);
soundObject.delegate = self;
soundObject.numberOfLoops = 0;
soundObject.volume = 1.0;
NSLog(@"about to play");
[soundObject prepareToPlay];
[soundObject play];
NSLog(@"[soundObject play];");
Run Code Online (Sandbox Code Playgroud)
mp3曾经很好玩,它仍然可以在模拟器上运行.但不在设备上.
我最近在软件中添加了一些录音代码(不是我的).它使用的AudioQueue东西略微超出我的范围.这与AVAudioPlayer有冲突吗?或者可能是什么问题?我注意到,一旦音频录制代码开始工作,我就无法再调整设备上的音量,所以它可能会阻止音频播放吗?
编辑
解决方案似乎是将它放在我的代码中.我把它放在applicationDidFinishLaunching中:
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayAndRecord error: nil];
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);
Run Code Online (Sandbox Code Playgroud)
第一行允许播放和录制,而其他行显然重新路由事物以使音量更大.
所有的音频代码都是伏都教给我的.
这是一个快速、简单的尝试:
我在图像和声音方面遇到了类似的问题。iPhone 是大小写敏感的,甚至是文件扩展名,但模拟器不是。NSURL除了 soundObject 之外,还检查object != nil。
您可能还想尝试[[NSBundle mainBundle] URLForResource:@"sound" ofType:@"mp3"];确保该文件被复制到 .app 下YourApp.app/Resources/
-史蒂芬