列表播放视频简单实现

最近因项目需求,需要在滚动列表中播放视频。查了半天资料,发现其实现没有想象中的那么难。

原理

众所周知,由于SurfaceView没有UI同步缓冲区,就导致了在列表滚动的时候,正在播放的视频跟不上滚动的步伐;而在SDK 15的时候,谷歌官方提供了新的具有UI同步缓存的视频播放api TextureView。
本文真是基于该新Api实现。

代码实现

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:orientation="vertical"
    android:focusable="true"
    >

  <TextureView
      android:id="@+id/video"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      />

  <ProgressBar
      android:id="@+id/pb"
      
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于Swift中的列表播放视频,可以使用AVPlayer和AVPlayerViewController来实现。下面是一个简单的示例代码: ```swift import UIKit import AVKit class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { @IBOutlet weak var tableView: UITableView! let videoList = [ Video(title: "Video 1", url: "https://example.com/video1.mp4"), Video(title: "Video 2", url: "https://example.com/video2.mp4"), Video(title: "Video 3", url: "https://example.com/video3.mp4") ] func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return videoList.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "videoCell", for: indexPath) cell.textLabel?.text = videoList[indexPath.row].title return cell } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { let videoUrl = URL(string: videoList[indexPath.row].url)! let player = AVPlayer(url: videoUrl) let playerViewController = AVPlayerViewController() playerViewController.player = player present(playerViewController, animated: true) { player.play() } } } struct Video { let title: String let url: String } ``` 在这个示例中,我们首先定义了一个Video结构体,其中包含视频的标题和URL。然后,我们创建了一个包含三个Video对象的数组videoList。 接下来,我们实现UITableViewDataSource和UITableViewDelegate协议中的方法,以在表格视图中显示视频列表。 在tableView(_:didSelectRowAt:)方法中,当用户点击表格视图中的某个行时,我们获取选定视频的URL并使用AVPlayer创建一个新的视频播放器。然后,我们创建一个AVPlayerViewController并将其设置为当前视图控制器的子视图控制器,最后我们播放视频

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值