gpt4 book ai didi

ios - 快速制作播放列表(开始下一首歌曲)

转载 作者:搜寻专家 更新时间:2023-10-31 21:58:22 24 4
gpt4 key购买 nike

我使用 AVFoundation 在 swift 中创建了一个声音播放器。播放歌曲结束后,我正在尝试开始播放下一首歌曲。我试图实现这段代码

if (audioPlayer.currentTime >= audioPlayer.duration){
var recentSong = songPlaylist[selectedSongNumber + 1]
audioPlayer = AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath:
NSBundle.mainBundle().pathForResource(recentSong, ofType: "mp3")!), error: nil)
audioPlayer.play()
}

但我无法实现这段代码(我不知道在哪里实现它)。这是我的完整代码

import UIKit
import AVFoundation
import AVKit

public var audioPlayer = AVPlayer()
public var selectedSongNumber = Int()
public var songPlaylist:[String] = ["song1", "song2"]
public var recentSong = "song1"
let playImage = UIImage(named: "Play.png") as UIImage!
let pauseImage = UIImage(named: "Pause.png") as UIImage!

class FirstViewController: UIViewController {

@IBOutlet weak var musicSlider: UISlider!

@IBOutlet weak var PlayPause: UIButton!

var audioPlayer = AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath:
NSBundle.mainBundle().pathForResource(recentSong, ofType: "mp3")!), error: nil)

override func viewDidLoad() {
super.viewDidLoad()

musicSlider.maximumValue = Float(audioPlayer.duration)

var timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: Selector("updateMusicSlider"), userInfo: nil, repeats: true)


if (audioPlayer.currentTime >= audioPlayer.duration){

var recentSong = songPlaylist[selectedSongNumber + 1]

audioPlayer = AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath:
NSBundle.mainBundle().pathForResource(recentSong, ofType: "mp3")!), error: nil)

audioPlayer.play()

}
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()

}
@IBAction func PlayPauseButton(sender: AnyObject) {

if (audioPlayer.playing == false){
audioPlayer.play()
PlayPause.setImage(pauseImage, forState: .Normal)
}else{
audioPlayer.pause()
PlayPause.setImage(playImage, forState: .Normal)
}
}

@IBAction func StopButton(sender: AnyObject) {
audioPlayer.stop()
audioPlayer.currentTime = 0
PlayPause.setImage(playImage, forState: .Normal)
}

@IBAction func musicSliderAction(sender: UISlider) {
audioPlayer.stop()
audioPlayer.currentTime = NSTimeInterval(musicSlider.value)
audioPlayer.play()

}

func updateMusicSlider(){

musicSlider.value = Float(audioPlayer.currentTime)

}

}

最佳答案

我正在用不同的东西更新我的代码:

import UIKit
import AVFoundation

class ViewController: UIViewController, AVAudioPlayerDelegate {

var counter = 0
var song = ["1","2","3"]
var player = AVAudioPlayer()

@IBOutlet weak var musicSlider: UISlider!

override func viewDidLoad() {
super.viewDidLoad()
musicSlider.value = 0.0
}

func updateMusicSlider(){

musicSlider.value = Float(player.currentTime)
}

@IBAction func playSong(sender: AnyObject) {

music()
}
@IBAction func sliderAction(sender: AnyObject) {

player.stop()
player.currentTime = NSTimeInterval(musicSlider.value)
player.play()
}

func music(){

var audioPath = NSBundle.mainBundle().pathForResource("\(song[counter])", ofType: "mp3")!
var error : NSError? = nil
player = AVAudioPlayer(contentsOfURL: NSURL(string: audioPath), error: &error)
musicSlider.maximumValue = Float(player.duration)
var timer = NSTimer.scheduledTimerWithTimeInterval(0.05, target: self, selector: Selector("updateMusicSlider"), userInfo: nil, repeats: true)
player.delegate = self
if error == nil {
player.delegate = self
player.prepareToPlay()
player.play()
}
}

func audioPlayerDidFinishPlaying(player: AVAudioPlayer!, successfully flag: Bool)
{
println("Called")
if flag {
counter++
}

if ((counter + 1) == song.count) {
counter = 0
}

music()
}

}

你可以这样做。

希望它会有所帮助并且HERE是更多信息的示例项目。

关于ios - 快速制作播放列表(开始下一首歌曲),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30214187/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com