2
在暂停后的AVAudioPlayer中,歌曲不会继续,在Swift上重新开始播放。问题是当我选择暂停按钮,然后再次播放按钮时,它应该从开始。在暂停后的AVAudioPlayer中,歌曲不会在Swift上继续播放
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
}
代码上播放按钮需要我只播放声音当推玩按钮 – Zhanserik
FUNC intiAudioPlayer()将只初始化AudioPlayer也不会播放声音,直到你点击按钮。确保在点击播放按钮之前初始化您的AudioPlayer。否则它不会播放声音。 –
你的方法不起作用 – Zhanserik