4

In AVAudioPlayer after a pause the song does not continue, start again on Swift. Problem is when I choose Pause button and again Play button then It should start from starting.

import UIKit
import AVFoundation
class DetailViewController: UIViewController {

let musicArray:[String] = ["reamon", "sandman"]

var audioPlayer: AVAudioPlayer?

var songIndex:Int = 0

@IBAction func pauseButton(sender: AnyObject) {
    audioPlayer!.pause()

}

@IBAction func playButton(sender: AnyObject) {

    let mySound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource(musicArray[songIndex], ofType: "mp3")!)

    do{
        audioPlayer = try AVAudioPlayer(contentsOfURL: mySound)
        audioPlayer!.prepareToPlay()
        audioPlayer!.play()
    } catch {
        print("Error getting the audio file")
    }

}

@IBOutlet weak var stopButtonOutlet: UIButton!

@IBAction func stopButton(sender: AnyObject) {
    audioPlayer!.stop()
    audioPlayer!.currentTime = 0
}

func playSongAtIndex(index: Int) {

    songIndex = index

}
Edward
  • 2,864
  • 2
  • 29
  • 39
Janserik
  • 2,306
  • 1
  • 24
  • 43

2 Answers2

5

you are initialising AVAudioPlayer every time on your play button action try to initialise your AVAudioPlayer in some other method which will called only once try this

override func viewWillAppear(animated: Bool) 
{
        self .intiAudioPlayer()
}

func intiAudioPlayer() 
{

      let mySound = NSURL(fileURLWithPath:NSBundle.mainBundle().pathForResource(musicArray[songIndex], ofType: "mp3")!)

    do
   {
        audioPlayer = try AVAudioPlayer(contentsOfURL: mySound)
        audioPlayer!.prepareToPlay()
    } 
    catch {
        print("Error getting the audio file")
    }
}

your play button action will contain only this much of code

 @IBAction func playButton(sender: AnyObject) 
{
   audioPlayer!.play() 
}
  • Code on play button need me to play sound only when push on play button – Janserik Mar 08 '16 at 10:52
  • func intiAudioPlayer() will just initialise the AudioPlayer it will not play the sound until you click on the push button. Make sure that you initialise your AudioPlayer before you click on the Play button. Otherwise it will not play the sound. – Darshit Vadodaria Mar 08 '16 at 11:03
  • @Zhan have you called the func intiAudioPlayer() before the play button? – Darshit Vadodaria Mar 09 '16 at 08:36
  • @Zhan Please add viewWillAppear in the code in that you can call the func intiAudioPlayer() so AudioPlayer will get initialise here is a code for that override func viewWillAppear(animated: Bool) { self .intiAudioPlayer() } – Darshit Vadodaria Mar 09 '16 at 08:53
2

I do another way. It is one button to PLAY and PAUSE.

@IBAction func playPauseButton(sender: AnyObject) {

    if (player.playing == true) {
        player.stop()
        playPauseButtonOutlet.setImage(UIImage(named: "play.jpg"), forState: UIControlState.Normal)
    } else {
        player.play()
        playPauseButtonOutlet.setImage(UIImage(named: "pause.jpg"), forState: UIControlState.Normal)
    }
}
Janserik
  • 2,306
  • 1
  • 24
  • 43