运行时swift时无法更改类变量

时间:2015-05-06 17:43:51

标签: ios class swift variables

这就是我想要做的。我想在项目运行时更改类变量并在播放器中使用它。例如,当用户播放按钮时应用程序正在运行,播放器将停止并更改URL并重新开始。我该怎么办?

编辑:改变问题以避免论坛污染。

let pa = PlayerAv()

class PlayerAv
{
    var audioLink = ""
    var player: AVPlayer
    init()
    {
        player = AVPlayer(URL: NSURL(string: self.audioLink))
    }
}

@IBAction func changeToSabiha() {
    pa.player.pause()
    PlayerAv().audioLink = "http://www.liveatc.net/play/ltfj.pls"
    println("\(pa.audioLink)")
    pa.player.play()
}

整个代码

//
//  ViewController.swift
//  LTBA ATC
//
//  Created by Arda KARACA on 29/04/15.
//  Copyright (c) 2015 Arda's. All rights reserved.
//

import UIKit
import AVFoundation
import MediaPlayer
import GoogleMobileAds


class ViewController: UIViewController {


    @IBOutlet weak var statLabel: UILabel!
    @IBOutlet weak var ataturkButton: UIButton!
    @IBOutlet weak var sabihaButton: UIButton!
    @IBOutlet weak var esenbogaButton: UIButton!
    @IBOutlet weak var weatherButton: UIButton!
    @IBOutlet weak var statusLabel: UILabel!
    @IBOutlet weak var playButton: UIButton!

    var googleBannerView: GADBannerView!




    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        googleBannerView = GADBannerView(adSize: kGADAdSizeSmartBannerPortrait)
        googleBannerView.adUnitID = "ca-app-pub-5378899862041789/8532100959"
        googleBannerView.rootViewController = self
        var request: GADRequest = GADRequest()
        googleBannerView.loadRequest(request)

        googleBannerView.frame = CGRectMake(0, view.bounds.height - googleBannerView.frame.size.height, googleBannerView.frame.size.width, googleBannerView.frame.size.height)
        self.view.addSubview(googleBannerView!)



        //setTitle commands start
        statusLabel.text = "Status: Not Playing"
        playButton.setTitle("Play", forState: UIControlState.Normal)
        weatherButton.setTitle("Show Weather", forState: .Normal)
        ataturkButton.setTitle("Ataturk - LTBA", forState: .Normal)
        sabihaButton.setTitle("Sabiha Gökçen - LTFJ", forState: .Normal)
        esenbogaButton.setTitle("Esenboğa - LTAC", forState: .Normal)
        //setTitle commands end





        switch Reachability.isConnectedToNetwork() {
        case false :
            println("Internet connection FAILED")
            var alert = UIAlertView(title: "No Internet Connection", message: "Make sure your device is connected to the internet.", delegate: nil, cancelButtonTitle: "OK")
            alert.show()
        default:
            println("Internet connection OK")


        }


    }//viewDidLoad() end

    let pa = PlayerAv().audioLink

    class PlayerAv
    {
        var audioLink:String = ""
        var player: AVPlayer
        init()
        {
            player = AVPlayer(URL: NSURL(string: self.audioLink))
        }
    }


    @IBAction func sliderValueChanged(sender: UISlider) {
        var currentValue = Float(sender.value)
        println(currentValue)
        PlayerAv().player.volume = currentValue
    }


    @IBAction func getWeatherWindow(sender: AnyObject) {
        UIApplication.sharedApplication().openURL(NSURL(string: "http://www.aviationweather.gov/adds/metars/?station_ids=ltac&std_trans=standard&chk_metars=on&hoursStr=most+recent+only&chk_tafs=on&submitmet=Submit")!)
        println("Directed to weather page")
    }





    //Tower change statements start

    @IBAction func changeToAtaturk() {
        PlayerAv().player.pause()
        pa == "http://www.liveatc.net/play/ltba.pls"
        println("\(pa)--a")
        PlayerAv().player.play()
    }



    @IBAction func changeToEsenboga() {
        PlayerAv().player.pause()
        PlayerAv().audioLink = "http://www.liveatc.net/play/ltac.pls"
        println("\(pa)--a")
        PlayerAv().player.play()
    }

    @IBAction func changeToSabiha() {
        PlayerAv().player.pause()
        pa == "http://www.liveatc.net/play/ltfj.pls"
        println("\(pa)--a")
        PlayerAv().player.play()
    }


    //Tower change statements end




    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    @IBAction func buttonPressed(sender: AnyObject) {
        toggle()
    }


    func toggle() {
        if playButton.titleLabel?.text == "Play" {
            playRadio()
            println("Playing")
            statusLabel.text = "Status: Playing"
        } else {
            pauseRadio()
            println("Paused")
            statusLabel.text = "Status: Paused"
        }
    }

    func playRadio() {
        PlayerAv().player.play()
        playButton.setTitle("Pause", forState: UIControlState.Normal)

    }

    func pauseRadio() {
        PlayerAv().player.pause()
        playButton.setTitle("Play", forState: UIControlState.Normal)
    }

    override func remoteControlReceivedWithEvent(event: UIEvent) {
        if event.type == UIEventType.RemoteControl {
            if event.subtype == UIEventSubtype.RemoteControlPlay {
                println("received remote play")
                playRadio()
            } else if event.subtype == UIEventSubtype.RemoteControlPause {
                println("received remote pause")
                pauseRadio()
            } else if event.subtype == UIEventSubtype.RemoteControlTogglePlayPause {
                println("received toggle")
                toggle()
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

这不好,因为您创建的网址无效:

// NOT GOOD
class PlayerAv {
    var audioLink: String = ""
    var player: AVPlayer
    init()
    {
        player = AVPlayer(URL: NSURL(string: self.audioLink))
    }
}
let myPlayer = PlayerAv()

一旦知道要使用的URL,最好初始化播放器:

// GOOD
class PlayerAv {
    var audioLink: String?
    var player: AVPlayer
    init(link: String) {
        self.audioLink = link
        self.player = AVPlayer(URL: NSURL(string: link))
    }
}

另外,请将其设为var,以便我们稍后更改内容:

var myPlayer = PlayerAv(link: "http://yourFirstURLhere")

现在,此myPlayer对象是您的播放器

如果您想使用它:

myPlayer.player.play()
myPlayer.player.pause()
myPlayer.player.volume = 2

如果你想要一个有新链接的玩家,现在需要做些什么?

当您使用新链接创建新的时,您想将此新播放器分配给同一个实例变量,所以:

myPlayer = PlayerAv(link: "http://yourNewURLhere")
myPlayer.player.play()

请注意,我们刚刚为现有的myPlayer变量分配了一个新创建的播放器对象。

如果要打印当前链接:

println(myPlayer.audiolink!)

myPlayer对象内部是播放器本身(player)和链接(audioLink)。

注意:这些是基于您的代码的示例。但是,对于使用AVPlayer的有趣工作示例,您应该查看this之类的内容。