checkVoiceRecordValue, appendSampleValue to voiceRecordSampleView

This commit is contained in:
nixzhu
2015-11-25 16:17:36 +08:00
parent 9a4fabe67b
commit b45f5e4be7
3 changed files with 32 additions and 1 deletions
@@ -92,6 +92,7 @@
</navigationItem>
<connections>
<outlet property="voiceRecordButton" destination="RHU-Kc-Wb9" id="sNA-HQ-sU2"/>
<outlet property="voiceRecordSampleView" destination="RCG-r4-rAb" id="08Y-Px-kgb"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="GYe-Vs-C7U" userLabel="First Responder" sceneMemberID="firstResponder"/>
@@ -11,6 +11,8 @@ import AVFoundation
class NewFeedVoiceRecordViewController: UIViewController {
@IBOutlet weak var voiceRecordSampleView: VoiceRecordSampleView!
@IBOutlet weak var voiceRecordButton: UIButton!
var isVoiceRecording = false {
@@ -22,10 +24,15 @@ class NewFeedVoiceRecordViewController: UIViewController {
var voiceFileURL: NSURL?
var displayLink: CADisplayLink!
override func viewDidLoad() {
super.viewDidLoad()
title = NSLocalizedString("New Voice", comment: "")
displayLink = CADisplayLink(target: self, selector: "checkVoiceRecordValue")
displayLink.addToRunLoop(NSRunLoop.currentRunLoop(), forMode: NSRunLoopCommonModes)
}
// MARK: - Actions
@@ -34,6 +41,20 @@ class NewFeedVoiceRecordViewController: UIViewController {
dismissViewControllerAnimated(true, completion: nil)
}
func checkVoiceRecordValue() {
if let audioRecorder = YepAudioService.sharedManager.audioRecorder {
if (audioRecorder.recording) {
audioRecorder.updateMeters()
let normalizedValue = pow(10, audioRecorder.averagePowerForChannel(0)/40)
let value = CGFloat(normalizedValue)
voiceRecordSampleView.appendSampleValue(value)
}
}
}
@IBAction func voiceRecord(sender: UIButton) {
if isVoiceRecording {
@@ -10,6 +10,8 @@ import UIKit
class VoiceRecordSampleView: UIView {
var sampleValues: [CGFloat] = []
lazy var sampleCollectionViewLayout: UICollectionViewFlowLayout = {
let layout = UICollectionViewFlowLayout()
layout.minimumInteritemSpacing = 0
@@ -60,6 +62,13 @@ class VoiceRecordSampleView: UIView {
NSLayoutConstraint.activateConstraints(sampleCollectionViewConstraintH)
NSLayoutConstraint.activateConstraints(sampleCollectionViewConstraintV)
}
func appendSampleValue(value: CGFloat) {
sampleValues.append(value)
let indexPath = NSIndexPath(forItem: sampleValues.count - 1, inSection: 0)
sampleCollectionView.insertItemsAtIndexPaths([indexPath])
}
}
extension VoiceRecordSampleView: UICollectionViewDataSource {
@@ -69,7 +78,7 @@ extension VoiceRecordSampleView: UICollectionViewDataSource {
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 300
return sampleValues.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {