Ну, друг мой, во-первых, вы не можете установить фон для вашего VideoView
и заставить его играть в фоновом режиме на экране.
Пожалуйста, следуйте моим шагам и добавьте свои усилия, и вы должны быть там.
Удалите видео из папки для рисования и добавьте его в папку «Raw».Пожалуйста, Google, как создать сырую папку.Это просто, хотя.И поместите ваш видео файл внутри него.
Прежде всего, создайте SurfaceView
в своем xml, как это.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/home_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<SurfaceView
android:id="@+id/surface"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="10dip" />
</Framelayout>
Теперь создайте класс, подобный приведенному ниже, который может реализовывать SurfaceView
,
public class YourMovieActivity extends Activity implements SurfaceHolder.Callback {
private MediaPlayer mp = null;
//...
SurfaceView mSurfaceView=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mp = new MediaPlayer();
mSurfaceView = (SurfaceView) findViewById(R.id.surface);
mSurfaceView.getHolder().addCallback(this);
//...
}
}
Теперь ваш класс попросит добавить нереализованные методы.Добавьте эти методы, просто нажав «Добавить невыполненные методы»
Теперь вы сможете увидеть автоматически сгенерированный метод, подобный этому,
@Override
public void surfaceCreated(SurfaceHolder holder) {
}
И внутри этого метода добавьте следующий код,
@Override
public void surfaceCreated(SurfaceHolder holder) {
Uri video = Uri.parse("android.resource://" + getPackageName() + "/"
+ R.raw.your_raw_file);
mp.setDataSource(video);
mp.prepare();
//Get the dimensions of the video
int videoWidth = mp.getVideoWidth();
int videoHeight = mp.getVideoHeight();
//Get the width of the screen
int screenWidth = getWindowManager().getDefaultDisplay().getWidth();
//Get the SurfaceView layout parameters
android.view.ViewGroup.LayoutParams lp = mSurfaceView.getLayoutParams();
//Set the width of the SurfaceView to the width of the screen
lp.width = screenWidth;
//Set the height of the SurfaceView to match the aspect ratio of the video
//be sure to cast these as floats otherwise the calculation will likely be 0
lp.height = (int) (((float)videoHeight / (float)videoWidth) * (float)screenWidth);
//Commit the layout parameters
mSurfaceView.setLayoutParams(lp);
//Start video
mp.setDisplay(holder);
mp.start();
}