stopPlayback后的And​​r​​oid VideoView清晰显示() [英] Android VideoView clear display after stopPlayback()

查看:539
本文介绍了stopPlayback后的And​​r​​oid VideoView清晰显示()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是VideoView组件播放视频。我似乎无法清除VideoView显示器后,我手动调用stopPlayback()。我想重置VideoView使原来的背景是可见的。

  1. 在一个VideoView组件播放视频。
  2. 选择一个新的视频播放。
  3. 的VideoView粘在第一视频的最后一帧上,直到下一个视频开始。如果有一个与下一个视频的误差,从所述第一视频的静态图象仍然停留在那里。
  4. 如果我让视频播放完成状态时,显示被清除。

我正在使用的code:

 私人VideoView视频查看= NULL;

公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState){
...
  如果(视频查看!= NULL){
    videoViewer.setOn preparedListener(新在preparedListener());
  }
...
}

公共无效onItemClick(适配器视图<>母公司视图中查看,INT位置,长的id){
  如果(视频查看!= NULL){
    videoViewer.stopPlayback();
    videoViewer.setVideoURI(Uri.parse(HTTP://my_vido_url/playlist.m3u8));
  }
}

私有类在preparedListener实现MediaPlayer.On preparedListener {
  @覆盖
  公共无效于prepared(MediaPlayer的MP){
    videoViewer.start();
  }
}
 

请注意,基于VideoView.java源,stopPlayback()主罚基础上的MediaPlayer以下操作:

 公共无效stopPlayback(){
  如果(mMediaPlayer!= NULL){
    mMediaPlayer.stop();
    mMediaPlayer.release();
    mMediaPlayer = NULL;
  }
}
 

解决方案

我看中了,除非有人能提供一个更好的解决办法,就是用 setBackgroundResource ,其中因为它似乎改变前景似乎资源名为奇怪。

 公开查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState){
...
  videoViewer.setBackgroundResource(R.drawable.viewer_background);
...
}

公共无效onItemClick(适配器视图<>母公司视图中查看,INT位置,长的id){
  如果(视频查看!= NULL){
    videoViewer.stopPlayback();
    //设置背景回绘制资源切换到一个新的时清除当前的视频。
    videoViewer.setBackgroundResource(R.drawable.viewer_background);
    videoViewer.setVideoURI(Uri.parse(HTTP://my_vido_url/playlist.m3u8));
  }
}

私有类在preparedListener实现MediaPlayer.On preparedListener {
  @覆盖
  公共无效于prepared(MediaPlayer的MP){
    videoViewer.start();
    //清除当视频是ppared玩$ P $后台资源。
    videoViewer.setBackgroundResource(0);
  }
}
 

我引用的绘制是一个简单的图层列表与自定义蓝色背景和中心的标志形象。

绘制\ viewer_background.xml:

 < XML版本=1.0编码=UTF-8&GT?;
<层列表的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
  <项目>
    <形机器人:形状=矩形>
      [固体机器人:颜色=@色/ custom_blue/>
    < /形状>
  < /项目>
  <项目>
    <位图机器人:SRC =@可绘制/ img_logo
      机器人:重力=中心/>
  < /项目>
< /层列表>
 

另外我还可以使用 setZOrderOnTop 来控制,其中表面观放置(你也可以定义在视频查看上面另外的看法,切换能见度方式):

  videoViewer.setZOrderOnTop(假);
videoViewer.setZOrderOnTop(真正的);
 

另外我还可以使用 setBackgoundColor 来完成同样的事情 setBackgroundResource

  videoViewer.setBackgroundColor(getResources()的getColor(R.color.custom_blue)。);
videoViewer.setBackgroundColor(Color.TRANSPARENT);
 

I'm using a VideoView component to play videos. I can't seem to clear the VideoView display after I manually invoke stopPlayback(). I would like to reset the VideoView so that the original background is visible.

  1. Play a video in a VideoView component.
  2. Select a new video to play.
  3. The VideoView is stuck on the last frame of the first video until the next video starts. If there is an error with the next video, the static image from the first video remains stuck there.
  4. If I let the video play to completion state, the display is cleared.

The code I'm using:

private VideoView videoViewer = null;

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
...
  if (videoViewer != null) {
    videoViewer.setOnPreparedListener(new OnPreparedListener());
  }
...
}

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  if (videoViewer != null) {
    videoViewer.stopPlayback();
    videoViewer.setVideoURI(Uri.parse("http://my_vido_url/playlist.m3u8"));
  }
}

private class OnPreparedListener implements MediaPlayer.OnPreparedListener {
  @Override
  public void onPrepared(MediaPlayer mp) {
    videoViewer.start();
  }
}

Note that based on the VideoView.java source, stopPlayback() takes the following action on the underlying MediaPlayer:

public void stopPlayback() {
  if (mMediaPlayer != null) {
    mMediaPlayer.stop();
    mMediaPlayer.release();
    mMediaPlayer = null;
  }
}

解决方案

The solution I settled on, unless someone can offer a better one, is to use setBackgroundResource, which seems oddly named since it appears to change the foreground resource.

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
...
  videoViewer.setBackgroundResource(R.drawable.viewer_background);
...
}

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  if (videoViewer != null) {
    videoViewer.stopPlayback();
    // Set the background back to the drawable resource to clear the current video when switching to a new one.
    videoViewer.setBackgroundResource(R.drawable.viewer_background);
    videoViewer.setVideoURI(Uri.parse("http://my_vido_url/playlist.m3u8"));
  }
}

private class OnPreparedListener implements MediaPlayer.OnPreparedListener {
  @Override
  public void onPrepared(MediaPlayer mp) {
    videoViewer.start();
    // Clear the background resource when the video is prepared to play.
    videoViewer.setBackgroundResource(0);
  }
}

The drawable I'm referencing is a simple layer-list with a custom blue background and a centered logo image.

drawable\viewer_background.xml:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
  <item>
    <shape android:shape="rectangle">
      <solid android:color="@color/custom_blue" />
    </shape>
  </item>
  <item>
    <bitmap android:src="@drawable/img_logo"
      android:gravity="center" />
  </item>
</layer-list>

Alternatively I was also able to use setZOrderOnTop to control where the surface view is placed (you could also define another view on top of the VideoViewer and toggle the visibility that way):

videoViewer.setZOrderOnTop(false);
videoViewer.setZOrderOnTop(true);

Alternatively I could also use setBackgoundColor to accomplish the same thing as setBackgroundResource:

videoViewer.setBackgroundColor(getResources().getColor(R.color.custom_blue));
videoViewer.setBackgroundColor(Color.TRANSPARENT);

这篇关于stopPlayback后的And​​r​​oid VideoView清晰显示()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆