AVPlayerViewController using AVPlayer audio

Just start with AVKit, and I'm trying to play audio. It would be nice to use the new AVPlayerViewController, described in detail in Mastering Modern Multimedia Playback , so that I have a ready-made play / pause / search user interface. I basically have a storyboard with a container view that has a built-in segue for AVPlayerViewController. Then I use:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if (segue.identifier == "embedAudio") {
        var playerViewController:AVPlayerViewController = segue.destinationViewController as AVPlayerViewController

        playerViewController.player = AVPlayer(URL: NSURL(string: "http://dts.podtrac.com/redirect.mp3/files.serialpodcast.org/sites/default/files/podcast/1420057326/serial-s01-e01.mp3"))
    }
}

It fits well and plays, but it has a big black QuickTime symbol where the video will be. I am wondering if there is a way to make it more like the Music or Podcasts application.

+4
source share
2 answers

Do not use AVPlayerViewController , as it is a full-screen, black box, video player.

Instead, create your own view controller, say, a toolbar and controls available in the OS (play, pause, stop, etc.) and attach them to yours AVAudioPlayer. Here's what your own view controller code looks like:

Maintain Player Instance

var audioPlayer:AVAudioPlayer!
@IBOutlet weak var playProgress: UIProgressView!

Example: Playback

@IBAction func doPlayAction(_ sender: AnyObject) {
    do {
        try audioPlayer = AVAudioPlayer(contentsOf: audioRecorder.url)
        audioPlayer.play()
    } catch {}
}

Example: Stop

@IBAction func doStopAction(_ sender: AnyObject) {
    if let audioPlayer = self.audioPlayer {
        audioPlayer.stop()
    }
}

Example: Tracking Progress

func playerProgress() {
    var progress = Float(0)
    if let audioPlayer = audioPlayer {
        progress = ((audioPlayer.duration > 0)
            ? Float(audioPlayer.currentTime/audioPlayer.duration)
            : 0)
    }
    playProgress.setProgress(progress, animated: true)
}

I published an example of recording and playback using the methodology outlined in this answer.

Preview

β–Ί GitHub .

+3

? AVPlayerViewController contentOverlayView. UIView QuickTime ( ) .

, , AVPlayerViewController, contentOverlayView nil prepareForSegue

+3

All Articles