CardinalNow / iOS-CircleProgressView

CircleProgressView
MIT License
518 stars 96 forks source link

Play Audio #6

Closed Phloxx1 closed 9 years ago

Phloxx1 commented 9 years ago

everytime I click to play an audio file, it crashes. how would I play the audio while at the same time showing the progress of it? screen shot 2015-02-14 at 4 05 03 pm

xrolfex commented 9 years ago

Why are you using the lay the audio? Is it avfoundation?

xrolfex commented 9 years ago

Using avfoundation i was not able to reproduce the problem. You can take a look at the example I created quickly.


//
//  ViewController.swift
//  CircleProgressView
//
//  Created by Eric Rolf on 8/11/14.
//  Copyright (c) 2014 Eric Rolf, Cardinal Solutions Group. All rights reserved.
//

import UIKit
import AVFoundation

class ViewController: UIViewController, AVAudioPlayerDelegate {

    @IBOutlet weak var circleProgressView: CircleProgressView!
    @IBOutlet weak var progressLabel: UILabel!
    @IBOutlet weak var progressSlider: UISlider!
    @IBOutlet weak var clockwiseSwitch: UISwitch!

    var player:AVAudioPlayer?
    var timer:NSTimer!

    let nf = NSNumberFormatter()

    override func viewDidLoad() {
        super.viewDidLoad()

        nf.numberStyle = NSNumberFormatterStyle.DecimalStyle
        nf.maximumFractionDigits = 2

        self.clockwiseSwitch.setOn(self.circleProgressView.clockwise, animated: false)
        self.progressSlider.value = Float(self.circleProgressView.progress)
        self.progressLabel.text = "Progress: " + nf.stringFromNumber(NSNumber(double: self.circleProgressView.progress))!

        let audioFilePath = NSBundle.mainBundle().pathForResource("14 Wonderland", ofType: "mp3")!
        let pathAsURL = NSURL(fileURLWithPath: audioFilePath)

        var error:NSError?

        player = AVAudioPlayer(contentsOfURL: pathAsURL, error: &error)

        // Check out what's wrong in case that the player doesn't init.
        if (error != nil) {
            println("\(error?.localizedDescription)");
        }
        else{
            player?.prepareToPlay()
        }

        player?.delegate = self

        timer = NSTimer.scheduledTimerWithTimeInterval(1.0/60.0, target: self, selector: "update", userInfo: nil, repeats: true)
    }

    func update() {
        if (player != nil) {

            let progress = player!.currentTime / player!.duration

            self.circleProgressView.progress = progress
            self.progressSlider.setValue(Float(progress), animated: true)
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    // MARK: - IBActions

    @IBAction func sliderDidChangeValue(sender: AnyObject) {
        let slider:UISlider = sender as UISlider
        self.circleProgressView.progress = Double(slider.value)
        self.progressLabel.text = "Progress: " + nf.stringFromNumber(NSNumber(double: self.circleProgressView.progress))!
    }

    @IBAction func switchDidChangeValue(sender: AnyObject) {
        let mySwitch:UISwitch = sender as UISwitch
        self.circleProgressView.clockwise = mySwitch.on
        self.circleProgressView.progress = self.circleProgressView.progress
    }

    // MARK: - Helpers

    func delay(delay:Double, closure:()->()) {
        dispatch_after(
            dispatch_time(
                DISPATCH_TIME_NOW,
                Int64(delay * Double(NSEC_PER_SEC))
            ),
            dispatch_get_main_queue(), closure)
    }

    @IBAction func playButtonTouched(sender: AnyObject) {
        player?.play()
    }

}