Phiên bản đầy đủ của phiên bản sửa đổi của luigi23 với tránh một số tai nạn.
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
không có hoạt động chính. bạn có thể muốn làm cho nó fullscreen bằng cách thêm
<style name="Theme.AppCompat.Light.NoActionBar.FullScreen" parent="@style/Theme.AppCompat.Light.NoActionBar">
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">@null</item>
</style>
Tạo một file IntroVideoSurfaceView.java
import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.os.Build;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import java.io.IOException;
public class IntroVideoSurfaceView extends SurfaceView implements SurfaceHolder.Callback {
private MediaPlayer mp;
private boolean has_started = false;
@TargetApi(Build.VERSION_CODES.LOLLIPOP) public IntroVideoSurfaceView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init();
}
public IntroVideoSurfaceView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
public IntroVideoSurfaceView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public IntroVideoSurfaceView(Context context) {
super(context);
init();
}
private void init() {
mp = new MediaPlayer();
getHolder().addCallback(this);
}
@Override public void surfaceCreated(SurfaceHolder holder) {
AssetFileDescriptor afd = getResources().openRawResourceFd(R.raw.slideshow);
try {
if (!has_started) {
has_started = true;
mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getDeclaredLength());
}
mp.prepare();
android.view.ViewGroup.LayoutParams lp = getLayoutParams();
lp.height = getHeight();
lp.width = getWidth();
setLayoutParams(lp);
mp.setDisplay(getHolder());
mp.setLooping(true);
mp.start();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override public void surfaceDestroyed(SurfaceHolder holder) {
mp.stop();
}
}
thêm một "slideshow.mp4" tài nguyên/thô
sửa đổi activity_main.xml bằng
<?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"
>
<com.androidadvance.videobackground.IntroVideoSurfaceView
android:id="@+id/surface"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Press Me"
android:id="@+id/button"
android:layout_gravity="center_horizontal|bottom"
android:layout_margin="16dp"
/>
</FrameLayout>
Một số lưu ý.
Thêm video sẽ làm cho apk của bạn rất lớn, do đó bạn có thể muốn tránh điều này ... Thỉnh thoảng bị treo chưa biết xảy ra ngay cả trên điện thoại cao cấp (thiên hà s6) Nó là điều cần thiết để giữ cho các tập tin nhỏ.
Có cách nào tôi có thể làm điều tương tự từ một dịch vụ nền? Giống như tạo trình phát video từ dịch vụ? –
Còn về 'TextureView' thì sao? @AndroSelva –
Điều này không hiệu quả đối với tôi, Hy vọng có ai đó đến với giải pháp tốt hơn hoặc cách hiệu quả để tích hợp tệp video. – XcodeNOOB