How to Make Karaoke App With Minimal Coding Experience – Part 2

0
Shares
How to Make Karaoke App With Minimal Coding Experience – Part 2
4 (80%) 1 vote

karaoke app

In the first part of Karaoke app tutorial series, we covered how to use AVFoundation framework and add songs into karaoke app.

In this part, we’ll be adding feature of recording vocals along with the music.

Let’s Get Started

Set IBOutlet of Record button and add IBAction for the same.

@IBOutlet weak var btnRecord: UIButton!

//MARK: Start Recording

    @IBAction func actionStartRecord(sender: AnyObject) {

    }

Create object of AVAudioRecorder and set an object for Settings of recorder.

var audioRecorder:AVAudioRecorder!

    

    //Setting for recorder

    let recordSettings = [AVEncoderAudioQualityKey: AVAudioQuality.Min.rawValue,

                          AVEncoderBitRateKey: 16,

                          AVNumberOfChannelsKey : 2,

                          AVSampleRateKey: 44100.0]

Now initialize AVAudioRecorder and set destination path for output sound.

override func viewDidLoad() {

        super.viewDidLoad()

        // Do any additional setup after loading the view, typically from a nib.

        UIApplication.sharedApplication().statusBarHidden=true;

        btnStop.enabled = false

        

        self.prepareRecorder()

        self.initilizePlayer()

    }

func prepareRecorder() {

        // getting URL path for audio

        let dirPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)

        let docDir = dirPath[0]

        let soundFilePath = (docDir as NSString).stringByAppendingPathComponent("sound.caf")

        let soundFileURL = NSURL(fileURLWithPath: soundFilePath)

        print(soundFilePath)

        

        var error : NSError?

        let audioSession = AVAudioSession.sharedInstance()

        do {

            try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord)

        } catch let error1 as NSError {

            error = error1

        }

        if let err = error{

            print("audioSession error: \(err.localizedDescription)")

        }

        do {

            audioRecorder = try AVAudioRecorder(URL: soundFileURL, settings: recordSettings as! [String : AnyObject])

        } catch let error1 as NSError {

            error = error1

            audioRecorder = nil

        }

        

        if let err = error{

            print("audioSession error: \(err.localizedDescription)")

        }else{

            audioRecorder?.prepareToRecord()

        }

    }

Give definition to “actionStartRecord” method.

//MARK: Start Recording

    @IBAction func actionStartRecord(sender: AnyObject) {

        if !audioRecorder.recording {

            btnPlay.enabled = false

            audioPlayer.play()

            audioRecorder?.record()

            btnRecord.setTitle("Stop", forState: .Normal)

        } else {

            //Stop audio

            btnPlay.enabled = true

            self.initilizePlayer()

            audioRecorder?.stop()

            btnRecord.setTitle("Record", forState: .Normal)

        }

    }

And done!

how to make karaoke app

In this karaoke app tutorial series, we’ve covered how bundle songs in an iOS app and record vocals along with the music.

However, there are many possibilities in karaoke apps such as adding music tracks dynamically, or download music files online within the app. And if you have such iOS app idea for startup, hire iPhone app developer, UI/UX designer, and consult with a iOS app development company to make the next best karaoke app.

Grab free copy of karaoke app example demo from Github.

 
0
Shares
 

Want to Develop Money Making App? Contact Us Now

Get your free consultation now