简体   繁体   English

AVPlayerViewController 使用纯音频 AVPlayer

[英]AVPlayerViewController using audio-only AVPlayer

Just starting out with AVKit, and I'm trying to play some audio.刚开始使用 AVKit,我正在尝试播放一些音频。 It would be nice to use the new AVPlayerViewController detailed in Mastering Modern Media Playback so that I have a ready-made play/pause/seek UI.使用掌握现代媒体播放中详述的新 AVPlayerViewController 会很好,这样我就有一个现成的播放/暂停/搜索 UI。 I basically have a storyboard with a container view which has an embed segue to the AVPlayerViewController.我基本上有一个带有容器视图的故事板,它具有到 AVPlayerViewController 的嵌入 segue。 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 embeds nicely and plays, but it has a big black QuickTime symbol where the video would be.它可以很好地嵌入和播放,但在视频所在的位置有一个大的黑色 QuickTime 符号。 I'm wondering if there's a way to make it look more like the Music or Podcasts app.我想知道是否有办法让它看起来更像音乐或播客应用程序。

Why reinvent the wheel?为什么要重新发明轮子? Just use AVPlayerViewController 's contentOverlayView property.只需使用AVPlayerViewControllercontentOverlayView属性。 Here you can set an album artwork image, or place any UIView between the QuickTime logo (where the video would be), and the controls.您可以在此处设置专辑插图图像,或在 QuickTime 徽标(视频所在的位置)和控件之间放置任何UIView

Note that you must do this after the AVPlayerViewController has been presented, as its contentOverlayView property will be nil in prepareForSegue请注意,您必须在AVPlayerViewController出现后执行此操作,因为它的contentOverlayView属性在prepareForSegue将为 nil

Do not use the AVPlayerViewController , as it is a full screen, black box, video player.不要使用AVPlayerViewController ,因为它是一个全屏、黑匣子、视频播放器。

Instead, create your own view controller with, say, a toolbar and controls readily available in the OS (play, pause, stop, etc.) and hook them to your AVAudioPlayer .相反,创建您自己的视图控制器,例如,在操作系统中可用的工具栏和控件(播放、暂停、停止等)并将它们挂接到您的AVAudioPlayer This is what the code of your very own view controller may look like:这是您自己的视图控制器的代码可能如下所示:

Maintain an instance of the player维护一个播放器实例

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

Example: Play示例:播放

@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: Track 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 have posted a recording and playback example using the methodology outlined in this answer.我已经使用此答案中概述的方法发布了一个录制播放示例。

预览

► Find this solution on GitHub . ► 在GitHub 上找到此解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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