不完全确定我在这里做错了什么。第一次使用Swift。试着播放一个简单的音频文件。
// Imports I need
import UIKit
import AVFoundation
class ViewController: UIViewController {
var isPlaying = false // variable to control playback selection, used in togglePlayPause
var player = AVPlayer()
// A button I have in the center of the View Controller
@IBOutlet weak var PlayButton: UIButton!
// A method that toggles playback when that button is hit
// This SEEMS to work. Everything except audio playing and pausing seems to be working
// when I debug
@IBAction func togglePlayPause(sender: AnyObject) {
if (isPlaying) {
player.pause()
PlayButton.setTitle("Play", forState: UIControlState.Normal) // This works
print("Pause") // and this
} else {
player.play()
PlayButton.setTitle("Pause", forState: UIControlState.Normal) // This works
print("Play") // and so does this
}
isPlaying = !isPlaying // I do end up swapping each time this runs. I have
// Breakpoints set up on the first statements of each
// and I hit the opposite one each time I press the button
// Oh, and the text on the button changes accordingly
}
override func viewDidLoad() {
super.viewDidLoad()
//let url = "http://192.99.131.205:8000/stream.mp3" // This is a Shoutcast stream.
// Not using it for now
let url = "http://www.nasa.gov/mp3/640149main_Computers%20are%20in%20Control.mp3"
// Normal MP3 file from the web
player = AVPlayer(URL: (NSURL(string: url))!) // This SHOULD make an AVPlayer
// that connects to said URL and
player.play() // this should play it
isPlaying = true // Keep track
PlayButton.setTitle("Pause", forState: UIControlState.Normal) // Set button text
player.volume = 1 // FORCE VOLUME FULL JUST IN CASE
}
// I honestly could remove this
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
那么,有什么不对?好吧,至少在我的iPhone 5上运行8.3,我的屏幕左上角有一个网络加载指示器,但没有音频。它的行为就像是在尝试播放,但我的设备没有声音。
我真的不知道为什么这不起作用。我已经看到其他例子有效,他们就是这样。我可以说,Shoutcast / Icecast流需要更多的工作,但这只是一个非常疯狂的MP3文件。
我有AVFoundation和CFNetwork编译我的应用程序。我不认为我需要CFNetwork,但是哦。哦。
我定位8.3,运行最新的xcode(昨天刚刚下载),并且正在建立在10.11.4的VM上。我会更新我的iPhone,但我现在不能。