I'm developing a wrapper to stream icecast radio. This is some of my code:
This loads the URL for the Radio
var radio = Radio(userAgent: "my App")
var playing = true
var link = "http://ift.tt/1zJKb3z"
This is the playbutton:
// MARK: RadioPlayPause
@IBAction func toggleRadio(sender: UIButton) {
if (playing)
{
playing = false
radio.updatePlay(false)
sender.setImage(UIImage(named: "play"), forState: UIControlState.Normal)
}
else
{
playing = true
radio.updatePlay(true)
sender.setImage(UIImage(named: "pause"), forState: UIControlState.Normal)
AVAudioSession.sharedInstance().setActive(true, error: nil)
}
}
And here in the viewDidLoad is that connects to the radio:
// RadioLader
radio.connect(link, withDelegate: self, withGain: 1.0)
And here some of the code that loads the title and the interpret What i get is for example this:
'Fox Stevenson - Sweets (Soda Pop)';
so i had to play this arround so it shows me interpret: Fox Stevenson, Title: Sweets. If you have any tips for doing this easyier i'd be very pleased because i'm new to swift.
func metaTitleUpdated(title: String) {
var length = countElements(title)
if length < 17 {
println("kein Titel")
interpretLabel.text = " "
titleLabel.text = "Spot/Nachrichten"
}
else {
var StreamTitle = split(title) {$0 == "="}
var titelinterpret: String = StreamTitle[1]
var titelinterpretgetrennt = split(titelinterpret) {$0 == "-"}
var titel: String = titelinterpretgetrennt[1]
titel = dropLast(titel)
titel = dropFirst(titel)
titel1 = titel
titel1 = dropLast(titel1)
interpret = titelinterpretgetrennt[0]
interpret = dropFirst(interpret)
titleLabel.text = titel1
interpretLabel.text = interpret
println("\(titelinterpret) wird gerate gespielt")
println("\(titel1) ist der Song")
println("\(interpret) ist der Interpret")
// Sendet Info an NowPlayingInfoCenter
let playingInfo = [MPMediaItemPropertyAlbumArtist: interpret, MPMediaItemPropertyAlbumTitle: titel1]
MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo = playingInfo
}
}
I know that the radio.connect in the viewdidload is causing that problem. I can put the radio.connect also into the playbutton and remove it from the viewdidload:
@IBAction func toggleRadio(sender: UIButton) {
if (playing)
{
playing = false
radio.updatePlay(false)
sender.setImage(UIImage(named: "play"), forState: UIControlState.Normal)
}
else
{
radio.connect(link, withDelegate: self, withGain: 1.0)
playing = true
radio.updatePlay(true)
sender.setImage(UIImage(named: "pause"), forState: UIControlState.Normal)
AVAudioSession.sharedInstance().setActive(true, error: nil)
}
}
With this option the music doesn't start playing again when reloading the view. But the problem is, that the song name and interpret doesn't reload anymore.
Thank you :)
Aucun commentaire:
Enregistrer un commentaire