所以我的意图在一定程度上起作用。就像我的意思是siri repsonse表示“完成”或“确定”。当我问Siri以下命令时
列表项
:“嘿siri Play DRN1”
Siri响应已完成。 (何时应该现在打开)
但是该应用程序什么也不做。
现在我注意到我尚未设置以下屏幕,或者错过了一步?
AppDelegate.swift
//
// AppDelegate.swift
// DRN1
//
// Created by Russell Harrower on 24/11/19.
// Copyright © 2019 Russell Harrower. All rights reserved.
//
import UIKit
@UIApplicationmain
class AppDelegate: UIResponder,UIApplicationDelegate {
func application(_ application: UIApplication,continue useractivity: NSUseractivity,restorationHandler: @escaping ([UIUseractivityRestoring]?) -> Void) -> Bool {
if useractivity.activityType == "PlayDRN1Intent" {
print("YES I RAN")
}
return true
}
func application(_ application: UIApplication,didFinishlaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
return true
}
// MARK: UIScenesession Lifecycle
func application(_ application: UIApplication,configurationForConnecting connectingScenesession: UIScenesession,options: UIScene.ConnectionOptions) -> UISceneConfiguration {
// Called when a new scene session is being created.
// Use this method to select a configuration to create the new scene with.
return UISceneConfiguration(name: "Default Configuration",sessionRole: connectingScenesession.role)
}
func application(_ application: UIApplication,didDiscardScenesessions scenesessions: Set<UIScenesession>) {
// Called when the user discards a scene session.
// If any sessions were discarded while the application was not running,this will be called shortly after application:didFinishlaunchingWithOptions.
// Use this method to release any resources that were specific to the discarded scenes,as they will not return.
}
}
FirstViewController.swift
//
// FirstViewController.swift
// DRN1
//
// Created by Russell Harrower on 24/11/19.
// Copyright © 2019 Russell Harrower. All rights reserved.
//
import UIKit
import Intents
import AVKit
import Kingfisher
extension UIImageView {
func setImage(with urlString: String){
guard let url = URL.init(string: urlString) else {
return
}
let resource = ImageResource(downloadURL: url,cacheKey: urlString)
var kf = self.kf
kf.indicatorType = .activity
self.kf.setImage(with: resource)
}
}
struct Nowplayng: Decodable{
let data: [Data]
}
struct Data: Decodable{
let track: Trackinfo
}
struct Trackinfo: Decodable {
let title: String
let artist: String
let imageurl: String
}
class FirstViewController: UIViewController {
var timer = Timer()
var rplayed = false;
var playButton = true;
@IBOutlet weak var artist: UILabel!
@IBOutlet weak var song: UILabel!
@IBOutlet weak var imageurl: UIImageView!
override func viewDidLoad() {
let intent = PlayDRN1Intent()
intent.playdrn1 = "DRN1"
let interaction = INInteraction(intent: intent,response: nil)
interaction.donate { (error) in
print(error ?? "error")
}
MusicPlayer.shared.startBackgroundMusic()
// Do any additional setup after loading the view.
self.artist.textAlignment = .center
self.song.textAlignment = .center
scheduledTimerWithTimeInterval()
self.nowplaying()
}
func scheduledTimerWithTimeInterval(){
// Scheduling timer to Call the function "updateCounting" with the interval of 1 seconds
timer = Timer.scheduledTimer(timeInterval: 30,target: self,selector: #selector(self.nowplaying),userInfo: nil,repeats: true)
}
@objc func nowplaying(){
let jsonURLString = "https://api.drn1.com.au/station/playing"
guard let feedurl = URL(string: jsonURLString) else { return }
URLSession.shared.dataTask(with: feedurl) { (data,response,err)
in
guard let data = data else { return }
do{
let nowplaying = try JSONDecoder().decode(Nowplayng.self,from: data)
nowplaying.data.forEach {
DispatchQueue.main.async {
self.artist.text = nowplaying.data.first?.track.artist
self.song.text = nowplaying.data.first?.track.title
}
print($0.track.title)
if var strUrl = nowplaying.data.first?.track.imageurl {
strUrl = strUrl.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
self.imageurl.kf.setImage(with: URL(string: strUrl),placeholder: nil)
//MusicPlayer.shared.nowplaying(artist: $0.track.artist,song: $0.track.title,cover:strUrl)
MusicPlayer.shared.getartBoard(artist: $0.track.artist,cover:strUrl)
}
//self.imageurl.setImage(with: $0.track.imageurl)
}
}catch let jsonErr{
print("error json ",jsonErr)
}
}.resume()
}
override func viewDidAppear(_ animated: Bool) {
self.tabBarController?.navigationItem.title = "Now Playing"
}
@IBaction func playVideo(_ sender: Any) {
if(playButton == false){
playButton = true;
// (sender as! UIButton).setTitle("Pause",for: [])
(sender as! UIButton).setBackgroundImage(UIImage(systemName: "pause.circle"),for: UIControl.State.normal)
MusicPlayer.shared.startBackgroundMusic()
}else
{
playButton = false;
// (sender as! UIButton).setTitle("Play",for: [])
(sender as! UIButton).setBackgroundImage(UIImage(systemName: "play.circle"),for: UIControl.State.normal)
MusicPlayer.shared.stopBackgroundMusic()
//self.player.pause()
}
}
}