29

我尝试开发一个从服务器检索视频并在 viewpager 内的 videoview 上播放的应用程序,来自 raw 文件夹的视频工作正常,但它们是 2 个问题:

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

那么如何使用 URL 而不是 `

android.resource://mypackagename/video1.mp4

以及如何在 PageIsChanged 时暂停/停止。任何教程或任何点击我都会感谢的。

这里我的代码是源代码

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>

在此处输入图像描述

4

5 回答 5

9

我认为最好使用FragmentStatePagerAdapter而不是PagerAdapter. 在这种情况下,您不再需要OnPageChangeListener,您可以使用片段生命周期回调方法,例如onResumeonPause来播放或暂停您的视频。

以及如何在 PageIsChanged 时暂停/停止。任何教程或任何点击我都会感谢的。

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

Android FragmentStatePagerAdapter 示例

FragmentStatePagerAdapter

于 2015-05-15T12:04:40.447 回答
3

我编写了FrameViedoView,它提高了播放视频的性能。

它也适用于ViewPager.

如何使用它?

添加http://bright.github.io/maven-repo/到您的存储库:

repositories {
    maven {
        url "http://bright.github.io/maven-repo/"
    }
}

然后在模块内声明一个依赖项:

dependencies {
    compile('mateuszklimek.framevideoview:framevideoview:1.1.0@aar')
    //other dependencies
}

示例中,您可以找到如何在简单的Activity.

阅读我关于FrameVideoView.

于 2015-05-20T07:19:07.540 回答
1
  1. 要检测是否ViewPager正在滚动/更改页面,您只需将一个附加OnPageChangeListener到您的ViewPager.

  2. 要从远程 URL 播放视频,您可以使用标准MediaController/ MediaPlayer。看看这个教程l。

于 2015-05-13T12:44:51.840 回答
1
 viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

           //do the needful action

            }

            @Override
            public void onPageSelected(int position) {
                System.out.println("selected " + position);
   //do the needful action
                }

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
于 2015-05-19T12:59:29.880 回答
0

您必须从 OnPageChangeListener 访问您的视频。必须这样做,你必须:

view.setTag("view"+position);在你的public Object instantiateItem(ViewGroup container, int position)

在你可以从 OnPageChangeListener 取回它之后:

View viewTag = viewPager.findViewWithTag("view" + viewPager.getCurrentItem());

接着

VideoView videoViewTag = (VideoView)viewTag.findViewById(R.id.videoView);
于 2017-02-19T10:14:08.340 回答