package com.ixuea.courses.mymusic.service; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.res.AssetFileDescriptor; import android.content.res.AssetManager; import android.content.res.Configuration; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.SurfaceTexture; import android.media.MediaPlayer; import android.os.Bundle; import android.os.Handler; import android.service.wallpaper.WallpaperService; import android.view.MotionEvent; import android.view.Surface; import android.view.SurfaceHolder; import android.view.TextureView; import android.view.View; import com.ixuea.courses.mymusic.R; import java.io.IOException; public class MyWallpaperService extends WallpaperService { public static final String ACTION_WALLPAPER_BROADCAST = "com.example.BroadcastReceiverApp.WALLPAPER"; public static final String ACTION_PLAY_VIDEO_BROADCAST = "com.example.BroadcastReceiverApp.PLAY_VIDEO"; public static final String ACTION_DISPLAY_PICTURE_BROADCAST = "com.example.BroadcastReceiverApp.DISPLAY_PICTURE"; @Override public Engine onCreateEngine() { return new MyWallpaperEngine(); } private class MyWallpaperEngine extends Engine { private TextureView textureView; private MediaPlayer mediaPlayer; private boolean isVideoWallpaper; private boolean isVisible; private Bitmap bitmap; private AssetFileDescriptor descriptor; private BroadcastReceiver receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (ACTION_PLAY_VIDEO_BROADCAST.equals(action)) { isVideoWallpaper = true; playVideo(); } if (ACTION_DISPLAY_PICTURE_BROADCAST.equals(action)) { isVideoWallpaper = false; stopVideo(); } } }; private final MediaPlayer.OnPreparedListener onPreparedListener = new MediaPlayer.OnPreparedListener() { @Override public void onPrepared(MediaPlayer mp) { mediaPlayer.start(); } }; private final TextureView.SurfaceTextureListener surfaceTextureListener = new TextureView.SurfaceTextureListener() { @Override public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) { mediaPlayer.setSurface(new Surface(surface)); mediaPlayer.prepareAsync(); } @Override public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) { // Ignored, the video size is set in the MediaPlayer } @Override public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) { return true; } @Override public void onSurfaceTextureUpdated(SurfaceTexture surface) { // Invoked every time there's a new frame available to draw } }; private final View.OnClickListener toggleButtonClickListener = new View.OnClickListener() { @Override public void onClick(View v) { isVideoWallpaper = !isVideoWallpaper; if (isVideoWallpaper) { playVideo(); } else { stopVideo(); } } }; private void playVideo() { mediaPlayer = new MediaPlayer(); try { mediaPlayer.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength()); mediaPlayer.setOnPreparedListener(onPreparedListener); mediaPlayer.setLooping(true); mediaPlayer.prepare(); } catch (IOException e) { e.printStackTrace(); } mediaPlayer.start(); } private void stopVideo() { if (mediaPlayer != null) { mediaPlayer.stop(); mediaPlayer.release(); mediaPlayer = null; } } @Override public void onCreate(SurfaceHolder surfaceHolder) { super.onCreate(surfaceHolder); setTouchEventsEnabled(true); IntentFilter filter = new IntentFilter(); filter.addAction(ACTION_WALLPAPER_BROADCAST); getApplicationContext().registerReceiver(receiver, filter); bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.qihai); AssetManager manager = getApplicationContext().getAssets(); try { descriptor = manager.openFd("test.mp4"); } catch (IOException e) { e.printStackTrace(); } } @Override public void onDestroy() { super.onDestroy(); if (mediaPlayer != null) { mediaPlayer.release(); mediaPlayer = null; } unregisterReceiver(receiver); } @Override public void onVisibilityChanged(boolean visible) { isVisible = visible; if (isVisible) { if (isVideoWallpaper) { playVideo(); } else { drawFrame(); } } else { stopVideo(); } } @Override public void onSurfaceCreated(SurfaceHolder holder) { super.onSurfaceCreated(holder); if (textureView == null) { textureView = new TextureView(getApplicationContext()); textureView.setSurfaceTextureListener(surfaceTextureListener); textureView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { // Handle touch events if needed return true; } }); // Add TextureView to the SurfaceHolder addTextureViewToSurface(textureView); } } private void addTextureViewToSurface(TextureView textureView) { SurfaceHolder holder = getSurfaceHolder(); Canvas canvas = null; try { canvas = holder.lockCanvas(); if (canvas != null) { textureView.setLayerType(TextureView.LAYER_TYPE_HARDWARE, null); textureView.layout(0, 0, canvas.getWidth(), canvas.getHeight()); textureView.draw(canvas); } } finally { if (canvas != null) { holder.unlockCanvasAndPost(canvas); } } } private void drawFrame() { SurfaceHolder holder = getSurfaceHolder(); Canvas canvas = null; try { canvas = holder.lockCanvas(); if (canvas != null) { // Clear the canvas canvas.drawBitmap(bitmap, 0, 0, new Paint()); // Draw static content } } catch (Exception e) { e.printStackTrace(); } finally { if (canvas != null) { holder.unlockCanvasAndPost(canvas); } } } } }
WallpaperService实现动态壁纸和静态图片展示
最新推荐文章于 2024-08-17 03:53:45 发布