使AVAudioPlayer通过电话(耳机)扬声器播放音乐文件 [英] Make AVAudioPlayer play a music file through phone call (ear) speaker

查看:86
本文介绍了使AVAudioPlayer通过电话(耳机)扬声器播放音乐文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过耳机播放音乐文件,

I want to play a music file through ear speaker,

我现在正在做的是:

viewDidLoad 方法::::

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

viewDidLoad ::::

musicFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"1985_Ring_" ofType:@"wav"]];                                                                        
click = [[AVAudioPlayer alloc] initWithContentsOfURL:musicFile  error:nil];

现在在方法中路由音频后播放文件:::::

Now Playing file after routing audio in a method :::::

[[AVAudioSession sharedInstance] overrideOutputAudioPort:kAudioSessionOverrideAudioRoute_None error:nil];
[click2 play];

通过扬声器播放。请指导我正确的方法!

It's playing through loud speaker. Please guide me the proper way!

推荐答案

您需要更改 AVAudioSession的类别像这样:

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];

音频会话编程指南


您可以通过编程方式影响音频输出路径。当使用
AVAudioSessionCategoryPlayAndRecord类别时,音频通常会以
的价格发送到接收器(当您拨打
电话时,您收听的小音箱)。

You can programmatically influence the audio output route. When using the AVAudioSessionCategoryPlayAndRecord category, audio normally goes to the receiver (the small speaker you hold to your ear when on a phone call).

这篇关于使AVAudioPlayer通过电话(耳机)扬声器播放音乐文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆