我需要播放视频以及2秒的音频播放。在iPhone模拟器中,一切正常,但在设备中,一切正常。我将音频文件存储在项目包中,然后从那里播放音频。
NSString *gotItsoundFilePath = [[NSBundle mainBundle] pathForResource:@"gotit" ofType:@"mp3"];
NSData *gotItsampleData = [[NSData alloc] initWithContentsOfFile:gotItsoundFilePath];
NSError *gotItaudioError = nil;
gotItAudioPlayer = [[AVAudioPlayer alloc] initWithData:gotItsampleData error:&gotItaudioError];
gotItAudioPlayer.numberOfLoops = 0;
当我使用
[gotItAudioPlayer play];
但该设备无法正常运行时,此代码在iPhone Simulator中可以正常工作。我在某处做错了吗?有人可以帮忙吗?
最佳答案
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayAndRecord error: nil];
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);
输入此代码。在使玩家成为对象之前。
关于iphone - 使用AVAudioPlayer播放的音频在iPhone模拟器中有效,但在设备中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11240838/