为iPhone开发应用程序,其中包含需要通过耳机收听的音频文件.
如何检查耳机是否未插入,以便告诉用户插入耳机.
我有来自另一个线程的以下代码,但不推荐使用audioSessionGetProperty方法.任何人都知道如何更改以下代码以使其工作或拥有自己的代码/解决方案.
谢谢.
- (BOOL)isHeadsetPluggedIn {
UInt32 routeSize = sizeof (CFStringRef);
CFStringRef route;
//Maybe changing it to something like the following would work for iOS7?
//AVAudioSession* session = [AVAudioSession sharedInstance];
//OSStatus error = [session setCategory:kAudioSessionProperty_AudioRoute...?
//the line below is whats giving me the warning
OSStatus error = AudioSessionGetProperty (kAudioSessionProperty_AudioRoute,
&routeSize,
&route);
/* Known values of route:
* "Headset"
* "Headphone"
* "Speaker"
* "SpeakerAndMicrophone"
* "HeadphonesAndMicrophone"
* "HeadsetInOut"
* "ReceiverAndMicrophone"
* "Lineout"
*/
if (!error && (route != NULL)) …Run Code Online (Sandbox Code Playgroud) 要在静音模式下播放声音我使用以下方法.但它是如何不起作用的.
// Works on Swift 3
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
} catch {
print(error)
}
Run Code Online (Sandbox Code Playgroud)
如何让它在4.2/iOS 12中运行?
在较新的版本中,我们需要设置模式和选项.
try AVAudioSession.sharedInstance().setCategory(
<#T##category:AVAudioSession.Category##AVAudioSession.Category#>,
mode: <#T##AVAudioSession.Mode#>,
options: <#T##AVAudioSession.CategoryOptions#>)`
Run Code Online (Sandbox Code Playgroud) 我似乎无法在SDK中找到如何以编程方式感知iPhone上的静音按钮/开关.当我的应用程序播放背景音乐时,它会正确响应音量按钮,而我没有任何代码可以遵循,但是,当我使用静音开关时,它只是继续播放.
如何测试静音的位置?
(注意:我的程序有自己的静音开关,但我希望物理开关覆盖它.)
谢谢!
我在XCode 6中测试我的应用程序,并在iOS8中发现AVAudioSession的问题.
我打电话的时候
[[AVAudioSession sharedInstance] setActive:NO error:nil];
我收到以下错误消息:
AVAudioSession.mm:623: - [AVAudioSession setActive:withOptions:error:]:停用已运行I/O的音频会话.在停用音频会话之前,应停止或暂停所有I/O.
在AVAudioSession.h中,它说
请注意,如果会话在运行或暂停I/O(例如音频队列,播放器,录像机,转换器,远程I/O等)时设置为非活动状态,则此方法将在iOS 8上或之后链接的应用中引发异常. .
但我不知道如何检查是否有运行的I/O以及如何在需要重置音频会话时将其全部丢弃.
作为AudioSessionSetProperty有可能成为deprecated,我想找到的代码示例如何路由音频到speaker使用其他手段.
以前我做过以下事情:
-(void)setSpeakerEnabled
{
debugLog(@"%s",__FUNCTION__);
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (
kAudioSessionProperty_OverrideAudioRoute,
sizeof (audioRouteOverride),
&audioRouteOverride
);
}
Run Code Online (Sandbox Code Playgroud)
试图获得相同的结果,但without call到AudioSessionSetProperty.
我的应用程序使用麦克风并输出音频,因此我将我的音频会话设置为播放和录制类别,但这似乎禁用了Airplay.如果我将类别设置为Play,Airplay可以正常输出(但显然输入不起作用).
我已经尝试将输出路径覆盖到扬声器,以防它需要通过Airplay输出,但没有快乐.
有任何想法吗?
我尝试使用这些方法试图检测Ring/Silent开关是否处于活动状态:
但是在我的iPhone 4上,"状态"值始终是"扬声器"(并且CFStringGetLength(状态)返回的长度值始终为7).有没有人成功使用过这种方法?如果是这样,关于什么样的设备和SDK版本?
我这样称呼它:
- (BOOL)deviceIsSilenced {
CFStringRef state;
UInt32 propertySize = sizeof(CFStringRef);
OSStatus audioStatus = AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &state);
if (audioStatus == kAudioSessionNoError) {
NSLog(@"audio route: %@", state) // "Speaker" regardless of silent switch setting, but "Headphone" when my headphones are plugged in
return (CFStringGetLength(state) <= 0);
}
return NO;
}
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
audioSession.delegate = self;
[audioSession setCategory:AVAudioSessionCategoryAmbient error:nil];
[audioSession setActive:YES error:nil];
NSLog(@"muted? %i", [self deviceIsSilenced]);
...
}
Run Code Online (Sandbox Code Playgroud)
我想当手机上的物理开关被切换时,可能会触发其他一些(更准确的)kAudioSessionProperty事件.有人有主意吗?
顺便说一句,我正在使用AVAudioSessionCategoryAmbient类别和我的[AVAudioSession …
我有一个iOS应用程序,使用AVAudioSession流式传输背景音频.它工作正常,但我很好奇,有没有办法改变锁屏音频控件上的文字?现在它只显示我的应用程序的名称,但我想将其更改为轨道的名称.
此外,多任务栏在控件下没有文字 - 有没有办法在那里添加曲目名称,就像iPod应用程序一样?
我需要列出iOS应用程序可用的音频输出.我的问题与此类似:如何在iOS上列出可用的音频输出路径
我试过这段代码:
NSError *setCategoryError = nil;
BOOL success = [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback
error: &setCategoryError];
NSError *activationError = nil;
[[AVAudioSession sharedInstance] setActive: YES error: &activationError];
…
NSLog(@"session.currentRoute.outputs count %d", [[[[AVAudioSession sharedInstance] currentRoute] outputs ] count]);
for (AVAudioSessionPortDescription *portDesc in [[[AVAudioSession sharedInstance] currentRoute] outputs ]) {
NSLog(@"-----");
NSLog(@"portDesc UID %@", portDesc.UID);
NSLog(@"portDesc portName %@", portDesc.portName);
NSLog(@"portDesc portType %@", portDesc.portType);
NSLog(@"portDesc channels %@", portDesc.channels);
}
Run Code Online (Sandbox Code Playgroud)
但是我总是看到一个输出端口(计数是1),如果我有两个(Airplay和内置扬声器).如果我使用音乐应用程序,我可以看到两个端口并在它们之间切换.在我的应用程序中,我只看到我选择的那个.
我还需要做些什么吗?
谢谢
编辑:
我也试过这个代码:
CFDictionaryRef asCFType = nil;
UInt32 dataSize = sizeof(asCFType);
AudioSessionGetProperty(kAudioSessionProperty_AudioRouteDescription, &dataSize, &asCFType); …Run Code Online (Sandbox Code Playgroud) 录像应用程序.我希望它能够在不停止/暂停背景音乐的情况下工作(例如,当用户收听Apple Music时).这我可以设置类别做很好mixWithOthers的AVAudioSession单.
设置类别后,我还需要添加AVCaptureDeviceInput到AVCaptureSession(所以声音会被记录下来).这会导致背景音频的故障/打嗝以及视频重置/重新聚焦.
我已经调查过,似乎背景音频故障是无法避免的,但视频在添加输入时不应重置.视频重置的结果是录制视频的第一帧是暗/黑色,或者从失焦帧开始然后聚焦.
还检查Snapchat ios应用程序,他们在开始录制时也有音频故障,但视频开始顺利录制.我究竟做错了什么.
我的代码:
//Setting audio session to mixWithOthers upon startup
let session = AVAudioSession.sharedInstance()
do {
try session.setCategory(AVAudioSessionCategoryPlayAndRecord,
with: [.mixWithOthers])
if session.mode != AVAudioSessionModeVideoRecording {
try session.setMode(AVAudioSessionModeVideoRecording)
}
} catch let error {
print("avsession category error: \(error)")
}
Run Code Online (Sandbox Code Playgroud)
然后:
//Just before recording starts will add audio input
let audioDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeAudio)
do
{
let deviceInput = try AVCaptureDeviceInput(device: audioDevice) as AVCaptureDeviceInput
if captureSession.canAddInput(deviceInput) {
captureSession.addInput(deviceInput)
}
else …Run Code Online (Sandbox Code Playgroud) avaudiosession ×10
ios ×8
objective-c ×3
swift ×3
airplay ×2
audio ×1
avfoundation ×1
headphones ×1
ios4 ×1
ios7 ×1
ios8 ×1
iphone ×1
xcode6 ×1