iOS Swift-使用蓝牙外部扬声器时音质不佳 [英] iOS Swift - Poor audio quality when using Bluetooth external speaker

查看:77
本文介绍了iOS Swift-使用蓝牙外部扬声器时音质不佳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用从网址(广播应用)流式传输的iOS应用,一旦我试图通过蓝牙设计(例如外部扬声器或汽车音频系统)从应用程序流式传输,音频质量就会变差且刺耳.从iOS设备本身播放时,一切听起来都不错(扬声器和耳机).

Im using iOS app that stream from url (radio app), once im trying to stream from the app through Bluetooth devise like external speaker or car audio system, the audio quality is vert poor and jarring. When playing from the iOS devise itself, everything sounds good (speaker and earphones).

    override func viewDidLoad() {
    super.viewDidLoad()


    // Set AVFoundation category, required for background audio
    var error: NSError?
    var success: Bool


    do {

        try AVAudioSession.sharedInstance().setCategory(
            AVAudioSessionCategoryPlayAndRecord,

            withOptions: [
                AVAudioSessionCategoryOptions.AllowBluetooth,
                AVAudioSessionCategoryOptions.DefaultToSpeaker])
        success = true
    } catch let error1 as NSError {
        error = error1
        success = false
    }

    if !success {
        print("Failed to set audio session category.  Error: \(error)")
    }

推荐答案

感谢Michal Cichon的评论,该代码终于对我有用,并且蓝牙没有失真

Thanks to Michal Cichon's comment this code finally works for me and the bluetooth isn't distorted

        do{
            if #available(iOS 10.0, *) {
                try AVAudioSession.sharedInstance().setCategory(.playAndRecord, 
                mode: .default, options: [.mixWithOthers, .allowAirPlay, 
                .allowBluetoothA2DP,.defaultToSpeaker])
                print("ios 10 and above")

            } else {
                 print("ios 10 and lower")
                let options: [AVAudioSession.CategoryOptions] = 
                [.mixWithOthers, .allowBluetooth]
                let category = AVAudioSession.Category.playAndRecord
                let selector = 
                NSSelectorFromString("setCategory:withOptions:error:")
                AVAudioSession.sharedInstance().perform(selector, with: 
                 category, with: options)
            }

            try AVAudioSession.sharedInstance().setActive(true)
            captureSession.automaticallyConfiguresApplicationAudioSession = 
                false
         }

这篇关于iOS Swift-使用蓝牙外部扬声器时音质不佳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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