无法在设备上播放音频(iOS)

时间:2012-03-05 08:29:43

标签: iphone ios ipad avfoundation avaudioplayer

我使用以下代码在iOS中使用**AVAudioPlayer**类播放音频。

NSString *audioPath = [[NSBundle mainBundle] pathForResource:@"myAudio" ofType:@"mp3"];
NSURL *audioURL = [[NSURL alloc] initFileURLWithPath:audioPath];
NSError *error;
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:&error];
audioPlayer.delegate = self;
[audioPlayer prepareToPlay];
[audioPlayer play];

这在模拟器中工作正常。但是,当我在设备上运行它时,我没有得到声音输出。 AVAudioPlayerDelegate的委托方法也被调用,但没有音量输出。我在哪里错了....我在这里注意到的另一个重要方面是,我在模拟器上播放音频文件时在我的GDB中收到一条大信息。但是设备上没有发生同样的事情......在模拟器上运行时,请参考下面的屏幕截图,了解GDB消息。

enter image description here

3 个答案:

答案 0 :(得分:0)

检查您的设备是否处于扬声器模式..

通过Loundspeaker听声音使用这个 -

// set audio to loudSpeaker mode for iphone
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);

或者您可以使用此增加音频文件的音量 -

Player.volume = 10.0;

答案 1 :(得分:0)

您是否尝试过.caf或.wav文件?可能是.mp3不适用于iPhone。同时检查文件的持续时间不超过30秒。

答案 2 :(得分:0)

我是如此愚蠢....我的音量按钮已关闭。这就是原因。

真的很抱歉浪费你的时间......真诚的道歉。