我有以下问题.我想转换我的旧函数(直到Swift 3 beta 5):
func binarytotype <T> (_ value: [UInt8], _: T.Type) -> T
{
return value.withUnsafeBufferPointer
{
return UnsafePointer<T>($0.baseAddress!).pointee
}
}
Run Code Online (Sandbox Code Playgroud)
转到Swift 3 beta 6语法.此函数将UInt8的数组转换为另一种类型,例如:
let b: [UInt8] = [1,2,3,4,5,6,7,8]
var number: Double = binarytotype(b, Double.self)
Run Code Online (Sandbox Code Playgroud)
但是现在这在beta 6中不再起作用了,我必须使用withMemoryRebound,但我真的不知道,如何让它运行.有谁能够帮我?
相反的功能是:
func typetobinary <T> (_ value: T) -> [UInt8]
{
var v: T = value
return withUnsafePointer(to: &v)
{
Array(UnsafeBufferPointer(start: UnsafePointer<UInt8>($0), count: MemoryLayout<T>.size))
}
}
Run Code Online (Sandbox Code Playgroud)
这也不再适用.同样的问题.我的一些项目都需要这两个项目.这个反向函数被称为:
var binary: [UInt8] = typetobinary(number)
Run Code Online (Sandbox Code Playgroud) 我一直在尝试将AVAudioPCMBuffer转换为[UInt8],这是我到目前为止所拥有的.
请注意,我从这个答案中找到了Rhythmic Fistman的这种方法.
func audioBufferToBytes(audioBuffer: AVAudioPCMBuffer) -> [UInt8] {
let srcLeft = audioBuffer.floatChannelData![0]
let bytesPerFrame = audioBuffer.format.streamDescription.pointee.mBytesPerFrame
let numBytes = Int(bytesPerFrame * audioBuffer.frameLength)
// initialize bytes by 0
var audioByteArray = [UInt8](repeating: 0, count: numBytes)
srcLeft.withMemoryRebound(to: UInt8.self, capacity: numBytes) { srcByteData in
audioByteArray.withUnsafeMutableBufferPointer {
$0.baseAddress!.initialize(from: srcByteData, count: numBytes)
}
}
return audioByteArray
}
Run Code Online (Sandbox Code Playgroud)
最后这是我的方法,从上面的相同答案中找到.
func bytesToAudioBuffer(_ buf: [UInt8]) -> AVAudioPCMBuffer {
let fmt = AVAudioFormat(commonFormat: .pcmFormatFloat32, sampleRate: 44100, channels: 1, interleaved: true)
let frameLength = UInt32(buf.count) …Run Code Online (Sandbox Code Playgroud) 我是Swift/iOS的新手,几天前才开始学习.我正在使用Swift 3并希望开发两个iPhone应用程序,可以使用多对等连接将音频流从麦克风发送到其他iPhone设备.第一个应用程序将是扬声器的应用程序,另一个应用程序将是监听器的应用程序.
以前,我从这个有用的教程中学习了如何广告,浏览和邀请同伴
我学会了如何从麦克风获取音频数据并将其转换为来自此答案和答案的字节.非常感谢Rhythmic Fistman.
所以,我的代码是这些文章包含的内容的组合.
这是侦听器应用程序的ViewController
import UIKit
import MultipeerConnectivity
import AVFoundation
class ColorSwitchViewController: UIViewController {
@IBOutlet weak var connectionsLabel: UILabel!
let colorService = ColorServiceManager()
var engine = AVAudioEngine()
let player = AVAudioPlayerNode()
// Somewhere, schedule the stream in the mainRunLoop, set the delegate and open it. Choose the peer that you want to connect
var inputStream = InputStream()
var inputStreamIsSet: Bool!
var outputStreamIsSet: Bool!
public let peerID = MCPeerID(displayName: UIDevice.current.name) …Run Code Online (Sandbox Code Playgroud) 我已经用谷歌搜索和研究了好几天,但我似乎无法让它工作,我在互联网上找不到任何解决方案。
我正在尝试使用麦克风捕捉我的声音,然后通过扬声器播放。
这是我的代码:
class ViewController: UIViewController, AVAudioRecorderDelegate, AVCaptureAudioDataOutputSampleBufferDelegate {
var recordingSession: AVAudioSession!
var audioRecorder: AVAudioRecorder!
var captureSession: AVCaptureSession!
var microphone: AVCaptureDevice!
var inputDevice: AVCaptureDeviceInput!
var outputDevice: AVCaptureAudioDataOutput!
override func viewDidLoad() {
super.viewDidLoad()
recordingSession = AVAudioSession.sharedInstance()
do{
try recordingSession.setCategory(AVAudioSessionCategoryPlayAndRecord)
try recordingSession.setMode(AVAudioSessionModeVoiceChat)
try recordingSession.setPreferredSampleRate(44000.00)
try recordingSession.setPreferredIOBufferDuration(0.2)
try recordingSession.setActive(true)
recordingSession.requestRecordPermission() { [unowned self] (allowed: Bool) -> Void in
DispatchQueue.main.async {
if allowed {
do{
self.microphone = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeAudio)
try self.inputDevice = AVCaptureDeviceInput.init(device: self.microphone)
self.outputDevice = AVCaptureAudioDataOutput()
self.outputDevice.setSampleBufferDelegate(self, queue: DispatchQueue.main)
self.captureSession = AVCaptureSession()
self.captureSession.addInput(self.inputDevice) …Run Code Online (Sandbox Code Playgroud)