【问题标题】:Pausing a JSON array暂停 JSON 数组
【发布时间】:2017-03-03 16:39:57
【问题描述】:

我目前正在开发适用于 iOS 的音频流应用程序,而且我是 swift 新手。我目前能够使用 JSON 序列化从数据库中提取歌曲,然后为每首歌曲创建按钮并播放它们,但我不知道如何暂停它们?

这是我的代码:

import UIKit
import AVKit
import AVFoundation


class ViewController: UIViewController {

    var songArray: [Array<String>] = []  //array to contain song names and filepaths

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        //connect to website
        let url = URL(string:"http://cgi.soic.indiana.edu/~team22/SongData.php")
        let task = URLSession.shared.dataTask(with: url!) { (data, response, error) in
            if error != nil
            {
                print("error")
            }
            else
            {
                if let content = data
                {
                    do
                    {
                        //download JSON data from php page, display data
                        let JSON = try JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers) as! [[String]]
                        print(JSON)

                        //Make buttons with JSON array
                        var buttonY:  CGFloat = 20
                        for song in JSON {
                            //add information to array
                            self.songArray.append(song)
                            let SongButton = UIButton(frame: CGRect(x: 50, y: buttonY, width: 250, height: 30))
                            buttonY = buttonY + 50 // 50px spacing

                            SongButton.layer.cornerRadius = 10  //Edge formatting for buttons

                            SongButton.backgroundColor = UIColor.darkGray //Color for buttons

                            SongButton.setTitle("\(song[0])", for: UIControlState.normal) //button title

                            SongButton.titleLabel?.text = "\(song[0])" // set title label

                            SongButton.addTarget(self,action: #selector(self.songButtonPressed(_:)), for: UIControlEvents.touchUpInside)  //button press / response

                            self.view.addSubview(SongButton)  // adds buttons to view
                        }
                    }
                    catch
                    {

                    }
                }
            }
        }
        task.resume()
        print(songArray)

    }


    func songButtonPressed(_ sender:UIButton!) { // Streaming function for buttons when pressed
        for song in songArray {
            if  "\(song[0])" == sender.titleLabel?.text { //compare loop element to name of button pressed



                let URL = NSURL(string: song[1])  //plug loop element into audio player
                let player = AVPlayer(url: URL! as URL)
                let playerLayer = AVPlayerLayer(player: player)
                playerLayer.frame = self.view.bounds
                self.view.layer.addSublayer(playerLayer)
                player.play()
            }
        }
    }
}

【问题讨论】:

  • 这与“暂停 JSON 数组”无关。这与暂停某些媒体有关。
  • 谷歌“暂停音频 swift ios”

标签: arrays json swift


【解决方案1】:

转到 Apple 文档并查找 AVPlayer。有一个.pause() 函数。就那么简单。您的另一个问题是您无法在songButtonPressed 范围之外访问该播放器,因此您需要在视图控制器中创建一个变量(即在您的var songArray... 下方,以便当有人按下暂停按钮时你可以说self.player.pause()。

文档链接:https://developer.apple.com/reference/avfoundation/avplayer

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-19
    • 1970-01-01
    • 1970-01-01
    • 2015-12-10
    • 1970-01-01
    • 1970-01-01
    • 2011-09-26
    • 1970-01-01
    相关资源
    最近更新 更多