98 lines
3.2 KiB
Java
98 lines
3.2 KiB
Java
package com.live.flowlivewallpaper.service;
|
|
|
|
import android.content.Context;
|
|
import android.content.SharedPreferences;
|
|
import android.net.Uri;
|
|
import android.service.wallpaper.WallpaperService;
|
|
import android.util.Log;
|
|
import android.view.SurfaceHolder;
|
|
import com.google.android.exoplayer2.C;
|
|
import com.google.android.exoplayer2.ExoPlayer;
|
|
import com.google.android.exoplayer2.MediaItem;
|
|
import com.google.android.exoplayer2.source.ProgressiveMediaSource;
|
|
import com.google.android.exoplayer2.upstream.DefaultDataSource;
|
|
import java.io.File;
|
|
|
|
public class LiveService extends WallpaperService {
|
|
|
|
@Override
|
|
public Engine onCreateEngine() {
|
|
return new VideoEngine();
|
|
}
|
|
|
|
private class VideoEngine extends Engine {
|
|
private ExoPlayer exoPlayer;
|
|
|
|
@Override
|
|
public void onCreate(SurfaceHolder surfaceHolder) {
|
|
super.onCreate(surfaceHolder);
|
|
initExoPlayer();
|
|
}
|
|
|
|
private void initExoPlayer() {
|
|
exoPlayer = new ExoPlayer.Builder(LiveService.this).build();
|
|
exoPlayer.setRepeatMode(ExoPlayer.REPEAT_MODE_ONE);
|
|
update();
|
|
}
|
|
|
|
@Override
|
|
public void onSurfaceCreated(SurfaceHolder holder) {
|
|
super.onSurfaceCreated(holder);
|
|
if (holder != null && exoPlayer != null) {
|
|
exoPlayer.setVideoSurface(holder.getSurface());
|
|
exoPlayer.setVideoScalingMode(C.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onVisibilityChanged(boolean visible) {
|
|
super.onVisibilityChanged(visible);
|
|
if (exoPlayer != null) {
|
|
if (visible) {
|
|
update();
|
|
exoPlayer.play();
|
|
} else {
|
|
exoPlayer.pause();
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onSurfaceDestroyed(SurfaceHolder holder) {
|
|
super.onSurfaceDestroyed(holder);
|
|
if (exoPlayer != null) {
|
|
exoPlayer.release();
|
|
exoPlayer = null;
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onDestroy() {
|
|
super.onDestroy();
|
|
if (exoPlayer != null) {
|
|
exoPlayer.release();
|
|
exoPlayer = null;
|
|
}
|
|
}
|
|
|
|
private void update() {
|
|
Uri uri = getVideoUrl(LiveService.this);
|
|
if (uri != null && exoPlayer != null) {
|
|
MediaItem mediaItem = MediaItem.fromUri(uri);
|
|
ProgressiveMediaSource mediaSource = new ProgressiveMediaSource.Factory(
|
|
new DefaultDataSource.Factory(LiveService.this))
|
|
.createMediaSource(mediaItem);
|
|
exoPlayer.setMediaSource(mediaSource);
|
|
exoPlayer.prepare();
|
|
exoPlayer.setPlayWhenReady(true);
|
|
}
|
|
}
|
|
|
|
private Uri getVideoUrl(Context context) {
|
|
SharedPreferences prefs = context.getSharedPreferences("WallpaperPrefs", MODE_PRIVATE);
|
|
String path = prefs.getString("video_path", "");
|
|
File file = new File(path);
|
|
return (file.isFile() && file.exists()) ? Uri.fromFile(file) : null;
|
|
}
|
|
}
|
|
} |