Not exactly sure what I'm doing wrong here. First time working with Swift. Trying to play a simple audio file.
// 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://ift.tt/1PxL9my" // This is a Shoutcast stream.
// Not using it for now
let url = "http://ift.tt/1UZ1zK0"
// 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.
}
}
So, what's wrong? Well, on my iPhone 5, running 8.3, at least, I get a network loading indicator in the top left corner of my screen, but no audio. It acts like it's TRYING to play, but no sound comes out of my device.
I really have no idea why this isn't working. I've seen other examples work and they were exactly this. I could unde3rstand that Shoutcast/Icecast streams require more work, but this is just a freaking static MP3 file.
I have AVFoundation and CFNetwork compiling with my app. I don't think I need CFNetwork, but oh well.
I'm targeting 8.3, running the latest xcode (Just downloaded it yesterday) and am building on a VM of 10.11.4. I would update my iPhone, but I can't right now.
Aucun commentaire:
Enregistrer un commentaire