[英]Detect (listen to) audio route change in iOS 7
剛開始為iOS 7開發,發現iOS 7中不推薦使用AudioSession相關的函數和PropertyListeners。
在我使用以下方法檢測耳機是否已插入或從設備拔出之前:
/* add callback for device route change */
AudioSessionAddPropertyListener (
kAudioSessionProperty_AudioRouteChange,
audioRouteChangeListenerCallback,
(__bridge void *)(self));
然后實現監聽器回調對內部算法做不同的事情。 現在 iOS 7 棄用了它,並且沒有任何替代方案的文檔,這里有專家提供的任何解決方案嗎? 謝謝!
處理通知AVAudioSessionRouteChangeNotification
(適用於 iOS 6.0 及更高版本。)
試試這個Swift 4.2
代碼:
@objc func handleRouteChange(_ notification: Notification) {
let reasonValue = (notification as NSNotification).userInfo![AVAudioSessionRouteChangeReasonKey] as! UInt
let routeDescription = (notification as NSNotification).userInfo![AVAudioSessionRouteChangePreviousRouteKey] as! AVAudioSessionRouteDescription?
NSLog("Route change:")
if let reason = AVAudioSession.RouteChangeReason(rawValue: reasonValue) {
switch reason {
case .newDeviceAvailable:
NSLog(" NewDeviceAvailable")
case .oldDeviceUnavailable:
NSLog(" OldDeviceUnavailable")
case .categoryChange:
NSLog(" CategoryChange")
NSLog(" New Category: %@", AVAudioSession.sharedInstance().category.rawValue)
case .override:
NSLog(" Override")
case .wakeFromSleep:
NSLog(" WakeFromSleep")
case .noSuitableRouteForCategory:
NSLog(" NoSuitableRouteForCategory")
case .routeConfigurationChange:
NSLog(" RouteConfigurationChange")
case .unknown:
NSLog(" Unknown")
@unknown default:
NSLog(" UnknownDefault(%zu)", reasonValue)
}
} else {
NSLog(" ReasonUnknown(%zu)", reasonValue)
}
if let prevRout = routeDescription {
NSLog("Previous route:\n")
NSLog("%@", prevRout)
NSLog("Current route:\n")
NSLog("%@\n", AVAudioSession.sharedInstance().currentRoute)
}
}
並在func setupAudioSession()
調用它
private func setupAudioSession() {
// Configure the audio session
let sessionInstance = AVAudioSession.sharedInstance()
// we don't do anything special in the route change notification
NotificationCenter.default.addObserver(self,
selector: #selector(self.handleRouteChange(_:)),
name: AVAudioSession.routeChangeNotification,
object: sessionInstance)
}
對於Objective C
試試這個代碼
- (void)handleRouteChange:(NSNotification *)notification
{
UInt8 reasonValue = [[notification.userInfo valueForKey:AVAudioSessionRouteChangeReasonKey] intValue];
AVAudioSessionRouteDescription *routeDescription = [notification.userInfo valueForKey:AVAudioSessionRouteChangePreviousRouteKey];
NSLog(@"Route change:");
switch (reasonValue) {
case AVAudioSessionRouteChangeReasonNewDeviceAvailable:
NSLog(@" NewDeviceAvailable");
break;
case AVAudioSessionRouteChangeReasonOldDeviceUnavailable:
NSLog(@" OldDeviceUnavailable");
break;
case AVAudioSessionRouteChangeReasonCategoryChange:
NSLog(@" CategoryChange");
NSLog(@" New Category: %@", [[AVAudioSession sharedInstance] category]);
break;
case AVAudioSessionRouteChangeReasonOverride:
NSLog(@" Override");
break;
case AVAudioSessionRouteChangeReasonWakeFromSleep:
NSLog(@" WakeFromSleep");
break;
case AVAudioSessionRouteChangeReasonNoSuitableRouteForCategory:
NSLog(@" NoSuitableRouteForCategory");
break;
default:
NSLog(@" ReasonUnknown");
}
NSLog(@"Previous route:\n");
NSLog(@"%@\n", routeDescription);
NSLog(@"Current route:\n");
NSLog(@"%@\n", [AVAudioSession sharedInstance].currentRoute);
}
並在(void)setupAudioSession
調用它
- (void)setupAudioSession {
// Configure the audio session
AVAudioSession *sessionInstance = [AVAudioSession sharedInstance];
// we don't do anything special in the route change notification
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleRouteChange:)
name:AVAudioSessionRouteChangeNotification
object:sessionInstance];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.