gpt4 book ai didi

ios - swift 代码在 Xcode 7 中不起作用(setCategory、AVFormatIDKey、AVAudioRecorder)

转载 作者:行者123 更新时间:2023-11-29 01:33:31 26 4
gpt4 key购买 nike

我有一段代码以前可以在 Xcode 6 中运行,但现在不能在 Xcode 7 中运行。我在这里有一张它的图片 errors

我收到 3 个关于“无法分配值”、“无法调用initializer", "无法调用 Setcategory",

另外,这是代码,您知道我做错了什么吗?

最好的,

import UIKit
import AVFoundation

class RecordViewController: UIViewController {

required init?(coder aDecoder: NSCoder) {


var baseString : String = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0] as String
self.audioURL = "sound.m4a"
var pathComponents = [baseString, self.audioURL]
var audioNSURL = NSURL.fileURLWithPathComponents(pathComponents)
var session = AVAudioSession.sharedInstance()
session.setCategory(AVAudioSessionCategoryPlayAndRecord, error: nil)

var recordSettings: [NSObject : AnyObject] = Dictionary()
recordSettings[AVFormatIDKey] = kAudioFormatMPEG4AAC
recordSettings[AVSampleRateKey] = 44100.0
recordSettings[AVNumberOfChannelsKey] = 2

self.audioRecorder = AVAudioRecorder(URL: audioNSURL, settings: recordSettings, error: nil)
self.audioRecorder.meteringEnabled = true
self.audioRecorder.prepareToRecord()

super.init(coder: aDecoder)
}


@IBOutlet weak var recordButton: UIButton!
@IBOutlet weak var playButton: UIButton!
@IBOutlet weak var saveButton: UIBarButtonItem!

var audioRecorder : AVAudioRecorder
var audioURL = ""

override func viewDidLoad() {
super.viewDidLoad()

self.playButton.enabled = false
self.saveButton.enabled = false

// Do any additional setup after loading the view.
}

@IBAction func cancelTapped(sender: AnyObject) {
self.dismissViewControllerAnimated(true, completion: nil)
}

@IBAction func saveTapped(sender: AnyObject) {
}

@IBAction func recordTapped(sender: AnyObject) {
self.playButton.enabled = true
}

@IBAction func playTapped(sender: AnyObject) {
}

最佳答案

试试这个:

do {
try session.setCategory(AVAudioSessionCategoryPlayAndRecord)
} catch (_) {

}

var recordSettings: [String : AnyObject] = Dictionary()
recordSettings[AVFormatIDKey] = NSNumber(unsignedInt: kAudioFormatMPEG4AAC)
recordSettings[AVSampleRateKey] = 44100.0
recordSettings[AVNumberOfChannelsKey] = 2

self.audioRecorder = AVAudioRecorder()

do {
self.audioRecorder = try AVAudioRecorder(URL: audioNSURL!, settings: recordSettings)
} catch (_) {

}

基本上,使用 Swift 2,您需要以稍微不同的方式处理异常和类型转换。

关于ios - swift 代码在 Xcode 7 中不起作用(setCategory、AVFormatIDKey、AVAudioRecorder),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33206443/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com