菜鸟教程小白 发表于 2022-12-11 18:17:58

ios - 我无法在 tablecell 上暂停播放视频


                                            <p><p>我曾经使用 avplayer 在 <code>UITableViewCell</code> 上加载视频。我可以在 <code>UITableViewCell</code> 上加载视频,但无法暂停/播放视频。我不能做任何基本的功能,比如增加/减少音量。请任何人帮我解决这个问题。
这是我的代码:</p>

<pre><code>func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell
    {

      let cell: FeedCell = tableView.dequeueReusableCell(withIdentifier: &#34;VedioCell&#34;, for: indexPath) as! FeedCell

      let videoURL = NSURL(string: &#34;https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4&#34;)
          player = AVPlayer(url: videoURL! as URL)
      let playerLayer = AVPlayerLayer(player: player)

      playerLayer.frame = CGRect(x:0, y: 0, width: UIScreen.main.bounds.width, height: 200)
       cell.PlayerView?.layer.addSublayer(playerLayer)
          player.play()

    return cell
    }
</code></pre>

<p>我在这里使用了avplayer,但我无法访问</p>

<p>提前致谢。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>试试这个</p>

<p>func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
      {</p>

<pre><code>      let cell: FeedCell = tableView.dequeueReusableCell(withIdentifier: &#34;VedioCell&#34;, for: indexPath) as! FeedCell

      let player = playerArray
      let playerLayer = AVPlayerLayer(player: player)
      playerLayer.frame = CGRect(x:0, y: 0, width: UIScreen.main.bounds.width, height: 200)
       cell.PlayerView?.layer.addSublayer(playerLayer)

      cell.play.tag = indexPath.row

      cell.play.addTarget(self, action: #selector(GetUpdatesButtonTouched), for: .touchUpInside)


               return cell
    }


@IBAction func GetUpdatesButtonTouched(_ sender: AnyObject) {

      if sender.currentTitle == &#34;Play&#34; {
            for i in 0..&lt;10{
                currentPlayer=playerArray
                currentPlayer.pause()
                print(&#34;tag: \(sender.tag)&#34;)
                print(&#34;tag: \(playerArray)&#34;)
                currentPlayer=playerArray
                print(&#34;tag: \(currentPlayer)&#34;)
                //handleVideoChageSlider((&#34;&#34; as AnyObject) as! UISlider)
                currentPlayer.play()

                sender.setTitle(&#34;Pause&#34;, for: UIControlState.normal)

            //let indexPath = tableView.indexPathForSelectedRow!
               let cell: FeedCell = tableView.cellForRow(at:sender.indexPath)as! FeedCell

                //print(currentCell.videoLabel.text)

                //cell.slider.tag=indexPath.row
                cell.slider.addTarget(self, action: #selector(handleVideoChageSlider), for:.valueChanged )

            }
      }
      else if sender.currentTitle == &#34;Pause&#34;{
            for i in 0..&lt;10{
                currentPlayer=playerArray
                currentPlayer.pause()
                sender.setTitle(&#34;Play&#34;, for: UIControlState.normal)
            }
      }
    }
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 我无法在 tablecell 上暂停播放视频,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/41034365/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/41034365/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 我无法在 tablecell 上暂停播放视频