由于一个单独的问题,我一直试图用AKPlayer替换我代码中的AKAudioPlayer,从而导致AKAudioPlayer的播放头在暂停后给出负数,并且不会更新。但是,在进行此切换之后,我遇到了一个单独的问题,即AKPlayer根本无法加载AKAudioFIle。这是我的代码的简单版本:
import UIKit
import AudioKit
import AudioKitUI
class ViewController: UIViewController
{
var microphone : AKMicrophone!
var mixer : AKMixer!
var waveformBooster: AKBooster!
var outputBooster : AKBooster!
var exportTape : AKAudioFile!
var recorder : AKNodeRecorder!
var player : AKPlayer!
var recordingTimer : Timer!
var playTimer : Timer!
@IBOutlet var startRecordingButton: CircularButton!
@IBOutlet var playAndPauseRecordingButton: UIButton!
override func viewDidLoad()
{
super.viewDidLoad()
microphone = AKMicrophone()
mixer = AKMixer(microphone)
AudioKit.output = mixer
try!AudioKit.start()
// Initialise file to store recorder output and set recorder to route mixer
// output to file
exportTape = try! AKAudioFile(name: "ExportTape")
recorder = try! AKNodeRecorder(node: mixer, file: exportTape)
// Create player with blank tape so that we have a reference player object
player = AKPlayer(audioFile: exportTape)
}
@IBAction func startRecording(_ sender: UIButton)
{
if player.isPlaying == false
{
// Delete contents of output file so it can be rewritten
try! recorder.reset()
microphone.start()
do
{
try recorder?.record()
} catch
{
AKLog("Couldn't record")
}
}
}
@IBAction func stopRecording(_ sender: UIButton)
{
microphone.stop()
recorder.stop()
}
@IBAction func playAndPauseRecording(_ sender: UIButton)
{
// Replace the empty or previous tape with the newly recorded one
//player.load(audioFile: exportTape) <-- This doesn't work either
var player = AKPlayer(audioFile: exportTape)
// TEST
print("CurrentTime \(player.currentTime)")
print("StartTime \(player.startTime)")
print("EndTime \(player.endTime)")
print("Duration \(player.duration)")
print("isPlaying \(player.isPlaying)")
// END TEST
// If something has been recorded...
if player.duration > 0
{
// If the player is currently playing...
if player.isPlaying == true
{
// ...and pause the tape
player.pause()
} else
{
AudioKit.output = player
player.play()
print("Playing...")
//player.resume()
}
}
}
}
使用.load()方法或创建新的播放器实例均不适用于我-输出如下:
CurrentTime nan
StartTime 0.0
EndTime 0.0
Duration 0.0
isPlaying false
奇怪的是
AKPlayer.swift:deinit:918:* deinit AKPlayer
这是否意味着在测试打印语句之后AKPlayer正在取消初始化?
需要明确的是,文件在作为AKAudioPlayer时会正确加载到播放器中,只是现在我将其更改为无法加载的AKPlayer。有人知道为什么是这样吗?任何帮助将非常感激。
编辑:
这是一段更短,更简单的代码,仍然显示错误:
import UIKit
import AudioKit
class ViewController: UIViewController
{
var file : AKAudioFile!
var player : AKPlayer!
var audioPlayer : AKAudioPlayer!
var microphone : AKMicrophone!
var mixer : AKMixer!
var recorder : AKNodeRecorder!
var booster : AKBooster!
@IBOutlet var startRecordingButton: UIButton!
@IBOutlet var stopRecordingButton: UIButton!
@IBOutlet var playRecordingButton: UIButton!
override func viewDidLoad()
{
super.viewDidLoad()
microphone = AKMicrophone()
mixer = AKMixer(microphone)
booster = AKBooster(mixer)
booster.gain = 0
AudioKit.output = booster
try!AudioKit.start()
file = try! AKAudioFile(name: "file")
recorder = try! AKNodeRecorder(node: mixer, file: file)
}
@IBAction func startRecording(_ sender: UIButton)
{
microphone.start()
print("Recording...")
do
{
try recorder?.record()
} catch
{
AKLog("Couldn't record")
}
}
@IBAction func stopRecording(_ sender: UIButton)
{
microphone.stop()
recorder.stop()
print("Recording Stopped")
}
@IBAction func playRecording(_ sender: UIButton)
{
audioPlayer = try! AKAudioPlayer(file: file)
player = AKPlayer(audioFile: file)
AudioKit.output = player
player.play()
// v this works! v
// AudioKit.output = audioPlayer
// audioPlayer.play()
}
}
最佳答案
即将出现的错误修复中已解决此问题。问题在于AKPlayer假定传入的AKAudioFile已打开以进行读取,而AKMicrophone的关联文件是为写入。
AKAudioPlayer创建了一个重复的文件对象,这就是它在那里工作的原因。应该很快更新!