一、AndroidManifest.xml
AndroidManifest.xml注册service
<service
android:name="com.android.dreamscreenvideoplay.MyVideoDreamService"
android:exported="true"
android:permission="android.permission.BIND_DREAM_SERVICE"
android:screenOrientation="landscape">
<intent-filter>
<action android:name="android.service.dreams.DreamService" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</service>
二、MyVideoDreamService.java
添加src/com/android/dreamscreenvideoplay/MyVideoDreamService.java
package com.android.dreamscreenvideoplay;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Build;
import android.service.dreams.DreamService;
import android.view.View;
import android.widget.MediaController;
import android.widget.VideoView;
public class VideoDreamService extends DreamService {
VideoView mVideoView=null;
@Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
setInteractive(false);
setFullscreen(true);
setContentView(R.layout.draem_video_play);
addview();
}
public void addview(){
Uri uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.video);
mVideoView = (VideoView) findViewById(R.id.video);
//videoView.setMediaController(new MediaController(this));
mVideoView.setVideoURI(uri);
mVideoView.start();
mVideoView.requestFocus();
mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mPlayer) {
// TODO Auto-generated method stub
mPlayer.start();
mPlayer.setLooping(true);
}
});
hideBottomUIMenu();
}
protected void hideBottomUIMenu() {
if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) { // lower api
View v = this.getWindow().getDecorView();
v.setSystemUiVisibility(View.GONE);
} else if (Build.VERSION.SDK_INT >= 19) {
//for new api versions.
View decorView = getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
}
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public void onDetachedFromWindow() {
super.onDetachedFromWindow();
if(mVideoView!=null){
mVideoView.stopPlayback();
mVideoView.setOnCompletionListener(null);
}
//mVideoView.setOnPreparedListener(null);
System.exit(0);
android.os.Process.killProcess(android.os.Process.myPid());
}
@Override
public void onDreamingStarted() {
super.onDreamingStarted();
}
}
三、draem_video_play.xml
添加布局res/layout/draem_video_play.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0000">
<VideoView
android:id="@+id/video"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_height="wrap_content"
/>
</RelativeLayout>
四、video.mp4
最后添加视频资源,res/raw/video.mp4