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. 首先要点开设置按钮,在搜索栏输入控制面板 (当然知道控制面板在哪里的小伙伴就不用 ...
随机推荐
- Dojo - 操作Dom的函数
DOM Manipulation You might be seeing a trend here if you have gotten this far in the tutorial, in th ...
- python自动化--语言基础五面向对象、迭代器、range和切片的区分
面向对象 一.面向对象简单介绍: class Test(): #类的定义 car = "buick" #类变量,定义在类里方法外,可被对象直接调用,具有全局效果 def __ini ...
- PHP——基本使用(二)
PHP与Apache Apache服务器在接受到客户端请求的时候,根据客户端所请求的文件的类型,然后去问模块能否处理此文件,php作为模块之一有可能可以处理此文件,处理之后将数据再返回给apache, ...
- (转)淘淘商城系列——使用Spring来管理Redis单机版和集群版
http://blog.csdn.net/yerenyuan_pku/article/details/72863323 我们知道Jedis在处理Redis的单机版和集群版时是完全不同的,有可能在开发的 ...
- 从输入URL到网页呈现的过程
1.域名解析当我们在浏览器中输入一个URL,例如”www.google.com”时,这个地址并不是谷歌网站真正意义上的地址.互联网上每一台计算机的唯一标识是它的IP地址,因此我们输入的网址首先需要先解 ...
- Vue指令3:v-for
列表渲染 我们用 v-for 指令根据一组数组的选项列表进行渲染.v-for 指令需要使用item in items 形式的特殊语法,items 是源数据数组并且 item 是数组元素迭代的别名. & ...
- CMU Database Systems - Two-phase Locking
首先锁是用来做互斥的,解决并发执行时的数据不一致问题 如图会导致,不可重复读 如果这里用lock就可以解决,数据库里面有个LockManager来作为master,负责锁的记录和授权 数据库里面的基本 ...
- 【maven】Description Resource Path Location Type An error occurred while filtering resources TESTVIDEO line
在maven中构建项目的时候发现了如下错误: Description Resource Path Location Type An error occurred while filtering res ...
- 学习笔记7——使用Scanner获取键盘输入
使用Scanner类可以很方面地获取用户的键盘输入,Scanner是一个基于正则表达式的文本扫描器,它可以从文件.输入流.字符串中解析出基本类型值和字符串值.Scanner类提供了多个构造器,不同的构 ...
- Window下的———TOMCAT环境的配置
1. 先去官方网站下载需要的猫(tomcat) http://tomcat.apache.org/ 2.下载好包,然后解压出来,放在你需要的位置上 3.去到配环境变量的地方,进行相应的环境配置 ...