在swift 2.0中暂停mp3文件

时间:2016-03-29 11:06:30

标签: ios swift mp3 avaudioplayer avplayer

我有一个问题: 我想创建一个暂停按钮,当我选择一首新歌时=>目前的歌曲将停止播放新歌。 我是一个非常新的swift,所以我希望你可以帮我完整的代码。 非常感谢你

这是我的代码:

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()

}

1 个答案:

答案 0 :(得分:0)

您的代码中存在许多基本错误,因此我建议您阅读更多教程,尤其是swift基本文档。

我为你突出了它们并且说实话我不认为继续你正在做的事情是个好主意,直​​到你对swift基础知识更加舒适。

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()

}

现在,如果您仍在寻找问题的答案,请在gitHub上查看我的助手,它非常简单,并向您展示如何暂停歌曲等。

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