6

示例代码:

如何在一个活动中播放两个视频

public class Two_videos extends Activity 
{
VideoView video1, video2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.two_video);

VideoView video1= (VideoView) findViewById(R.id.video1);
              video1.setVideoPath("/mnt/sdcard/Movies/com.bnb.giggle/IMG_20130415184609.mp4");
            video1.start();

VideoView video2= (VideoView) findViewById(R.id.video2);
        video2.setVideoPath("/mnt/sdcard/Movies/com.bnb.giggle/IMG_20130415184608.mp4");
        video2.start();
   }
}

不能同时播放两个视频。

4

5 回答 5

2

播放多个视频取决于硬件,但如果您的设备仅支持一个媒体播放器实例,那么您必须调用 yourVideoView.stopPlayBack() 以在另一个视频视图中播放视频。您可以使用其 resume() 方法在第一个 videoview 中恢复视频,但在从第二个停止播放之后。

于 2013-12-03T14:28:56.990 回答
2

您的设备应支持播放多个视频以及需要高端设备的视频质量(高清视频),否则会引发错误。

我尝试了此代码,它适用于所有 nexus 设备。

于 2014-02-05T05:10:54.493 回答
1

根据这个答案,很有可能同时播放多个视频,但这取决于设备及其硬件。安卓版本好像没问题。我建议您阅读他的评论和代码以便更好地理解。

于 2013-04-15T13:56:48.367 回答
0

请参见下面的代码,这里在一个活动中以不同的视频视图播放 4 个视频

main_activity.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1.0"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="ycrathi.com.multiplevideoplay.MainActivity">

    <VideoView
        android:id="@+id/v1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.25" />

    <VideoView
        android:id="@+id/v2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.25" />

    <VideoView
        android:id="@+id/v3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.25" />

    <VideoView
        android:id="@+id/v4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.25" />


</LinearLayout>

MainActivity.java

    package ycrathi.com.multiplevideoplay;

import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.VideoView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        VideoView v1 = (VideoView) findViewById(R.id.v1);
        VideoView v2 = (VideoView) findViewById(R.id.v2);
        VideoView v3 = (VideoView) findViewById(R.id.v3);
        VideoView v4 = (VideoView) findViewById(R.id.v4);
        v1.setVideoURI(Uri.parse("http://192.168.1.1/Video/vid5.mp4"));
        v1.start();
        v1.requestFocus();
        v1.setKeepScreenOn(true);

        v2.setVideoURI(Uri.parse("http://192.168.1.1/Video/vid5.mp4"));
        v2.start();
        v2.requestFocus();
        v2.setKeepScreenOn(true);

        v3.setVideoURI(Uri.parse("http://192.168.1.1/Video/vid5.mp4"));
        v3.start();
        v3.requestFocus();
        v3.setKeepScreenOn(true);

        v4.setVideoURI(Uri.parse("http://192.168.1.1/Video/vid5.mp4"));
        v4.start();
        v4.requestFocus();
        v4.setKeepScreenOn(true);

    }
}

AndroidMenifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="ycrathi.com.multiplevideoplay">
<uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
于 2016-05-09T04:54:26.060 回答
-1

这是在一项活动中显示多个视频的完整答案。在所有设备中工作。我使用 Surfaceview 和 Textureview 播放视频。

活动:

 public class ReactionViewActivity extends AppCompatActivity   {
    private static final String TAG = ReactionViewActivity.class.getSimpleName();

    private String mainVideo = "";
    private ProgressBar progress_bar;

    private SurfaceView mSurfaceView;
    private TextureView mTextureView;
    private MediaPlayer mMediaPlayer, mMediaPlayer1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reaction_view);
        initView();
    }

    private void initView() {
        progress_bar = findViewById(R.id.progress_bar);
        mSurfaceView = findViewById(R.id.surface_view);
        mTextureView = findViewById(R.id.textureView);

        textureParams = (FrameLayout.LayoutParams) mTextureView.getLayoutParams();
        surfaceParams = (FrameLayout.LayoutParams) mSurfaceView.getLayoutParams();

        final String video = getIntent().getStringExtra("video");
        mTextureView.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
            @Override
            public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int i, int i1) {
                Surface surface = new Surface(surfaceTexture);
                try {
                    mMediaPlayer1 = new MediaPlayer();
                    mMediaPlayer1.setDataSource(video);
                    mMediaPlayer1.setSurface(surface);
                    mMediaPlayer1.setLooping(true);
                    mMediaPlayer1.prepareAsync();

                    // Play video when the media source is ready for playback.
                    mMediaPlayer1.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                        @Override
                        public void onPrepared(MediaPlayer mediaPlayer) {
                            progress_bar.setVisibility(View.GONE);
                            mediaPlayer.start();
                        }
                    });
                    mMediaPlayer1.setAudioStreamType(AudioManager.STREAM_MUSIC);
                } catch (IllegalArgumentException e) {
                    Log.d(TAG, e.getMessage());
                } catch (SecurityException e) {
                    Log.d(TAG, e.getMessage());
                } catch (IllegalStateException e) {
                    Log.d(TAG, e.getMessage());
                } catch (IOException e) {
                    Log.d(TAG, e.getMessage());
                }
            }

            @Override
            public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int i, int i1) {

            }

            @Override
            public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
                return false;
            }

            @Override
            public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {

            }
        });

        if (Objects.requireNonNull(getIntent().getExtras()).containsKey("mainVideo")) {
            mainVideo = getIntent().getStringExtra("mainVideo");
            mSurfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
                @Override
                public void surfaceCreated(SurfaceHolder surfaceHolder) {
                    mMediaPlayer = new MediaPlayer();
                    mMediaPlayer.setDisplay(surfaceHolder);
                    try {
                        mMediaPlayer.setDataSource(mainVideo);
                        mMediaPlayer.setLooping(true);
                        mMediaPlayer.prepare();
                        mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                            @Override
                            public void onPrepared(MediaPlayer mediaPlayer) {
                                mMediaPlayer.start();
                            }
                        });
                        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {

                }

                @Override
                public void surfaceDestroyed(SurfaceHolder surfaceHolder) {

                }
            });
        }

    }

    @Override
    public void onBackPressed() {
        releaseMediaPlayer();
        finish();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        releaseMediaPlayer();
    }

    private void releaseMediaPlayer() {
        if (mMediaPlayer != null) {
            mMediaPlayer.release();
            mMediaPlayer = null;
        }

        if (mMediaPlayer1 != null) {
            mMediaPlayer1.release();
            mMediaPlayer1 = null;
        }
    }
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".ReactionViewActivity">

   <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/black"
        android:layout_below="@+id/rlTopbar">

        <TextureView
            android:id="@+id/textureView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center" />

        <SurfaceView
            android:id="@+id/surface_view"
            android:layout_width="110dp"
            android:layout_height="140dp"
            android:layout_gravity="right"
            android:layout_marginRight="@dimen/margin_20"
            android:layout_marginTop="@dimen/margin_20" />

        <ProgressBar
            android:id="@+id/progress_bar"
            android:layout_width="@dimen/margin_50"
            android:layout_height="@dimen/margin_50"
            android:layout_gravity="center"
            android:indeterminateDrawable="@drawable/custom_progress" />

    </FrameLayout>
</RelativeLayout>
于 2018-05-29T12:13:34.693 回答