【问题标题】:How play video on VideoView inside ViewPager from server如何从服务器在 ViewPager 内的 VideoView 上播放视频
【发布时间】:2015-05-10 12:34:18
【问题描述】:

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

  • 1:某些视频没有播放。或黑色活动表演。
  • 2:页面滚动时视频不停止。

那么如何使用 URL 而不是 `

android.resource://mypackagename/video1.mp4

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

我的代码是Source Code

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>

【问题讨论】:

    标签: android android-viewpager viewpage


    【解决方案1】:

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

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

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

    Android FragmentStatePagerAdapter Example

    FragmentStatePagerAdapter

    【讨论】:

      【解决方案2】:

      我写了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
      }
      

      examples 中,您可以在一个简单的Activity 中找到如何使用它。

      阅读我的blog post 了解FrameVideoView

      【讨论】:

      • @Attaullah 你这是什么意思?
      • 示例链接已失效
      【解决方案3】:
      1. 要检测ViewPager 是否是滚动/更改页面,您只需将OnPageChangeListener 附加到您的ViewPager

      2. 从远程 URL 播放视频,您可以使用标准 MediaController/MediaPlayer。看看this tutorial。

      【讨论】:

      【解决方案4】:
       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) {
      
                  }
              });
      

      【讨论】:

        【解决方案5】:

        您必须从 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);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多