音频无法正常播放

时间:2018-06-20 09:22:34

标签: ios swift avfoundation avaudioplayer

var sound1 = NSURL(fileURLWithPath: Bundle.main.path(forResource: "lion", ofType: "mp3")!)
var sound2 = NSURL(fileURLWithPath: Bundle.main.path(forResource: "tiger", ofType: "mp3")!)
var sound3 = NSURL(fileURLWithPath: Bundle.main.path(forResource: "fox", ofType: "mp3")!)
var sound4 = NSURL(fileURLWithPath: Bundle.main.path(forResource: "zebra", ofType: "mp3")!)
var sound5 = NSURL(fileURLWithPath: Bundle.main.path(forResource: "koala", ofType: "mp3")!)

var imageview = UIImageView()
var audioPlayer = AVAudioPlayer()

var sound : [NSURL] = [sound1,sound2,sound3,sound4,sound5]
    let height = self.navigationController?.navigationBar.frame.height
    horizontalscroll.frame = view.frame
    for i in 0 ..< receiveimg.count
    {

        imageview = UIImageView()
        imageview.image = receiveimg[i]
        imageview.contentMode = .scaleAspectFit

        let xvalue = self.view.frame.width * CGFloat(i)
        imageview.frame = CGRect(x: xvalue, y: 0, width: self.view.frame.width, height: self.view.frame.height - height!)


        horizontalscroll.contentSize.width = horizontalscroll.frame.width * CGFloat(i+1)
        horizontalscroll.addSubview(imageview)

        do{
            audioPlayer = try AVAudioPlayer(contentsOf: sound[i] as! URL)
            audioPlayer.play()
        }

        catch
        {
            print(error)
        }

    }
    horizontalscroll.isPagingEnabled = true
我有一个滚动视图,其中有一个uiimage视图,滚动时它显示5条不同的消息,滚动时它还应该播放5种不同的音频,但是在第一个图像上只播放一个音频,而最后一个音频也在播放。任何帮助表示赞赏。

0 个答案:

没有答案