AVAudioPlayer 在 iPod touch 2G 和 3G 中播放音乐
我正在尝试使用 AVAudioPlayer 在 iPhone 和 iPod touch 上播放音频文件。该代码在 iPhone 和 iPod touch 4G 上运行良好,我可以听到音乐。但是当我在 iPod touch 2G、iPod touch 3G 上测试时,我听不到任何声音。
下面是我用来播放音频文件的代码:
NSString *zeroAudioPath = [[NSBundle mainBundle] pathForResource:@"TimerBell"
ofType:@"aif"];
NSURL *file = [[NSURL alloc] initFileURLWithPath:zeroAudioPath];
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:file
error:nil];
[file release];
self.zeroAudioPlayer = audioPlayer;
zeroAudioPlayer.delegate = self;
[audioPlayer release];
[zeroAudioPlayer prepareToPlay];
[zeroAudioPlayer play];
I'm trying to play an audio file on iPhone and iPod touch with AVAudioPlayer. The code works fine in iPhone and iPod touch 4G, I can hear the music. But when I test it on iPod touch 2G, iPod touch 3G, I can't hear anything.
Below is the code I use to play audio file:
NSString *zeroAudioPath = [[NSBundle mainBundle] pathForResource:@"TimerBell"
ofType:@"aif"];
NSURL *file = [[NSURL alloc] initFileURLWithPath:zeroAudioPath];
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:file
error:nil];
[file release];
self.zeroAudioPlayer = audioPlayer;
zeroAudioPlayer.delegate = self;
[audioPlayer release];
[zeroAudioPlayer prepareToPlay];
[zeroAudioPlayer play];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我发现从
kAudioSessionCategory_PlayAndRecord
更改为kAudioSessionCategory_AmbientSound
可以纠正此问题。我怀疑这与 iPod touch 上与录音相关的硬件差异有关。I found that changing from
kAudioSessionCategory_PlayAndRecord
tokAudioSessionCategory_AmbientSound
corrected this issue. I suspect this has something to do with a hardware difference on the iPod touch related to recording.在玩之前你应该像这样使用audiosession:
You should use audiosession like this before you play: