AVAudioPlayer在模拟器的工作,但不是在设备
我的MP3播放代码:AVAudioPlayer在模拟器的工作,但不是在设备
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];");
用来玩细的MP3,它仍然没有在模拟器上。但不在设备上。
我最近在软件中添加了一些录音代码(不是我的)。它使用的AudioQueue东西略微超出了我的视野。这与AVAudioPlayer有冲突吗?或者可能是什么问题?我注意到,一旦audiorecording代码开始工作,我不能再调整设备上的音量,所以它可能会阻止音频播放?
编辑
的解决方案似乎是把这个在我的代码。我把它的applicationDidFinishLaunching:
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayAndRecord error: nil];
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);
第一行同时允许播放和录制,而其他线路改道显然的事情,使音量大。
所有的音频代码对我来说都是巫术。
这里有一个快速,简单的事情尝试:
我跑进与图像以及声音类似的问题。 iPhone是敏感的,即使是文件扩展名,但模拟器不是。除了soundObject,请检查NSURL
对象!= nil。
你也可以试试[[NSBundle mainBundle] URLForResource:@"sound" ofType:@"mp3"];
,并确保该文件正在YourApp.app/Resources/
-Stephen
这似乎不是问题所在。 – cannyboy 2010-06-15 17:56:26
一些代码,我知道作品下复制到的.app:
- (void)playOnce:(NSString *)aSound {
// Gets the file system path to the sound to play.
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:aSound ofType:@"caf"];
// Converts the sound's file path to an NSURL object
NSURL *soundURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
self.soundFileURL = soundURL;
[soundURL release];
AVAudioPlayer * newAudio=[[AVAudioPlayer alloc] initWithContentsOfURL: soundFileURL error:nil];
self.theAudio = newAudio; // automatically retain audio and dealloc old file if new file is loaded
[newAudio release]; // release the audio safely
[theAudio prepareToPlay];
// set it up and play
[theAudio setNumberOfLoops:0];
[theAudio setVolume: volumeLevel];
[theAudio setDelegate: self];
[theAudio play];
}
东西时在模拟器上工作而不在设备上,首先要做的是重新启动设备并检查。有时重新启动设备已为我工作并节省了大量时间。否则,你可以开始调试
确保你的iPhone的“Ringer Button”没有显示红色。
多么愚蠢...然而多么真实!这是我的问题:) – 2016-06-04 23:43:22
同样,现在我觉得自己像个白痴! :D – Shiri 2016-07-20 10:13:58
OH,使手机静音的侧面开关。的Bleh!大声笑 – 2017-03-17 22:50:39
我在模拟器中有声音,但没有在设备上。它开始工作,当我设置AVAudioPlayer代理
如果您在iOS 10模拟器上运行代码,它不会要求安全权限,但是,如果您在ios 10设备上运行代码,则需要在plist中添加音频权限密钥。添加隐私 - 麦克风使用说明键入plist。
哪部分不起作用? – 2010-06-15 17:42:00
在设备上播放声音不起作用。它在模拟器上工作。 – cannyboy 2010-06-15 17:53:23