如何使用AVPlayer和形象的UITableViewCell取决于从我的API响应

问题描述 投票:0回答:1

功能:我需要实现的tableview,其中细胞将有一个图像或取决于我的API类的视频。

发生了什么:图像和视频的显示良好,但在显示视频细胞它取代所有的细胞与AVPlayer,有太多的图像细胞。

代码: -

(in cellForRowAtIndexPath - )

if (![dict[@"video_360x290"] isEqualToString:@""]) {

 AVPlayerItem* playerItem = [AVPlayerItem playerItemWithURL:[NSURL URLWithString:dict[@"video_360x290"]]];
 AVPlayer *playVideo = [[AVPlayer alloc] initWithPlayerItem:playerItem];
 cell.playerViewController = [[AVPlayerViewController alloc] init];
 cell.playerViewController.player = playVideo;
 cell.playerViewController.player.volume = 10;
 cell.playerViewController.view.frame = CGRectMake(0, 0, cell.vPost.frame.size.width, cell.vPost.frame.size.height);
 [cell.vPost addSubview:cell.playerViewController.view];
 [playVideo play];

}
else{
 [cell.iPost sd_setImageWithURL:dict[@"image"] placeholderImage:[UIImage imageNamed:@"imageNotAvailable"] options:SDWebImageHighPriority];
}

我需要的是什么:我需要与图像单元格显示的图像和细胞与视频显示器的视频在此先感谢。

objective-c uitableview video avplayer
1个回答
1
投票

让TableViewCell类名是ImageVideoCell

在TableView中,1代表图像和2视频创建从故事板中的三个单元并改变小区标识符。对于图像小区改变标识符“的ImageCell”和视频小区更改标识为“VideoCell1”和“VideoCell2”像这里ImageVideoCell所有单元格添加相同tableViewCell类。

而在控制器

class ViewController: UIViewController {

  // MARK: - Outlets
  @IBOutlet weak var tableView: UITableView!

  // MARK: - Properties
  var imgVideos = [ImgVideo?]()
  var player : AVPlayer?

  // MARK: - View Life Cycle
  override func viewDidLoad() {
    super.viewDidLoad()

    // API call to get Images and videos in TableView
    tableView.dataSource = self
}


 override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(true)

    if player?.isPlaying == true {
        self.player?.pause()
        self.player = nil
    }
}

// MARK: - Action
@IBAction func play_pauseButtonPressed(_ sender: UIButton) {

    guard let video = imgVideos[sender.tag] else { return } // get video URl here
    guard let cell = tableView.cellForRow(at: IndexPath(row: sender.tag, section: 0)) as? ImageVideoCell else { return }

    // if Video is already Playing And User want to Pause it then
    if player?.isPlaying ?? false && video.isSelected {
        player?.pause()
        sender.setImage(#imageLiteral(resourceName: "play-button"), for: .normal)

    }
    // if the Video is pause and User want to Play it again
    else if player?.isPlaying ?? true == false && video.isSelected {
        player?.play()
        sender.setImage(#imageLiteral(resourceName: "media-pause"), for: .normal)

    }
    // Play any other Video 
    else {

        for (index, video) in audios.enumerated() {

            if video?.isSelected ?? false {
                video?.isSelected = false
                tableView.beginUpdates()
                tableView.reloadRows(at: [IndexPath(row: index, section: 0)], with: .none)
                tableView.endUpdates()
            }
        }
        video.isSelected = true
        tableView.beginUpdates()
        tableView.reloadRows(at: [IndexPath(row: sender.tag, section: 0)], with: .none)
        tableView.endUpdates()

        guard let cell = tableView.cellForRow(at: IndexPath(row: sender.tag, section: 0)) as? ImageVideoCell else { return }
        cell.play_pauseButton.setImage(#imageLiteral(resourceName: "media-pause"), for: .normal)



        play(videoUrl: vedio.url ?? "") // Play is a function for playing Videos
    }

}

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  let cell = ImageVideoCell()
   if Image {
    cell = tableView.dequeueReusableCell(withIdentifier: "ImageCell", for: indexPath) as! ImageVideoCell
   // Display Img
   }else {

     if video?.isSelected ?? false {
      cell = tableView.dequeueReusableCell(withIdentifier: "VideoCell1", for: indexPath) as! ImageVideoCell
     }else {
      cell = tableView.dequeueReusableCell(withIdentifier: "VideoCell2", for: indexPath) as! ImageVideoCell
     }

   }

   cell.play_pauseButton.tag = indexPath.row
   cell.play_pauseButton.setImage(audio?.isSelected ?? false ? #imageLiteral(resourceName: "media-pause") : #imageLiteral(resourceName: "play-button"), for: .normal)
   return cell
  }
}


// MARK: - AVPlayer
  extension AVPlayer {
     var isPlaying: Bool {
         return ((rate != 0) && (error == nil))
     }
  }
© www.soinside.com 2019 - 2024. All rights reserved.