File saved in AVAudioEngine.Unable to play the first time

Asked 2 years ago, Updated 2 years ago, 38 views

I tried using AVAudioEngin to create a program that saves and plays audio to a file, but I couldn't play the first time.From the second time, you can now record and play.
I searched the web to see why, but I don't know why.
I don't understand the basics of both framework and swift.I'm sorry to ask you a question in such a situation, but please give me advice on how to play it from the first round.

import UIKit
import AVFoundation

classViewController:UIViewController, AVAudioPlayerDelegate{
varrecBtn —UIButton!
varplayBtn —UIButton!
varfile —AVAudioFile?
varplayer=AVAudioPlayerNode()
var path —String!
varengine —AVAudioEngine!
varmixer —AVAudioMixerNode!
var input —AVAudioInputNode!
varaudioFile —AVAudioFile!

varaudioRecorder:AVAudioRecorder!
varaudioPlayer —AVAudioPlayer!
var filePath —NSURL! 
let settings = [
    AVFormatIDKey: kAudioFormatLinearPCM,
    AVSampleRateKey:44100.0,
    AVNumberOfChannelsKey:1]  

override func viewDidLoad(){
    super.viewDidLoad()

    recBtn = UIButton (frame: CGRectMake(0,0,100,40))
    recBtn.setTitle("RECORD", forState:UIControlState.Normal)
    recBtn.setTitleColor (UIColor.blackColor(), forState:.Normal)
    recBtn.center= CGPointMake (self.view.bounds.width/2,200)
    recBtn.backgroundColor=UIColor.lightGrayColor()
    self.view.addSubview(recBtn)

    playBtn = UIButton (frame: CGRectMake(0,0,100,40))
    playBtn.setTitle("PLAY", forState:UIControlState.Normal)
    playBtn.setTitleColor (UIColor.blackColor(), forState: .Normal)
    playBtn.center= CGPointMake (self.view.bounds.width/2,300)
    playBtn.backgroundColor=UIColor.lightGrayColor()
    self.view.addSubview(playBtn)

    recBtn.addTarget(self, action: "recbtn", forControlEvents:UICControlEvents.TouchDown)
    recBtn.addTarget(self, action: "recbtnRelease", forControlEvents:UICControlEvents.TouchUpInside)
    playBtn.addTarget(self, action: "pplay", forControlEvents: UICControlEvents.TouchDown)
    playBtn.addTarget(self, action: "play", forControlEvents:UIControlEvents.TouchUpInside)

    initEngine()
}

func initEngine(){
    filePath=URLFor("testrecord.wav")
    engine=AVAudioEngine()
    input = engine.inputNode
    mixer=engine.mainMixerNode
    mixer.outputVolume=1.0
}

func URLFor (filename: String) - > NSURL?{
    iflet dirs: [String] = NSSearchPathForDirectoriesInDomains (NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.AllDomainsMask, true) as? [String] {
        let dir=dirs[0]
        path=dir.stringByAppendingPathComponent(filename)

        println(path)
        return NSURL (fileURLWithPath:path)
    }
    return nil
}

funcrecbtn(){
    recBtn.backgroundColor=UIColor.redColor()
    variformat=engine.inputNode.inputFormatForBus(0)
    engine.connect (input, to:mixer, format:iformat)
    engine.startAndReturnError(nil)

    audioFile=AVAudioFile(for Writing:filePath, settings:settings as [NSObject:AnyObject], error:nil)

    let Input = engine.inputNode
    Input.installTapOnBus(0, bufferSize:4096, format:audioFile.processingFormat){
        (buffer:AVAudioPCMBuffer!, when:AVAudioTime!) in
        self.audioFile.writeFromBuffer(buffer, error:nil)
    }
}

funcrecbtnRelease(){
    recBtn.backgroundColor=UIColor.lightGrayColor()
    variable error:NSError?
    println("recbuttonReleaqse")
    engine.stop()
    engine.inputNode.removeTapOnBus(0)

    iflet attr:NSDictionary=NSFileManager.defaultManager().attributesOfItemAtPath(path!, error:&error){
        println(attr.fileModificationDate())
        println(attr.fileSize())
    } else {
        println ("DAME")
    }
}

funcpplay(){
    playBtn.backgroundColor=UIColor.redColor()
}

funcplay(){
    playBtn.backgroundColor=UIColor.lightGrayColor()

    var attr —NSDictionary
    variable error:NSError?

    iflet attr:NSDictionary=NSFileManager.defaultManager().attributesOfItemAtPath(path!, error:&error){
        println(attr.fileModificationDate())
        println(attr.fileSize())
    } else {
        println("error\(error!.localizedDescription)")
    }
    audioPlayer=AVAudioPlayer(contentsOfURL:filePath, error:nil)
    audioPlayer.delegate=self
    audioPlayer.prepareToPlay()
    audioPlayer.play()
}
}

ios swift

2022-09-30 20:54

1 Answers

engine.inputNode.removeTapOnBus(0)

followed by
self.audioFile=nil
Why don't you add ?


2022-09-30 20:54

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.