1

I have a problem: I want to create an pause button and when I choose a new song => current song will be stop and play new song. I am very new in swift so I hope you can help me with full code. thank you a lot

this is my code:

import UIKit
import AVFoundation
class ChiTietViewController: UIViewController {
var thamsoTruyen:NSUserDefaults!


override func viewDidLoad() {
    super.viewDidLoad()
    self.view.backgroundColor = UIColor(patternImage: UIImage(named: "nen.jpg")!)
    thamsoTruyen = NSUserDefaults()
    var myplayer:AVPlayer!
    var playeritem: AVPlayerItem!
    var row:Int = thamsoTruyen.objectForKey("number") as! Int
    var url:NSURL = NSURL(string:"http://ozz4u.com/danhsach.php?cot=mp3")!
    var ds:NSString!
    do{
        ds = try NSString(contentsOfURL: url, encoding: NSUTF8StringEncoding)
    } catch{
    }
    var chuoi:[String] = ds.componentsSeparatedByString("#") as [String]

    var error:NSError? = nil
    var u:NSURL = NSURL(string: chuoi[row])!
    playeritem = AVPlayerItem(URL: u)
    myplayer = AVPlayer(playerItem: playeritem)
    let playerlayer = AVPlayerLayer(player: myplayer)
    playerlayer.frame = CGRectMake(0, 0, 10, 15)
    self.view.layer.addSublayer(playerlayer)
    myplayer.play()

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

}
Vvk
  • 4,031
  • 29
  • 51
ozzbmt
  • 13
  • 4
  • My suggestion is that before you add a new feature you should fix the existing code. There's many errors and issues here. 1- local variables should not be implicitly unwrapped optionals. 2- the player should be in an instance variable, not in a local one. 3- the `catch` branch is empty, this is a big mistake, always show the caught error. 4- remove the unnecessary `var error`. 5- do not force unwrap optionals. – Eric Aya Mar 29 '16 at 11:16
  • thank you for reply. I am very new in this so I know I have many mistake. if you can please help me to edit this code please – ozzbmt Mar 29 '16 at 11:24
  • I have listed the main issues in my comment. Have a look here for an example: http://stackoverflow.com/a/32994222/2227743. Also read the Swift documentation about Optionals, this is *very* important. – Eric Aya Mar 29 '16 at 11:29
  • Thanks you! let me try – ozzbmt Mar 29 '16 at 11:33
  • I will post an answer for you. However I strongly suggest to do what Eric said and read the basic swift documentation. You code has a lot of errors – crashoverride777 Mar 29 '16 at 11:36
  • Thank you! i have the list of song here: http://ozz4u.com/danhsach.php?cot=TenBH i want to get its mp3 link from here : http://ozz4u.com/danhsach.php?cot=mp3 then it can play can you code for me example? – ozzbmt Mar 29 '16 at 11:53
  • have you check http://stackoverflow.com/questions/32993896/impossible-to-stop-avplayer – Nitin Gohel Mar 29 '16 at 12:03
  • Yes, but I dont know how to insert my MP3 LIST link into it? let player = AVPlayer(URL: NSURL(string: "http://streaming.radio.rtl.fr/rtl-1-48-192")!) can i insert to this? – ozzbmt Mar 29 '16 at 12:05

1 Answers1

0

There is many errors in your code with the fundamentals so I would suggest that you read some more tutorials, especially the swift basic documentation.

I highlighted them for you and to be honest I dont think its a good idea to continue what you are doing until you get a bit more comfortable with the swift basics.

import UIKit
import AVFoundation
class ChiTietViewController: UIViewController {

  //var thamsoTruyen:NSUserDefaults!  //BAD CODE, ALSO BAD NAMING
  let localDefaults = NSUserDefaults.standardUserDefaults()


       override func viewDidLoad() {
       super.viewDidLoad()

       self.view.backgroundColor = UIColor(patternImage: UIImage(named: "nen.jpg")!)
//thamsoTruyen = NSUserDefaults() // BAD CODE, Delete. Also not calling standardUserDefaults.
//var my player:AVPlayer! // BAD CODE, Make no sense so delete. This would make only sense if you put it above "ViewDidLoad" so you can use it across your whole class.
//var playeritem: AVPlayerItem! // See above

 var row:Int = thamsoTruyen.objectForKey("number") as! Int // BAD CODE, You are not checking if the object exists but are forcing it to be an Int, which can cause a nil crash. Also you might as well use IntegerForKey when your value is an INT. 

var row = localDefaults.integerForKey("number)" // Better option than above

var url:NSURL = NSURL(string:"http://ozz4u.com/danhsach.php?cot=mp3")!
var ds:NSString! // Make this optional?
do{
    ds = try NSString(contentsOfURL: url, encoding: NSUTF8StringEncoding)
} catch{

   // NOT CATCHING ERROR HERE, DANGEROUS 
}
var chuoi:[String] = ds.componentsSeparatedByString("#") as [String]

var error:NSError? = nil
var u:NSURL = NSURL(string: chuoi[row])! // Force unwrapping so you better make sure the array exists.

let playeritem = AVPlayerItem(URL: u)
let myplayer = AVPlayer(playerItem: playeritem)
let playerlayer = AVPlayerLayer(player: myplayer)
playerlayer.frame = CGRectMake(0, 0, 10, 15)
self.view.layer.addSublayer(playerlayer)
myplayer.play()

}

Now if you are still looking for an answer to your question check my helper on gitHub, it is very simple and shows you how to pause songs etc.

https://github.com/crashoverride777/Swift2-Music-Helper

crashoverride777
  • 10,581
  • 2
  • 32
  • 56
  • Thank you so much! I will learn hard :D i am still a student :D – ozzbmt Mar 29 '16 at 11:52
  • 1
    Yeah please don take this the wrong way, we are all learning. Its just bad idea to help you with this question when your code is flawed. Just slow down a tiny bit and do things step by step. You have to read the basic apple documentation, all of it. https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html#//apple_ref/doc/uid/TP40014097-CH5-ID309 – crashoverride777 Mar 29 '16 at 11:54
  • Yeah :D i just want to have a good demo to easier learning.. so i do it for myself and ask then edit => it will make me better for remember – ozzbmt Mar 29 '16 at 11:56
  • 1
    I understand. But you have to read the link I send you, its the basic swift documentation. It teaches you about optionals, optional chaining and all the other basics and is a must read. – crashoverride777 Mar 29 '16 at 11:57
  • Awesome. They are not that long and its really basic stuff written in as much plain english as possible. I know sometimes its better to just do stuff rather than read, but in that case you should at least read those. Also make sure that when you watch tutorials online that they are as modern as possible so you get proper swift advice and not some clunky ObjC conversion. – crashoverride777 Mar 29 '16 at 12:05
  • Also just so you can get a further understanding, I have a music helper on gitHub. You can check it out to see how to handle music in a nice and clean way, including the question that you asked. https://github.com/crashoverride777/Swift2-Music-Helper – crashoverride777 Mar 29 '16 at 12:07
  • Do you have any tutorial about play MP3 list online? i can did play mp3 file local but online mp3 list which i am look for – ozzbmt Mar 29 '16 at 12:13
  • I dont unfortunately. – crashoverride777 Mar 29 '16 at 12:15
  • Yeah. :D my project which i posted here to ask it was running but cant stop and when i choose other song the curent song and new song, they play all :D – ozzbmt Mar 29 '16 at 12:22
  • Which is why I send you my gitHub project. It shows you all of this, just read the instructions and check the code. I will put the link into my answer as well so if you feel it helped you could please mark it correct. – crashoverride777 Mar 29 '16 at 12:24