-6
私は現在、iOS用のオーディオストリーミングアプリケーションを開発中です。私は現在、JSONシリアライゼーションを使用してデータベースから曲を引き出し、個々の曲ごとにボタンを作成して再生できますが、一時停止する方法はわかりません。ここで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配列を一時停止」とは何の関係もありませんこれは、いくつかのメディアを一時停止して行うことです – Fogmeister
Googleの「一時停止オーディオスウィフトIOS」。 –