美文网首页
Swift3.0 录音使用

Swift3.0 录音使用

作者: BrumeLoong | 来源:发表于2017-11-11 15:46 被阅读0次

关于录音反正都是调用系统的东西,没啥好说的,直接上流程


var recorder:AVAudioRecorder? //录音器

var recorderSeetingsDic:[String : Any]! //录音器设置参数数组

var volumeTimer:Timer! //定时器线程,循环监测录音的音量大小

var aacPath:String! //录音存储路径

let docDir = NSSearchPathForDirectoriesInDomains(.documentDirectory,

.userDomainMask, true)[0]//获取手机根目录

var musicPlayer = AVAudioPlayer()//播放器

//初始化录音器

let session:AVAudioSession = AVAudioSession.sharedInstance()

//设置录音类型

try! session.setCategory(AVAudioSessionCategoryPlayAndRecord)

//初始化字典并添加设置参数

recorderSeetingsDic =

[

AVFormatIDKey: NSNumber(value: kAudioFormatMPEG4AAC),

AVNumberOfChannelsKey: 2, //录音的声道数,立体声为双声道

AVEncoderAudioQualityKey : AVAudioQuality.max.rawValue,

AVEncoderBitRateKey : 320000,

AVSampleRateKey : 44100.0 //录音器每秒采集的录音样本数

]

判断路径文件夹是否存在

recordPath = docDir + "/sound"

let fileManager = FileManager.default

//        let exist = fileManager.fileExists(atPath: recordPath)�

let exist = fileManager.fileExists(atPath: recordPath)

if exist == false {

try! fileManager.createDirectory(atPath: recordPath,withIntermediateDirectories: true, attributes: nil)

//            try! fileManager

}

let currentDate = Date()

let dateFormatter = DateFormatter()

dateFormatter.dateFormat = "yyyyMMddHHmmss"

let myStr = dateFormatter.string(from: currentDate)

aacPath = recordPath + "/\(myStr).aac"

print(aacPath)

recorder = try! AVAudioRecorder(url: URL(string: aacPath)!,settings: recorderSeetingsDic!)

if recorder != nil {

//开启仪表计数功能

recorder!.isMeteringEnabled = true

//准备录音

recorder!.prepareToRecord()

//开始录音

recorder!.record()

}

//停止录音

recorder?.stop()

//录音器释放

recorder = nil

//暂停定时器

volumeTimer.invalidate()

volumeTimer = nil

开始播放

func startPlay() {

if (userDefult.value(forKey: "mySelectBtn") == nil) {

print(myIndex)

userDefult.setValue(myIndex.row, forKey: "mySelectBtn")

playSound()

}else{

linkRow = userDefult.value(forKey: "mySelectBtn") as! Int

if linkRow != myIndex.row {

let myindex :IndexPath = IndexPath(row: linkRow, section: 0)

userDefult.setValue(myIndex.row, forKey: "mySelectBtn")

DispatchQueue.main.async {

let mycell = self.recordTable.cellForRow(at: myindex) as! RecordTableViewCell

mycell.playBtn.isSelected = false

}

}

playSound()

}

}

func playSound(){

let sound = recordPath + "/\(titleArr[myIndex.row])"

//        播放

print(sound)

player = try! AVAudioPlayer(contentsOf: URL(string: sound)!)

player?.delegate = self

if player == nil {

print("播放失败")

}else{

player?.volume = 1.0

player?.prepareToPlay()

player?.play()

}

}

暂停播放

func stopPlay(){

if player == nil {

print("停止失败")

}else{

player?.currentTime = 0

player?.stop()

}

}

//播放完成监听

func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {

print("owari")

let cell = self.recordTable.cellForRow(at: myIndex) as! RecordTableViewCell

cell.playBtn.isSelected = false

}

录音的使用大致就是这样,只是还有小部分代码没有放上来,不过基本的流程就是这样的,等我时间充裕点再把录音单独从项目里面拿出来。

相关文章

网友评论

      本文标题:Swift3.0 录音使用

      本文链接:https://www.haomeiwen.com/subject/hcowmxtx.html