我无法在录制音频时找到文件的大小

loading...


1

这是我的整个视图控制器与一些基本的录音和播放的东西,它记录和播放没有问题,但我想在控制台中打印文件大小,不知怎的,它给了我一个错误$ Failed to get file attributes for local path: file:///Users/ashutoshmane/Library/Developer/CoreSimulator/Devices/DF46178B-BD38-45A9-87D8-4D4F378EEE8B/data/Containers/Data/Application/B949B5FF-9ACB-485F-BAE5-39F2D32664A0/Documents/sound.caf with error: Error Domain=NSCocoaErrorDomain Code=260 "The file “sound.caf” couldn’t be opened because there is no such file."

import UIKit

导入AudioKit导入AVFoundation

class MyViewController:UIViewController,AVAudioPlayerDelegate,AVAudioRecorderDelegate {//音频播放器和rec vars

var audioPlayer: AVAudioPlayer?
var audioRecorder: AVAudioRecorder?
var fileSize:UInt64 = 0

//record settings dictionary
var recordSettings =
    [AVEncoderAudioQualityKey: AVAudioQuality.min.rawValue,
     AVEncoderBitRateKey : 128,
      AVNumberOfChannelsKey:1 ,
        AVSampleRateKey: 22050 ] as [String : Any]



//creating file and checking size
func createFile() -> URL {
    let dirPaths = FileManager.default.urls(for: .documentDirectory,
                                            in: .userDomainMask)
    let soundFileURL = dirPaths[0].appendingPathComponent("sound.caf")
    print(soundFileURL)
    return soundFileURL
}

func KnowFileSize(filesUrl:URL){

    let StringURL=String(describing:filesUrl)
    do {
        let fileAttribute=try FileManager.default.attributesOfItem(atPath:StringURL )
        fileSize=fileAttribute[FileAttributeKey.size] as! UInt64
        print( "file size is:", fileSize)
    }
    catch {
        print("Failed to get file attributes for local path: \(StringURL) with error: \(error)")
    }



}




@IBOutlet weak var recordButton: UIButton!

@IBOutlet weak var playButton: UIButton!

@IBOutlet weak var stopButton: UIButton!

//record audio
@IBAction func recordAudio(_ sender: Any) {
    if audioRecorder?.isRecording == false {
        playButton.isEnabled = false
        stopButton.isEnabled = true
        audioRecorder?.record()
    }
}

//stop button
@IBAction func stopAudio(_ sender: Any) {
    stopButton.isEnabled = false
    playButton.isEnabled = true
    recordButton.isEnabled = true

    if audioRecorder?.isRecording == true {
        audioRecorder?.stop()
    } else {
        audioPlayer?.stop()
    }

}


//play button
@IBAction func playAudio(_ sender: Any) {

    if audioRecorder?.isRecording == false {
        stopButton.isEnabled = true
        recordButton.isEnabled = false

        do {
            try audioPlayer = AVAudioPlayer(contentsOf:
                (audioRecorder?.url)!)
            audioPlayer!.delegate = self
            audioPlayer!.prepareToPlay()
            audioPlayer!.play()
        } catch let error as NSError {
            print("audioPlayer error: \(error.localizedDescription)")
        }
    }
}

// view did load // override func viewDidLoad(){

super.viewDidLoad()
    print("in view didload")

    playButton.isEnabled = false
    stopButton.isEnabled = false

    //creating file in VDL

    let soundFile=createFile()

    print(AVSampleRateKey)
    let audioSession = AVAudioSession.sharedInstance()

    do {
        try audioSession.setCategory(
            AVAudioSessionCategoryPlayAndRecord)
    } catch let error as NSError {
        print("audioSession error: \(error.localizedDescription)")
    }

    do {

        try audioRecorder = AVAudioRecorder(url: soundFile,
                                            settings: recordSettings as [String : AnyObject])
        audioRecorder?.prepareToRecord()
        print(recordSettings)

    } catch let error as NSError {
        print("audioSession error: \(error.localizedDescription)")
    }

    //ending audio recorder task and checking file size
    audioRecorderDidFinishRecording(audioRecorder!, successfully: true)
    print(soundFile)
    KnowFileSize(filesUrl:soundFile)



}





//delegate functions


func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
    recordButton.isEnabled = true
    stopButton.isEnabled = false
}

func audioPlayerDecodeErrorDidOccur(_ player: AVAudioPlayer, error: Error?) {
    print("Audio Play Decode Error")
}

func audioRecorderDidFinishRecording(_ recorder: AVAudioRecorder, successfully flag: Bool) {

}

func audioRecorderEncodeErrorDidOccur(_ recorder: AVAudioRecorder, error: Error?) {
    print("Audio Record Encode Error")
}

}

0回答

No Data

loading...

评论

暂时没有评论!