我的问题是,在Android中,一个视频视图应该在整个屏幕上播放视频而另一个视频视图应该在屏幕的角落播放另一个小尺寸的视频。 为此,它需要两个视频视图同时播放视频。有可能在android中实现吗?
正如我所见,我们可以在下面的链接中同时播放两个视频。但是这里两个视频视图没有重叠。 How to play multiple video files simultaneously in one layout side by side in different view in Android
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/parentLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<VideoView
android:id="@+id/videoView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<VideoView
android:id="@+id/videoView1"
android:layout_width="300dp"
android:layout_height="200dp"
android:layout_centerInParent="true"/>
答案 0 :(得分:4)
是的,可以同时播放多个视频。您可以使用FrameLayout或RelativeLayout来放置VideoView。
示例源代码(示例使用资产文件夹中的视频文件)
activity_video_player.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<VideoView
android:layout_weight="1"
android:id="@+id/videoview2"
android:layout_width="200dp"
android:layout_height="150dp"/>
<VideoView
android:id="@+id/videoview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</FrameLayout>
VideoPlayerActivity.java
package com.mobiactivation.pop.activity;
import android.app.Activity;
import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.graphics.PixelFormat;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.PowerManager;
import android.view.SurfaceHolder;
import android.view.Window;
import android.view.WindowManager;
import android.widget.VideoView;
import com.mobiactivation.pop.R;
public class VideoPlayerActivity extends Activity implements SurfaceHolder.Callback
{
VideoView mVideoView = null;
VideoView mVideoView2 = null;
MediaPlayer mediaPlayer;
MediaPlayer mediaPlayer2;
SurfaceHolder surfaceHolder;
SurfaceHolder surfaceHolder2;
protected PowerManager.WakeLock mWakeLock;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_video_player);
getWindow().setFormat(PixelFormat.RGB_888);
mVideoView = (VideoView) findViewById(R.id.videoview);
mVideoView2 = (VideoView) findViewById(R.id.videoview2);
surfaceHolder = mVideoView.getHolder();
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
surfaceHolder.addCallback(this);
surfaceHolder2 = mVideoView2.getHolder();
surfaceHolder2.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
surfaceHolder2.addCallback(this);
final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
this.mWakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "VIDEO");
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
{
}
@Override
public void surfaceCreated(SurfaceHolder holder)
{
if (holder == surfaceHolder)
play(getIntent().getStringExtra("path"));
else
play2(getIntent().getStringExtra("path"));
}
@Override
public void surfaceDestroyed(SurfaceHolder holder)
{
mediaPlayer.release();
mWakeLock.release();
}
void play(String name)
{
mediaPlayer = new MediaPlayer();
mediaPlayer.setDisplay(surfaceHolder);
AssetFileDescriptor afd;
try
{
afd = getAssets().openFd(name);
mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
mediaPlayer.prepareAsync();
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp)
{
mp.start();
}
});
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer)
{
finish();
}
});
this.mWakeLock.acquire();
} catch (Exception e)
{
e.printStackTrace();
}
}
void play2(String name)
{
mediaPlayer2 = new MediaPlayer();
mediaPlayer2.setDisplay(surfaceHolder2);
AssetFileDescriptor afd;
try
{
afd = getAssets().openFd(name);
mediaPlayer2.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
mediaPlayer2.prepareAsync();
mediaPlayer2.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp)
{
mp.start();
}
});
mediaPlayer2.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer)
{
finish();
}
});
this.mWakeLock.acquire();
} catch (Exception e)
{
e.printStackTrace();
}
}
}