我是中国开发人员,所以我的英语不好,我在Appdelegate中设置播放背景时遇到了一些AudioKit问题, 我写了一些代码来播放音频背景:
import UIKit
import MediaPlayer
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
//设置音频后台播放
do {
try AVAudioSession.sharedInstance().setActive(true)
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
}catch{
}
UIApplication.shared.beginReceivingRemoteControlEvents()//设置后台波播放
return true
}
}
然后一个homeViewController是rootViewController,它可以推送recordViewController:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var toRecordButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
self.title = "首页"
}
@IBAction func toRecordAction(_ sender: Any) {
let vc = RecorderViewController()
self.navigationController?.pushViewController(vc, animated: true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
接下来是recordViewController的代码:
import UIKit
import AudioKitUI
import SnapKit
import MediaPlayer
class RecorderViewController: UIViewController {
private var fileForm = ".m4a"
private lazy var inputPlot:AKNodeOutputPlot = {
let plot = AKNodeOutputPlot.init(frame: CGRect.init(x: 20, y: 20, width: UIScreen.main.bounds.width - 40, height: 150))
return plot
}()
var micMixer: AKMixer!
var recorder: AKNodeRecorder!
var player: AKAudioPlayer!//播放背景音乐的播放器
var testPlayer:AKPlayer!//试听的播放器
var tape: AKAudioFile!
var micBooster: AKBooster!
var moogLadder: AKMoogLadder!
var delay: AKDelay!
var mainMixer: AKMixer!
let mic = AKMicrophone()//麦克风对象
fileprivate var micTracker:AKMicrophoneTracker?//话筒输入追踪器
fileprivate var playerTracker:AKAmplitudeTracker?//其他的追踪器
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.white
self.title = "录音"
self.setupViews()
self.stetupConfigs()
}
private func setupViews(){
self.view.addSubview(inputPlot)
let button = UIButton.init()
self.view.addSubview(button)
button.snp.makeConstraints { (make) in
make.center.equalTo(self.view.snp.center)
make.size.equalTo(CGSize.init(width: 80, height: 50))
}
button.setTitle("开始录音", for: .normal)
button.setTitle("停止录音", for: .selected)
button.setTitleColor(UIColor.black, for: .normal)
button.setTitleColor(UIColor.black, for: .selected)
button.addTarget(self, action: #selector(buttonClicked(sender:)), for: .touchUpInside)
}
private func stetupConfigs(){
// Clean tempFiles !
AKAudioFile.cleanTempDirectory()
// Session settings
AKSettings.bufferLength = .medium
do {
try AKSettings.setSession(category: .playAndRecord, with: .allowBluetoothA2DP)
} catch {
AKLog("Could not set session category.")
}
AKSettings.defaultToSpeaker = true
// Patching
inputPlot.node = mic
micMixer = AKMixer(mic)
micBooster = AKBooster(micMixer)
// Will set the level of microphone monitoring
micBooster.gain = 0
recorder = try? AKNodeRecorder(node: micMixer)
if let file = recorder.audioFile {
testPlayer = AKPlayer(audioFile: file)
}
testPlayer.isLooping = true
moogLadder = AKMoogLadder(testPlayer)
mainMixer = AKMixer(moogLadder, micBooster)
AudioKit.output = mainMixer
do {
try AudioKit.start()
} catch {
AKLog("AudioKit did not start!")
}
micBooster.gain = 0
}
@objc private func buttonClicked(sender:UIButton){
sender.isSelected = !sender.isSelected
if sender.isSelected == true {
do {
try recorder.record()
} catch { print("Errored recording.") }
}else{
recorder.stop()
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
当这些代码运行时,它打印出来:
2018-03-20 14:37:34.101644+0800 RecorderTest[18970:5568244]
[DYMTLInitPlatform] platform initialization successful
AKMicrophone.swift:init():45:Mixer inputs 8
2018-03-20 14:37:50.045692+0800 RecorderTest[18970:5568082] [aurioc]
918: failed: -10851 (enable 1, outf< 2 ch, 0 Hz, Float32, non-
inter> inf< 2 ch, 0 Hz, Float32, non-inter>)
AKAudioFile.swift:deleteFileWithFileName:139:"334A33E7-1237-447A-8A6A-
AA5F57CA5871.caf" deleted.
AKAudioFile.swift:cleanTempDirectory():157:1 files deleted
2018-03-20 14:37:50.482190+0800 RecorderTest[18970:5568082] [mcmx] 338:
input bus 0 sample rate is 0
2018-03-20 14:37:50.482772+0800 RecorderTest[18970:5568082] [avae]
AVAEInternal.h:103:_AVAE_CheckNoErr:
[AVAudioEngineGraph.mm:1266:Initialize: (err =
AUGraphParser::InitializeActiveNodesInOutputChain(ThisGraph,
kOutputChainOptimizedTraversal, *GetOutputNode(),
isOutputChainActive)): error -10875
2018-03-20 14:37:50.482822+0800 RecorderTest[18970:5568082] [avae]
AVAudioEngine.mm:149:-[AVAudioEngine prepare]: Engine@0x1038b0460:
could not initialize, error = -10875
2018-03-20 14:37:50.572746+0800 RecorderTest[18970:5568082] [mcmx] 338:
input bus 0 sample rate is 0
2018-03-20 14:37:50.572815+0800 RecorderTest[18970:5568082] [avae]
AVAEInternal.h:103:_AVAE_CheckNoErr:
[AVAudioEngineGraph.mm:1266:Initialize: (err =
AUGraphParser::InitializeActiveNodesInOutputChain(ThisGraph,
kOutputChainOptimizedTraversal, *GetOutputNode(),
isOutputChainActive)): error -10875
RecorderViewController.swift:stetupConfigs():90:AudioKit did not start!