简体   繁体   中英

Why can't I hear this mp3 file on my ios simulator?

import UIKit
import AVFoundation
class MainViewController: UIViewController,ViewController2Delegate,AVAudioPlayerDelegate {

    @IBOutlet weak var b1: UIButton!
    @IBAction func b1_pressed(sender: AnyObject) {
        NSLog("hi");
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        var player: AVPlayer! = nil
        var steamingURL:NSURL! = NSURL(string:"http://yflvr.com:8080/data/songs1/mp3/16287239.mp3")
        player = AVPlayer(URL: steamingURL)
        player.play()
    }

Your AVPlayer object will be deallocated after viewDidLoad finishes because you assigned the player to a local variable. You need to create a property for player instead.

@IBOutlet weak var b1: UIButton!
@IBAction func b1_pressed(sender: AnyObject) {
        NSLog("hi");
    }

var player: AVPlayer!

    override func viewDidLoad() {
        super.viewDidLoad()
        var steamingURL:NSURL! = NSURL(string:"http://yflvr.com:8080/data/songs1/mp3/16287239.mp3")
        player = AVPlayer(URL: steamingURL)
        player.play()
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM