如何从服务器在 ViewPager 内的 VideoView 上播放视频

2024-01-16

我尝试开发一个应用程序从服务器检索视频并在 viewpager 内的 videoview 上播放,原始文件夹中的视频工作正常,但有两个问题:

  • 1:部分视频无法播放。或黑色活动显示。
  • 2:页面滚动时视频不停止。

那么如何使用 URL 而不是 `

android.resource://mypackagename/video1.mp4

以及当 PageIsChanged 时如何暂停/停止。任何教程或任何点击我都会感激不尽。

这是我的代码源代码 http://topappstopgames.com/projects/ViewPagerVideoFromServer.zip

ViewPagerAdapter.java

import android.content.Context;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.Uri;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.VideoView;

public class ViewPagerAdapter extends PagerAdapter {

    Context context;
    String[] rank;
    String[] country;
    String[] population;
    int[] flag;
    LayoutInflater inflater;
    static int[] arrayvid;
    private VideoView mVideoView;

    public ViewPagerAdapter(Context context, String[] rank, String[] country,
            String[] population, int[] flag, int[] arrayvid) {
        this.context = context;
        this.rank = rank;
        this.country = country;
        this.population = population;
        this.flag = flag;
        this.arrayvid = arrayvid;
    }

    @Override
    public int getCount() {
        return rank.length;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == ((LinearLayout) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {

        // Declare Variables
        TextView txtrank;
        TextView txtcountry;
        TextView txtpopulation;
        ImageView imgflag;

        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View itemView = inflater.inflate(R.layout.viewpager_item, container,
                false);

        // Locate the TextViews in viewpager_item.xml
        txtrank = (TextView) itemView.findViewById(R.id.rank);
        txtcountry = (TextView) itemView.findViewById(R.id.country);
        txtpopulation = (TextView) itemView.findViewById(R.id.population);
        imgflag = (ImageView) itemView.findViewById(R.id.flag);
        mVideoView = (VideoView) itemView.findViewById(R.id.VVExe);

        txtrank.setText(rank[position]);
        txtcountry.setText(country[position]);
        txtpopulation.setText(population[position]);
        imgflag.setImageResource(flag[position]);

        mVideoView.setOnPreparedListener(new OnPreparedListener() {

            @Override
            public void onPrepared(MediaPlayer mp) {
                mp.setLooping(true);
            }
        });

        MediaController mediaController = new MediaController(context, false);
        mediaController.setAnchorView(mVideoView);
        mVideoView.setMediaController(mediaController);
        ((ViewPager) container).addView(itemView);
        return itemView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        // Remove viewpager_item.xml from ViewPager
        ((ViewPager) container).removeView((LinearLayout) object);

    }

    public void pausevideo() {
        mVideoView.stopPlayback();

    }

    public void play(int position) {
        mVideoView.setVideoURI(Uri
                .parse("android.resource://mypackagename/"
                        + arrayvid[position]));
        mVideoView.requestFocus();
        mVideoView.start();

    }
}

MainActivity.java

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.widget.ImageButton;

public class MainActivity extends Activity {

    // Declare Variables
    ViewPager viewPager;
    PagerAdapter adapter;
    String[] rank;
    private ImageButton play;
    String[] country;
    String[] population;
    int[] flag;
    public int position;
    int[] arrayvid;
    boolean isRunning = true;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Get the view from viewpager_main.xml
        setContentView(R.layout.viewpager_main);
        play = (ImageButton) findViewById(R.id.btnPlayAB);
        // Generate sample data
        rank = new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };

        country = new String[] { "China", "India", "United States",
                "Indonesia", "Brazil", "Pakistan", "Nigeria", "Bangladesh",
                "Russia", "Japan" };

        population = new String[] { "1,354,040,000", "1,210,193,422",
                "315,761,000", "237,641,326", "193,946,886", "182,912,000",
                "170,901,000", "152,518,015", "143,369,806", "127,360,000" };

        flag = new int[] { R.drawable.china, R.drawable.india,
                R.drawable.unitedstates, R.drawable.indonesia,
                R.drawable.brazil, R.drawable.pakistan, R.drawable.nigeria,
                R.drawable.bangladesh, R.drawable.russia, R.drawable.japan };

        arrayvid = new int[] { R.raw.basiccrunch, R.raw.bicyclecrunch,
                R.raw.reversecrunch, R.raw.longarmcrunch,
                R.raw.crossovercrunch, R.raw.rightobliquecrunch,
                R.raw.leftobliquecrunch, R.raw.halfcurl,
                R.raw.verticallegcrunch, R.raw.plank };

        // Locate the ViewPager in viewpager_main.xml
        viewPager = (ViewPager) findViewById(R.id.pager);
        // Pass results to ViewPagerAdapter Class
        adapter = new ViewPagerAdapter(MainActivity.this, rank, country,
                population, flag, arrayvid);
        // Binds the Adapter to the ViewPager
        viewPager.setAdapter(adapter);

        play.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                if (isRunning) {
                    ((ViewPagerAdapter) adapter).play(position);
                    isRunning = false;
                    play.setBackgroundResource(R.drawable.pausee);
                } else {
                    ((ViewPagerAdapter) adapter).pausevideo();
                    isRunning = true;
                    play.setBackgroundResource(R.drawable.playy);
                }

            }
        });

    }

}

viewpager_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:orientation="vertical"
    android:padding="10dp"
    android:weightSum="10" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1.6"
        android:orientation="horizontal" >

        <LinearLayout
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:layout_weight="3"
            android:orientation="vertical" >

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <TextView
                    android:id="@+id/ranklabel"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/ranklabel" />

                <TextView
                    android:id="@+id/rank"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <TextView
                    android:id="@+id/countrylabel"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/countrylabel" />

                <TextView
                    android:id="@+id/country"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <TextView
                    android:id="@+id/populationlabel"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/populationlabel" />

                <TextView
                    android:id="@+id/population"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
            </LinearLayout>
        </LinearLayout>

        <ImageView
            android:id="@+id/flag"
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:background="#000000"
            android:padding="1dp" />
    </LinearLayout>

    <VideoView
        android:id="@+id/VVExe"
        android:layout_width="wrap_content"
        android:layout_height="0dip"
        android:layout_marginTop="5dp"
        android:layout_weight="7.9"
        android:gravity="center" />

</LinearLayout>

viewpager_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/LinearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:weightSum="10" >

        <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_width="wrap_content"
            android:layout_height="0dip"
            android:layout_weight="8.6" />

<LinearLayout
    android:layout_width="fill_parent"
      android:layout_height="0dip"
        android:layout_weight="1.4"
    android:gravity="center"
    android:padding="10dp" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:background="#696969"
        android:gravity="center"
        android:orientation="horizontal"
        android:weightSum="10" >

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1" >
        </LinearLayout>

        <ImageButton
            android:id="@+id/btnprevAB"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:background="@drawable/prevsel" />

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1" >
        </LinearLayout>

        <ImageButton
            android:id="@+id/btnPlayAB"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:background="@drawable/playsel" />

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1" >
        </LinearLayout>

        <ImageButton
            android:id="@+id/btnNextAB"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:background="@drawable/nextsel" />

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1" >
        </LinearLayout>
    </LinearLayout>

    </LinearLayout>

我认为最好使用FragmentStatePagerAdapter代替PagerAdapter。在这种情况下你不需要OnPageChangeListener如果再多了,您可以使用片段生命周期回调方法,例如onResume and onPause播放或暂停您的视频。

以及当 PageIsChanged 时如何暂停/停止。任何教程或任何点击我 会为此感激不已。

您可以使用下面的链接来了解如何使用此适配器,然后您可以借助片段生命周期方法创建自己的逻辑。

Android FragmentStatePagerAdapter 示例 http://www.truiton.com/2013/05/android-fragmentstatepageradapter-example/

FragmentStatePagerAdapter http://developer.android.com/reference/android/support/v4/app/FragmentStatePagerAdapter.html

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何从服务器在 ViewPager 内的 VideoView 上播放视频 的相关文章

随机推荐

  • 如何获取scrapy失败的URL?

    我是 scrapy 的新手 这是我所知道的令人惊叹的爬虫框架 在我的项目中 我发送了超过 90 000 个请求 但其中有一些失败了 我将日志级别设置为INFO 我只能看到一些统计信息 但看不到详细信息 2012 12 05 21 03 04
  • 如何在Android国际象棋游戏中制作动画

    我正在为 Android 开发国际象棋游戏 androidchess appspot com http androidchess appspot com 如果我想添加动画 我应该使用扩展Canvas的自定义View 我现在这样做 还是扩展S
  • 不小心创造了病毒?

    我经常看到这种情况发生 我用 Delphi 编写一个应用程序 当我编译它时 病毒扫描程序告诉我我已经创建了一个病毒 然后立即再次删除了可执行文件 这很烦人 但通过完全重建 首先删除 dcu 文件 有时只需等待 即可轻松修复 据我所知 Del
  • 类型“[String,AnyObject?]”不符合协议 AnyObject?:为什么?

    我试图理解为什么会出现编译错误 类型 String AnyObject 不符合 AnyObject 协议 var cars String AnyObject model Ferrari var JSON String AnyObject c
  • 在 Python 中使用带有线程的全局字典

    访问 更改字典值是线程安全的吗 我有一本全球词典foo以及带有 id 的多个线程id1 id2 idn 是否可以访问和更改foo如果已知每个线程仅使用其 id 相关值 例如线程 则无需为其分配锁id1只会与foo id1 假设 CPytho
  • 再次休息并获取...

    一般来说 REST 社区似乎不喜欢 GET 请求中的复杂数据 我想知道这背后是否有一个好的原则 或者只是具体化了 GET 字典的 任意 url 长度 限制 我对 url 和资源之间的对应关系感到满意 但为什么我的 GET 请求不能在请求正文
  • Android Kudan - 扩展 ARActivity 将停止系统相机手电筒的工作(闪光灯)

    我正在开发 kudan SDK 用于使用标记构建 3D 模型增强现实对象 所有这些都工作得很好 但是当我在同一个 ARActivity 上构建相机手电筒时 闪光灯将停止工作 甚至它会停止系统闪光灯手电筒的工作 开 关手电筒 来自系统小部件
  • 从 Neo4j 删除所有节点时出现 ConstraintViolationTransactionFailureException

    当尝试从 Neo4j 图形数据库中删除所有节点时 我过去已经在较小的数据集上成功完成过多次 我一直遇到Error undefined undefined运行此查询后 MATCH n DETACH DELETE n 我认为我尝试一次删除的节点
  • 使用什么对称密码来加密消息?

    我对加密一无所知 但我需要它 如何 假设您有一个节点系统 节点通过异步消息在网络上相互通信 节点不维护有关其他节点的会话信息 这是设计限制 假设您想确保只有您的节点可以读取正在发送的消息 我相信加密是解决这个问题的方法 由于节点不维护会话
  • 终端上的“快速构建”抛出“错误:找不到根清单”

    我想在终端上运行我的快速编程 所以我cd我的项目的根文件夹 然后运行 swift build 但是出现了错误 error root manifest not found有什么帮助吗 如果其他人偶然发现同样的问题 我的解决方案是 cd 到我的
  • 在 Windows 上运行 Django 时出现“WinError 10013”

    自从我遇到这个问题以来已经快一个月了 我非常感谢您的帮助 尝试登录我的 Django Web 应用程序时 我在 accounts login 处遇到 OSError 我能够登录 127 0 0 1 8000 admin 但不能登录 acco
  • findDOMNode 与 getElementById 对于普通 DOM 元素

    我不太确定这个问题有真正的答案 但我想知道是否最好使用以下命令在 React 应用程序中查找常规 DOM 元素 A refs 和 ReactDOM findDOMNode or b 普通旧 document getElementById 我
  • 导入而不执行类 - python

    我的问题是我有一个包含类的文件 并且在这个类内有一堆代码将被执行 所以每当我导入该文件时它就会执行 无需创建该类的对象 这是例子 FILE X class d def init self print print this will NOT
  • Gradle 和 Android 支持库

    几乎在每个 Android 应用程序中 我们都需要一些库项目 例如 ABS HoloEverywhere 等 其中大部分都在 Maven Central 中 这很好 不好的是 它们中的大多数都依赖于支持库 并且自然地指向 Maven Cen
  • 在双变量中获取数字的问题

    我的 java 程序中需要的函数出现了一些问题 我想检查 双精度 变量的总位数 例如 5 应该返回 1 5 0034 应该返回 5 2 04 应该返回 3 我的函数是这样的 private int getDoubleLength doubl
  • WPF Datagrid 绑定自定义列标题

    我试图弄清楚如何使用 MVVM 模式将 WPF DataGrid 的列标题和主要数据绑定到数据源 我正在寻找的结果如下所示 source vallelunga com http brian vallelunga com files data
  • 浮点按位运算的用处

    我注意到浮点存在 SSE 指令 这让我想知道 您可以对 fp integer union 中的标量执行相同的操作 我突然想到 如果对浮点数数组的各个分量进行按位或运算 则可以通过查看结果的符号位来快速确定它们中是否有任何一个为负数 浮点值的
  • 获取 TWIG 模板中的控制器名称

    我正在学习 symfony2 3 当我尝试在 twig 模板中获取控制器名称时出现错误 控制器 namespace Acme AdminBundle Controller use Symfony Bundle FrameworkBundle
  • Mac OS-X Mountain Lion 上的 GCC-4.2 错误,无法使用 pip / virtualenv 安装某些软件包

    我看到一个非常烦人的错误 我真的不知道如何处理 这似乎很常见 我几乎尝试了所有能找到的解决方案 但都无济于事 我正在尝试使用 pip 安装库 gevent psycopg2 和 greenlet 都遇到过这个问题 问题似乎是我的计算机找不到
  • 如何从服务器在 ViewPager 内的 VideoView 上播放视频

    我尝试开发一个应用程序从服务器检索视频并在 viewpager 内的 videoview 上播放 原始文件夹中的视频工作正常 但有两个问题 1 部分视频无法播放 或黑色活动显示 2 页面滚动时视频不停止 那么如何使用 URL 而不是 and