我实际上想要制作的是一个简单的视频播放器,它可以播放视频" Side by Side"(适用于虚拟现实纸板)。我使用了两个VideoView来完成此任务。但问题是,播放中存在轻微的显着差异,即;其中一个先播放然后另一个播放音频同样的事情。我只想用VideoView做到这一点。这是我的代码。
main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:weightSum="2">
<VideoView
android:id="@+id/videoL"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"/>
<VideoView
android:id="@+id/videoR"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"/>
</LinearLayout>
MainActivity.java
package com.mycompany.mycb;
import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import android.net.*;
public class MainActivity extends Activity
{
VideoView vidL;
VideoView vidR;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
vidL = (VideoView)findViewById(R.id.videoL);
vidR = (VideoView)findViewById(R.id.videoR);
String urlpath = "android.resource://" + getPackageName() + "/" + R.raw.myvideo;
vidL.setVideoURI(Uri.parse(urlpath));
vidL.start();
vidR.setVideoURI(Uri.parse(urlpath));
vidR.start();
}
}
答案 0 :(得分:0)
找到答案。 &#34; requestFocus()方法;&#34;解决了这个问题。
vidL.requestFocus();
vidR.requestFocus();
完整的工作代码
MainActivity.java
package com.mycompany.mycb;
import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import android.net.*;
public class MainActivity extends Activity
{
VideoView vidL;
VideoView vidR;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
vidL = (VideoView)findViewById(R.id.videoL);
vidR = (VideoView)findViewById(R.id.videoR);
String urlpath = "android.resource://" + getPackageName() + "/" + R.raw.myvideo;
vidL.setVideoURI(Uri.parse(urlpath));
vidL.requestFocus();
vidL.start();
vidR.setVideoURI(Uri.parse(urlpath));
vidR.requestFocus();
vidR.start();
}
}
答案 1 :(得分:0)
您需要这种解决方案。仅在两者都准备好时播放两个视频。如果您想播放远程视频,这将非常有用。
public class MainActivity extends Activity {
private VideoView vidL;
private VideoView vidR;
private boolean vidRPrepared;
private boolean vidLPrepared;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
vidL = (VideoView)findViewById(R.id.videoL);
vidR = (VideoView)findViewById(R.id.videoR);
String urlpath = "android.resource://" + getPackageName() + "/" + R.raw.myvideo;
vidL.setVideoURI(Uri.parse(urlpath));
vidR.setVideoURI(Uri.parse(urlpath));
vidL.requestFocus();
vidR.requestFocus();
initRVideoView();
initLVideoView();
}
private void initRVideoView() {
vidL.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
vidRPrepared = true;
startPlaying();
}
});
}
private void initLVideoView() {
vidR.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
vidLPrepared = true;
startPlaying();
}
});
}
private void startPlaying() {
if(vidRPrepared && vidLPrepared) {
vidL.start();
vidR.start();
}
}
}