iOS 打开扬声器以及插入耳机的操作
废话不多说说一下现状
网上好多关于扬声器的操作,可是问题多多。SDK7.X 和SDK7.X以上版本号有点诧异
#import <Foundation/Foundation.h>
#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVFoundation.h>
@interface AudioManager :
NSObject
{
AVAudioPlayer *m_audioPlayer;
}
@property (atomic,
readonly)BOOL _isSpeakerOn;
@property (atomic,
readonly)BOOL _isHeadsetOn;
+(AudioManager*)shared;
// 打开扬声器
- (void)setSpeakerOn;
// 关闭扬声器
- (void)setSpeakerOff;
@end
//
// AudioManager.m
// AvconNetAndCallDemo
//
// Created by zjq on 14-7-21.
// Copyright (c) 2014年 zjq. All rights reserved.
//
#import "AudioManager.h"
#import <UIKit/UIKit.h>
#define IOSVersion [[UIDevice currentDevice].systemVersion floatValue]
@implementation AudioManager
@synthesize _isSpeakerOn;
@synthesize _isHeadsetOn;
- (id)init
{
self = [super
init];
if (self) {
_isSpeakerOn = NO;
AVAudioSession *audioSession = [AVAudioSession
sharedInstance];
//默认情况下扬声器播放
[audioSession setCategory:AVAudioSessionCategoryPlayback
withOptions:AVAudioSessionCategoryOptionMixWithOthers
error:nil];
[audioSession
setActive:YES
error:nil];
UInt32 sessionCategory =
kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory,
sizeof(sessionCategory),
&sessionCategory);
UInt32 audioRouteOverride =
kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,
sizeof (audioRouteOverride),
&audioRouteOverride);
AudioSessionAddPropertyListener (kAudioSessionProperty_AudioRouteChange,
audioRouteChangeListenerCallback, (__bridge
void *)(self));
}
return
self;
}
static
AudioManager *_audioManager =
NULL;
+(AudioManager*)shared
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_audioManager = [[AudioManager
alloc]
init];
});
return
_audioManager;
}
- (void)setSpeakerOn
{
NSLog(@"setSpeakerOn:%d",[NSThread
isMainThread]);
UInt32 doChangeDefaultRoute =
kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryDefaultToSpeaker,
sizeof (doChangeDefaultRoute),
&doChangeDefaultRoute
);
_isSpeakerOn = [self
checkSpeakerOn];
_isHeadsetOn = NO;
//[self resetOutputTarget];
}
- (void)setSpeakerOff
{
UInt32 doChangeDefaultRoute =
kAudioSessionOverrideAudioRoute_None;
AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryDefaultToSpeaker,
sizeof (doChangeDefaultRoute),
&doChangeDefaultRoute
);
_isSpeakerOn = [self
checkSpeakerOn];
}
- (BOOL)checkSpeakerOn
{
CFStringRef route;
UInt32 propertySize =
sizeof(CFStringRef);
AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &route);
if((route == NULL) || (CFStringGetLength(route) ==
))
{
// Silent Mode
NSLog(@"AudioRoute: SILENT, do nothing!");
}
else
{
NSString* routeStr = (__bridge
NSString*)route;
NSRange speakerRange = [routeStr
rangeOfString : @"Speaker"];
if (speakerRange.location !=
NSNotFound)
return YES;
}
return
NO;
}
- (BOOL)hasHeadset
{
CFStringRef route;
UInt32 propertySize =
sizeof(CFStringRef);
AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &route);
if((route == NULL) || (CFStringGetLength(route) ==
))
{
// Silent Mode
NSLog(@"AudioRoute: SILENT, do nothing!");
}
else
{
NSString* routeStr = (__bridge
NSString*)route;
NSLog(@"AudioRoute: %@", routeStr);
if ([routeStr isEqualToString:@"ReceiverAndMicrophone"]) {
// static dispatch_once_t onceToken;
// dispatch_once(&onceToken, ^{
// [self setSpeakerOn];
// });
[self
setSpeakerOn];
}
NSRange headphoneRange = [routeStr
rangeOfString : @"Headphone"];
NSRange headsetRange = [routeStr
rangeOfString : @"Headset"];
if (headphoneRange.location !=
NSNotFound)
{
return YES;
}
else if(headsetRange.location !=
NSNotFound)
{
return YES;
}
}
return
NO;
}
// 推断麦克风是否实用
- (BOOL)hasMicphone
{
return [[AVAudioSession
sharedInstance] isInputAvailable];
}
- (void)erjiOutPutTarget
{
BOOL hasHeadset = [self
hasHeadset];
if (hasHeadset) {
_isHeadsetOn = YES;
}
NSLog (@"Will Set output target is_headset = %@ .", hasHeadset?@"YES":@"NO");
UInt32 audioRouteOverride =
kAudioSessionOverrideAudioRoute_None;
AudioSessionSetProperty(kAudioSessionProperty_OverrideAudioRoute,
sizeof(audioRouteOverride), &audioRouteOverride);
}
- (void)resetOutputTarget
{
BOOL hasHeadset = [self
hasHeadset];
NSLog (@"Will Set output target is_headset = %@ .", hasHeadset?@"YES":@"NO");
UInt32 audioRouteOverride =
kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty(kAudioSessionProperty_OverrideAudioRoute,
sizeof(audioRouteOverride), &audioRouteOverride);
_isHeadsetOn =
NO;
}
void audioRouteChangeListenerCallback (void *inUserData,
AudioSessionPropertyID inPropertyID,
UInt32 inPropertyValueS, const
void *inPropertyValue)
{
if (inPropertyID !=
kAudioSessionProperty_AudioRouteChange)
return;
// Determines the reason for the route change, to ensure that it is not
// because of a category change.
CFDictionaryRef routeChangeDictionary = (CFDictionaryRef)inPropertyValue;
CFNumberRef routeChangeReasonRef = (CFNumberRef)CFDictionaryGetValue (routeChangeDictionary,
CFSTR (kAudioSession_AudioRouteChangeKey_Reason));
SInt32 routeChangeReason;
CFNumberGetValue (routeChangeReasonRef,
kCFNumberSInt32Type, &routeChangeReason);
NSLog(@"<<<RouteChangeReason: %d",(int)routeChangeReason);
NSLog(@"[=======%@",inUserData);
AudioManager *pMgr = (__bridge
AudioManager *)inUserData;
//没有耳机
if (routeChangeReason ==
kAudioSessionRouteChangeReason_OldDeviceUnavailable)
{
[pMgr
setSpeakerOn];
[pMgr resetOutputTarget];
}
else
if (routeChangeReason ==
kAudioSessionRouteChangeReason_NewDeviceAvailable)
{
[pMgr
erjiOutPutTarget];
}else
if (routeChangeReason ==
kAudioSessionRouteChangeReason_Override){
[pMgr
setSpeakerOn];
[pMgr resetOutputTarget];
}
NSLog(@"-------->%f",IOSVersion);
//if (IOSVersion >= 8.0) {
) {
[pMgr
hasHeadset];
}
//}
}
//- (BOOL)isAirplayActived
//{
// CFDictionaryRef currentRouteDescriptionDictionary = nil;
// UInt32 dataSize = sizeof(currentRouteDescriptionDictionary);
// AudioSessionGetProperty(kAudioSessionProperty_AudioRouteDescription, &dataSize, ¤tRouteDescriptionDictionary);
//
// BOOL airplayActived = NO;
// if (currentRouteDescriptionDictionary)
// {
// CFArrayRef outputs = CFDictionaryGetValue(currentRouteDescriptionDictionary, kAudioSession_AudioRouteKey_Outputs);
// if(outputs != NULL && CFArrayGetCount(outputs) > 0)
// {
// CFDictionaryRef currentOutput = CFArrayGetValueAtIndex(outputs, 0);
// //Get the output type (will show airplay / hdmi etc
// CFStringRef outputType = CFDictionaryGetValue(currentOutput, kAudioSession_AudioRouteKey_Type);
//
// airplayActived = (CFStringCompare(outputType, kAudioSessionOutputRoute_AirPlay, 0) == kCFCompareEqualTo);
// }
// CFRelease(currentRouteDescriptionDictionary);
// }
// return airplayActived;
//}
/*
- (void)openloudspeaker{
//初始化播放器的时候例如以下设置
UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory,
sizeof(sessionCategory),
&sessionCategory);
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,
sizeof (audioRouteOverride),
&audioRouteOverride);
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
//默认情况下扬声器播放
[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
[audioSession setActive:YES error:nil];
[self handleNotification:YES];
}
#pragma mark - 监听听筒or扬声器
- (void) handleNotification:(BOOL)state
{
[[UIDevice currentDevice] setProximityMonitoringEnabled:state]; //建议在播放之前设置yes。播放结束设置NO,这个功能是开启红外感应
if(state)//加入监听
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(sensorStateChange:) name:@"UIDeviceProximityStateDidChangeNotification"
object:nil];
else//移除监听
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"UIDeviceProximityStateDidChangeNotification" object:nil];
}
//处理监听触发事件
-(void)sensorStateChange:(NSNotificationCenter *)notification;
{
//假设此时手机靠近面部放在耳朵旁,那么声音将通过听筒输出,并将屏幕变暗(省电啊)
if ([[UIDevice currentDevice] proximityState] == YES)
{
NSLog(@"Device is close to user");
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
}
else
{
NSLog(@"Device is not close to user");
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
}
}
*/
@end
iOS 打开扬声器以及插入耳机的操作的更多相关文章
- 电脑没有声音,显示“未插入耳机或扬声器”,检测不到Realtek高清晰音频管理器
2018-7-16,电脑彻夜未关,早上发现已经死机了.关机重启之后,就发现没有声音了,提示“未插入耳机或扬声器”,并且检测不到Realtek高清晰音频管理器,只能检查到显卡音频输出.首先,音箱在其他电 ...
- 腾讯优测优分享 | 你是否体验过Android手机插入耳机后仍外放的尴尬?
腾讯优测是专业的移动自动化测试平台,提供多维度的自动化测试服务,让测试更简单! 近期有报道称,澳大利亚悉尼市新某大学的一名男生在课堂上看电影,不料耳机没有插好,变成了现场直播... 如果你认为耳机没插 ...
- 【win10】大水牛主机插入耳机没有声音
主机:大水牛,技嘉主板 操作系统:win10 问题:主机前面插入耳机,没有声音,扬声器图标出错 解决 一..插入耳机 二..Realtek高清晰音频管理器 1.打开音频管理器,点击右下角的设置 2.点 ...
- win10大水牛主机插入耳机没有声音
主机:大水牛,技嘉主板 操作系统:win10 问题:主机前面插入耳机,没有声音,扬声器图标出错 解决 一..插入耳机 二..Realtek高清晰音频管理器 1.打开音频管理器,点击右下角的设置 2.点 ...
- 为什么我的mac插入耳机耳机没有声音呢?
macOS 系统莫名其妙就遇到声音和音频播放问题的情况相当普遍,在新添音频设备.应用程序之间进行切换或更新操作系统后,都可能会遇到音频错误.好加在,解决大多数 macOS 声音无法正常工作的方法都非常 ...
- iOS 打开本地 其他应用程序(URL Types)
iOS 打开本地其他应用程序(URL Types) /*前言废话:Xcode是神奇的,是我所见到的编译器中最为神奇的,如:它可以同时运行两个甚至更多Project到我们模拟器上,可以同时使用一个模拟器 ...
- IOS学习之IOS沙盒(sandbox)机制和文件操作
IOS学习之IOS沙盒(sandbox)机制和文件操作(一) 1.IOS沙盒机制 IOS应用程序只能在为该改程序创建的文件系统中读取文件,不可以去其它地方访问,此区域被成为沙盒,所以所有的非代码文件都 ...
- ios 推送app badge 数字累加操作
ios 推送app badge 数字累加操作: 一:此数字需要后台配合: 二:大致原理: 后台发推送时,第一次 传badge 为1,往后,依次累加操作即可: 当用户打开app时,app向后台发送请求, ...
- 解决window10系统电脑插入耳机之后没有声音的问题
其实办法也是从百度百科上查到的 ⁄(⁄ ⁄•⁄ω⁄•⁄ ⁄)⁄ 可能是因为自己某个不小心的操作更改了设置 1. 首先要点开设置按钮,在搜索栏输入控制面板 (当然知道控制面板在哪里的小伙伴就不用 ...
随机推荐
- C语言指针的理解以及指针的指针的理解
指针指向的是内存地址编号,内存地址编号指向的是对应的内容. 我们需要一个变量,来储存内存地址编号,这个变量的值是一个内存地址编号,但是我们可以通过修改变量的值,来不断的改变内存地址编号. 但是,我们如 ...
- Hue - Error loading MySQLdb module: libmysqlclient.so.20: cannot open shared object file: No such file or
解决下面两点异常 >> 1. Hue页面 点击DB 查询时弹出: Error loading MySQLdb module: libmysqlclient.so.20: cannot op ...
- js中时钟表盘
1.js时钟表盘 代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...
- postgresql遇到的性能问题
问题SQL scwksmlcls.wk_cls_c , scwklrgcls.wk_lrg_cls_nm , scwkmdlcls.wk_mdl_cls_nm , scwksmlcls.wk_sml_ ...
- mongo 3.4分片集群系列之一:浅谈分片集群
这篇为理论篇,稍后会有实践篇. 这个系列大致想跟大家分享以下篇章: 1.mongo 3.4分片集群系列之一:浅谈分片集群 2.mongo 3.4分片集群系列之二:搭建分片集群--哈希分片 3.mong ...
- 本地调试hbase
需求说明 如果要本地调试Hbase程序,那么可以用本地连接集群的方式 配置文件 在maven里,配置文件cluster.properties放在target/classes里 cluster.prop ...
- 机器学习_K近邻Python代码详解
k近邻优点:精度高.对异常值不敏感.无数据输入假定:k近邻缺点:计算复杂度高.空间复杂度高 import numpy as npimport operatorfrom os import listdi ...
- JavaScipt30(第一个案例)(主要知识点:键盘事件以及transitionend)
今天得到一个github练习项目,是30个原生js写成的小例子,麻雀虽小五脏俱全,现在记录一下第一个. 第一个是键盘按键时页面上对应的键高亮,同时播放音频,松开后不再高亮. 我自己实现了一下,然后查看 ...
- ThinkPHP---ue编辑器
[一]简介 (1)介绍 一款百度开发的JS插件 ue编辑器,全称Ueditor(翻译为你的编辑器),百度开发的在线编辑器,是一款在线编辑器软件,在线编辑器又称为“富文本编辑器”. 国外也有一款类似的编 ...
- ThinkPHP---框架介绍
(1)什么是框架? ①框架是一堆包含了常量.方法和类等代码集合: ②半成品应用,只包含了项目开发时的底层架构,并不包含业务逻辑: ③包含一些设计模式,例如单例模式,工厂模式,AR(Active Rec ...