热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何显示AVPlayer播放音频的进度-HowtodisplayprogressofAVPlayerplaybackofaudio

Imbuildingamusicappthatwillhaveafeedofpoststhatcontainurltomusicfiles.Whenacell

I'm building a music app that will have a feed of posts that contain url to music files. When a cell in UITableView becomes fully visible playback of a corresponding music track starts. This is a layout I have now.

我正在构建一个音乐应用程序,其中包含包含音乐文件URL的帖子。当UITableView中的单元格变为完全可见时,将开始播放相应的音乐曲目。这是我现在的布局。

Layout

Waveform is being generated by a server and I receive its data as an array of type [Float]. Complete waveform is a UIView that has a bar subviews with a height of a corresponding item from array from a server.

波形由服务器生成,我将其数据作为[Float]类型的数组接收。完整波形是一个UIView,它有一个条形子视图,其高度与服务器阵列中的相应项目相同。

Here is a WaveformPlot Source:

这是一个WaveformPlot来源:

class WaveformPlot: UIView {


  override init(frame: CGRect) {
    super.init(frame: frame)
    
  }
  required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    

  }

  //MARK: Populate plot with data from server response
  
  func populateWithData(from dataSet: [Float]){
    
    DispatchQueue.main.async {
      
      var offset: CGFloat = 0
      
      for index in 0..

Source of a player I've implemented.

我实施的玩家的来源。

class StreamMusicPlayer: AVPlayer {
  private override init(){
    
    super.init()
    
  }
  
  static var currentItemURL: String?
  
  static var shared = AVPlayer()
  
  static func playItem(musicTrack: MusicTrack) {
    
    StreamMusicPlayer.shared = AVPlayer(url: musicTrack.trackURL)
    
    StreamMusicPlayer.currentItemURL = musicTrack.trackURL.absoluteString
    
    StreamMusicPlayer.shared.play()
    
    
    
  }
}

extension AVPlayer {
  
  var isPlaying: Bool {
    return rate != 0 && error == nil
  }
  
}

The problem is that it is required to show progress of a track currently playing in a corresponding cell waveform.

问题是需要显示当前在相应的单元波形中播放的轨道的进度。

It has to be similar to this:

它必须与此类似:

enter image description here

What approach should i choose? I'm out of ideas how to implement this. Any help appreciated.

我应该选择什么方法?我没有想法如何实现这一点。任何帮助赞赏。

1 个解决方案

#1


1  

The method described by war4l is a way of showing the progress. But to actually get the progress you need to add a periodic time observer. Something like this.

war4l描述的方法是显示进度的方式。但要实际获得进度,您需要添加一个定期时间观察器。像这样的东西。

let interval = CMTime(value: 1, timescale: 2)
StreamMusicPlayer.shared.addPeriodicTimeObserver(forInterval: interval, queue: DispatchQueue.main, using: { (progressTime) in

     let secOnds= CMTimeGetSeconds(progressTime)

     if let duration = self.StreamMusicPlayer.shared.currentItem?.duration {                   

         let totalDuratiOnInSeconds= CMTimeGetSeconds(duration)

         // Now you have current time and the duration so can display this somehow                          
    }
})

推荐阅读
author-avatar
多米音乐_34216717
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有