0 Replies - 783 Views - Last Post: 13 December 2016 - 09:27 PM

#1 11ash1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 25-October 15

music is not working on Xcode

Posted 13 December 2016 - 09:27 PM

I am currently creating a program that has multiple tabs. Everything else works but I am unable to get the music to work. When I select the song it does not show me the play, stop, and pause buttons or play the song for me. I am not sure what is wrong since I am not getting any error messages or anything. If someone can help me I would really appreciate it. Actually update progress is giving me an error saying use of string literal for objective-c is deprecated but not sure if this would cause it.
timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "updateprogress", userInfo: nil, repeats: true)
Posted Image

import UIKit
import AVFoundation

class AartisViewController: UIViewController,UITableViewDataSource, UITableViewDelegate, AVAudioSessionDelegate {



    @IBOutlet weak var img1: UIImageView!
    @IBOutlet weak var tableview: UITableView!


    @IBOutlet weak var lblAarti: UILabel!

    let arraartis = ["Kakad_Aarti","Madhyan_Aarti","Dhoop_Aarti","Shej_Aarti"]

    var soundno:Int = 0

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.


        tableview.delegate = self
        tableview.dataSource = self


    }


    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return arraartis.count
    }


    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = tableview.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! AartiTableViewCell

        let row = indexPath.row

       // cell.textLabel?.text = arraartis[row]
        cell.lblaarti.text =   arraartis[row]

        return cell
    }


    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {


        print("select")

        let cell = tableview.cellForRowAtIndexPath(indexPath)



        if  cell?.accessoryType ==  .Checkmark
        {

            cell?.accessoryType = .None

            stopsound()
        }
        else
        {
            cell?.accessoryType = .Checkmark
            soundno = indexPath.row
            playsound(arraartis[soundno])

            btnpause.enabled = true
            btnstop.enabled = true
        }


    }

    func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {

        print("de select")

        let cell = tableview.cellForRowAtIndexPath(indexPath)

        cell?.accessoryType = .None

        stopsound()
    }





    // MARK: sound releated


    var player :AVAudioPlayer!

    func playsound(nameofsound:String){
        let audioFilepath = NSBundle.mainBundle().pathForResource(nameofsound, ofType: "mp3")

        if audioFilepath != nil {

            let audioFileUrl = NSURL.fileURLWithPath(audioFilepath!)

            try! player = AVAudioPlayer(contentsOfURL: audioFileUrl)

            player.play()

            lblduration.text = getTimeAsString( player.duration)

            lblAarti.text = nameofsound

            starttimer()


        }
        else
        {
            print ("audio file not found")
        }

    }

    func stopsound(){

        player.stop()
    }

    // start,stop, duration related


    var timer = NSTimer()


    @IBAction func btnplay(sender: UIButton) {
        if !player.playing
        {
            player.play()
            btnpause.enabled = true
            btnstop.enabled = true
            btnplay.enabled = false
        }

    }

    @IBOutlet weak var btnplay: UIButton!


    @IBOutlet weak var btnpause: UIButton!


    @IBOutlet weak var btnstop: UIButton!

    @IBAction func btnpause(sender: AnyObject) {
        player.pause()
        btnplay.enabled = true
    }


    @IBAction func btnstop(sender: UIButton) {

        player.stop()
        player.currentTime = 0
        btnplay.enabled = true

    }



    @IBOutlet weak var lblduration: UILabel!


    @IBOutlet weak var lblcurrent: UILabel!


    func getTimeAsString(timeinterval:NSTimeInterval) -> String {
        var seconds = 0
        var minutes = 0


        //if let time = timeinterval {
            seconds = Int(timeinterval) % 60
            minutes = (Int(timeinterval) / 60) % 60
       // }
        return String(format: "%0.2d:%0.2d",minutes,seconds)
    }


    func starttimer(){
        timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "updateprogress", userInfo: nil, repeats: true)

    }

    func stoptimer(){
        timer.invalidate()

    }


    @IBOutlet weak var progresbar: UIProgressView!

    func updateprogress(){

        progresbar.progress =  Float( player.currentTime / player.duration)

        lblcurrent.text = getTimeAsString(player.currentTime)

    }

}


Is This A Good Question/Topic? 0
  • +

Page 1 of 1