Zhan Zhan - 1 year ago 68
Swift Question

In AVPlayer after a pause the song does not continue on Swift

In AVPlayer 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) {


@IBAction func playButton(sender: AnyObject) {

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

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


@IBOutlet weak var stopButtonOutlet: UIButton!

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

func playSongAtIndex(index: Int) {

songIndex = index


Answer Source

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

@IBAction func playPauseButton(sender: AnyObject) {

    if (player.playing == true) {
        playPauseButtonOutlet.setImage(UIImage(named: "play.jpg"), forState: UIControlState.Normal)
    } else {
        playPauseButtonOutlet.setImage(UIImage(named: "pause.jpg"), forState: UIControlState.Normal)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download