1

我的项目中有一些阻塞问题。我有一些视频链接,我想在 videoView 中看到这些视频。并通过滑动到下一个视图转到下一部电影。(就像在画廊中一样)第一个视频播放正常,但是当我转到第二个视频时,我发生了一些错误。但是下一个(第三个)视频(在破坏第一个视频之后)效果很好。下一个视频也出现错误。你能帮我解决它吗?

页面适配器

public class SingleVideoPagerAdapter extends PagerAdapter
{
    static final String DBG_TAG = "SingleNewsPagerAdapter";

    private ArrayList<ListNews> data;
    private VideoView videoView;
    private VideoView videoView2;
    private VideoView videoView3;
    private Context ctx;
    private MediaController mediaController;
    private MediaController mediaController2;
    private MediaController mediaController3;

    VideoHelper v1helper;
    VideoHelper v2helper;
    VideoHelper v3helper;

    private class VideoHelper
    {
        boolean isVideofree;
        int videoPosition;
        String uriToVideo;

        public VideoHelper()
        {
            this.isVideofree = true;
            this.videoPosition = -1;
            this.uriToVideo = null;
        }
    }

    public SingleNewsPagerAdapter(ArrayList<ListNews> d, Context c)
    {
        this.data = d;
        this.ctx = c;
        v1helper = new VideoHelper();
        v2helper = new VideoHelper();
        v3helper = new VideoHelper();
    }

    @Override
    public int getCount()
    {
        return Integer.MAX_VALUE;
    }

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

    @Override
    public Object instantiateItem(View collection, int position)
    {
        Log.d(DBG_TAG, "adding" + position);
        LayoutInflater inflater = (LayoutInflater) collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        // View v = null;

        View page = inflater.inflate(pl.orange.OrangeWorld.UI.R.layout.single_news_page, null);

        this.videoLayout = (RelativeLayout) page.findViewById(R.id.singleNewsVideo);
        this.videoProgressBar = (ProgressBar) videoLayout.findViewById(R.id.videoProgressBar);

        videoView = (VideoView) videoLayout.findViewById(R.id.videoView);
        videoView2 = (VideoView) videoLayout.findViewById(R.id.videoView2);
        videoView3 = (VideoView) videoLayout.findViewById(R.id.videoView3);

        videoView.setOnPreparedListener(setOnPrepareListener1);

        this.TVTitle = (TextView) page.findViewById(R.id.singleNewsTitle);
        TVTitle.setText(data.get(position).getTitle());


        playVideo(page, urlVideo, position);


        ((ViewPager) collection).addView(page, 0);
        return page;
    }

    private OnPreparedListener setOnPrepareListener1 = new OnPreparedListener()
    {
        @Override
        public void onPrepared(MediaPlayer arg0)
        {
            videoProgressBar.setVisibility(View.GONE);
            videoView.start();
            Toast.makeText(ctx, "setOnPreparedListener1", Toast.LENGTH_SHORT).show();
        }
    };
    @Override
    public void destroyItem(View collection, int position, Object view)
    {
        Log.d(DBG_TAG, "destroying" + position);
        if (position == v1helper.videoPosition)
        {
            v1helper.isVideofree = true;
        }
        if (position == v2helper.videoPosition)
        {
            v2helper.isVideofree = true;
        }
        if (position == v3helper.videoPosition)
        {
            v3helper.isVideofree = true;
        }
        ((ViewPager) collection).removeView((View) view);
    }

    private void playVideo(View view, String videoUri, int position)
    {

        videoLayout.setVisibility(View.VISIBLE);

        if (v1helper.isVideofree)
        {
            Uri video = Uri.parse(videoUri);
            mediaController = new MediaController(ctx, false);
            mediaController.setAnchorView(videoView);

            videoView.setMediaController(mediaController);
            videoView.setVideoURI(video);
            videoView.setKeepScreenOn(true);
            videoView.requestFocus();
            v1helper.isVideofree = false;
            v1helper.videoPosition = position;
        }
        else if (v2helper.isVideofree)
        {
           videoView2.setVisibility(View.VISIBLE);

            Uri video = Uri.parse(videoUri);
            mediaController2 = new MediaController(ctx, false);
            mediaController2.setAnchorView(videoView2);

            videoView2.setMediaController(mediaController2);
            videoView2.setVideoURI(video);
            videoView2.setKeepScreenOn(true);
            videoView2.requestFocus();
            v2helper.isVideofree = false;
            v2helper.videoPosition = position;

        }
        else if (v3helper.isVideofree)
        {
            videoView3.setVisibility(View.VISIBLE);

            Uri video = Uri.parse(videoUri);
            mediaController3 = new MediaController(ctx, false);
            mediaController3.setAnchorView(videoView3);

            videoView3.setMediaController(mediaController3);
            videoView3.setVideoURI(video);
        videoView3.setKeepScreenOn(true);
            videoView3.requestFocus();
            v3helper.isVideofree = false;
            v3helper.videoPosition = position;
        }

        videoView.setOnErrorListener(new OnErrorListener()
        {

            @Override
            public boolean onError(MediaPlayer mp, int what, int extra)
            {
                Toast.makeText(ctx, "Error occured1", Toast.LENGTH_SHORT).show();
                return false;
            }
        });

        videoView.setOnCompletionListener(new OnCompletionListener()
        {

            @Override
            public void onCompletion(MediaPlayer mp)
            {
                videoView.stopPlayback();
            }
        });

        videoView2.setOnErrorListener(new OnErrorListener()
        {

        @Override
        public boolean onError(MediaPlayer mp, int what, int extra)
        {
            Toast.makeText(ctx, "Error occured2", Toast.LENGTH_SHORT).show();
            return false;
        }
    });

        videoView2.setOnCompletionListener(new OnCompletionListener()
        {

            @Override
            public void onCompletion(MediaPlayer mp)
            {
                videoView2.stopPlayback();
            }
        });

        videoView3.setOnErrorListener(new OnErrorListener()
        {
            @Override
            public boolean onError(MediaPlayer mp, int what, int extra)
            {
                Toast.makeText(ctx, "Error occured2", Toast.LENGTH_SHORT).show();
                return false;
            }
        });

        videoView3.setOnCompletionListener(new OnCompletionListener()
        {

            @Override
            public void onCompletion(MediaPlayer mp)
            {
                videoView3.stopPlayback();
            }
        });

    }

    public void onPageIsChanged(int position)
    {
        if (position == v1helper.videoPosition && !v1helper.isVideofree)
        {
        videoView2.stopPlayback();
            videoView3.stopPlayback();
            videoView.requestFocus();
            videoView.start();            
        }
        if (position == v2helper.videoPosition && !v2helper.isVideofree)
        {
            videoView.stopPlayback();
            videoView3.stopPlayback();

            videoView2.setVisibility(View.VISIBLE);
            videoView2.requestFocus();
            videoView2.start();            
        }
        if (position == v3helper.videoPosition && !v3helper.isVideofree)
        {
            videoView.stopPlayback();
            videoView2.stopPlayback();

            videoView3.setVisibility(View.VISIBLE);
            videoView3.requestFocus();
            videoView3.start();            
        }
    }

}

看法:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/singleNewsTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Title"
        android:textColor="#cdcdcd"
        android:textSize="22sp"
        android:textStyle="bold"
        android:typeface="sans" />

    <RelativeLayout
        android:id="@+id/singleNewsVideo"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/singleNewsTitle"
        android:visibility="gone" >

        <VideoView
            android:id="@+id/videoView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_gravity="center"
            android:visibility="visible" />

        <VideoView
            android:id="@+id/videoView2"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_gravity="center"
            android:visibility="visible" />

        <VideoView
            android:id="@+id/videoView3"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_gravity="center"
            android:visibility="visible" />

        <ProgressBar
            android:id="@+id/videoProgressBar"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:visibility="gone" />
    </RelativeLayout>
</RelativeLayout>

</RelativeLayout>

活动:

public class SingleNewsActivity extends SherlockActivity
{
    static final String DBG_TAG = "SingleNewsActivity";

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        getWindow().setFormat(PixelFormat.TRANSLUCENT);
        setContentView(R.layout.single_news);

        ViewPager mPager = (ViewPager) findViewById(R.id.pager);
        ArrayList<ListNews> urlList = new ArrayList<ListNews>();

        Bundle extras = getIntent().getExtras();
        if (extras == null)
        {
            Log.d(DBG_TAG, "there is no extras in the intent");
            return;
        }
        // Get data via the key
        urlList = extras.getParcelableArrayList("listNews");

        int position = extras.getInt("position");
        Log.d(DBG_TAG, String.format("listNewsURL: %s", urlList.get(position)));

        final SingleNewsPagerAdapter mAdapter = new SingleNewsPagerAdapter(urlList, this);
        mPager.setAdapter(mAdapter);
        mPager.setCurrentItem(position);

        mPager.setOnPageChangeListener(new OnPageChangeListener()
        {

            @Override
            public void onPageSelected(int position)
            {
                mAdapter.onPageIsChanged(position);  
            }

            @Override
            public void onPageScrollStateChanged(int state)
            {
                // TODO Auto-generated method stub
            }

            @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
            {
                // TODO Auto-generated method stub
            }
        });
    }
    }

单击并加载 firstViedo 后从 logCat 登录:

06-03 15:30:43.909: I/ListNewsActivity(3999): onScroll():6total: 22
06-03 15:30:44.059: D/ListNewsActivity(3999): onItemClick position4
06-03 15:30:44.109: D/SingleNewsActivity(3999): listNewsURL: pl.orange.OrangeWorld.Data.ListNews@407442f0
06-03 15:30:44.129: D/SingleNewsPagerAdapter(3999): adding3
06-03 15:30:44.139: W/SingleNewsPagerAdapter(3999): SET VIDEOVIEW 1 at position: 3
06-03 15:30:44.149: D/SingleNewsPagerAdapter(3999): adding2
06-03 15:30:44.169: D/SingleNewsPagerAdapter(3999): adding4
06-03 15:30:44.179: W/SingleNewsPagerAdapter(3999): SET VIDEOVIEW 2 at position: 4
06-03 15:30:44.229: W/KeyCharacterMap(3999): No keyboard for id -1
06-03 15:30:44.229: W/KeyCharacterMap(3999): Using default keymap
 06-03 15:30:44.259: I/MediaPlayer(3999): uri is:
 06-03 15:30:44.259: I/MediaPlayer(3999): path is null
 06-03 15:30:44.259: D/MediaPlayer(3999): Couldn't open file on client side, trying server side
 06-03 15:30:46.939: I/MediaPlayer(3999): uri is:
 06-03 15:30:46.939: I/MediaPlayer(3999): path is null
 06-03 15:30:46.939: D/MediaPlayer(3999): Couldn't open file on client side, trying server side
 06-03 15:30:47.789: E/MediaPlayer(3999): error (1, -2147483648)
 06-03 15:30:47.789: E/MediaPlayer(3999): Error (1,-2147483648)
 06-03 15:30:47.789: D/VideoView(3999): Error: 1,-2147483648
 06-03 15:42:14.989: E/SingleNewsPagerAdapter(4769): mp = android.media.MediaPlayer@40733a20 what=1, extra=-2147483648
4

0 回答 0