问题
I am connecting to a socket which is transmitting audio wav file in 1024 byte chunks and I am looking for a swift library to play that. Just wondering if there is any.
Current state of my code is this.
import UIKit
import SwiftSocket
class ViewController: UIViewController {
let port : UInt32 = UInt32(8190)
let host = "localhost"
let BUFF_SIZE = 1024
let SECRET = "MYSECRET"
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let client = TCPClient(address: host, port: Int32(port))
switch client.connect(timeout: 1) {
case .success:
switch client.send(data: SECRET.data(using: .utf8) ?? Data() ) {
case .success:
var data = [UInt8]()
while true {
guard let response = client.read(BUFF_SIZE, timeout: 2) else { break }
data += response
// I am looking for a library which can play those bytes here.
}
case .failure(let error):
print(error)
}
case .failure(let error):
print(error)
}
}
}
Thanks
回答1:
The Audio Queue framework (a C API) can be initialized from Swift. An Audio Queue callback can be specified to provide for audio output of buffers of raw samples of various formats. You will have to remove (skip) the WAV file header after checking the format.
来源:https://stackoverflow.com/questions/56892135/ios-library-to-play-the-wav-file-coming-over-the-socket