处理视频拉伸问题和 黑屏

This commit is contained in:
litingting 2024-12-21 17:37:50 +08:00
parent 3dab1d77c9
commit d47bed73a5
6 changed files with 91 additions and 158 deletions

View File

@ -4,6 +4,8 @@
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SET_WALLPAPER" />
<uses-permission android:name="android.permission.BIND_WALLPAPER"
tools:ignore="ProtectedPermissions" />
<application
android:name=".App"

View File

@ -1,28 +1,23 @@
package com.live.mylivewallpaper.activity;
import static android.provider.Contacts.SettingsColumns.KEY;
import android.app.WallpaperInfo;
import android.app.WallpaperManager;
import android.content.ComponentName;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.CountDownTimer;
import android.view.SurfaceHolder;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.annotation.OptIn;
import androidx.media3.common.MediaItem;
import androidx.media3.common.util.UnstableApi;
import androidx.media3.datasource.DefaultDataSourceFactory;
import androidx.media3.datasource.DefaultHttpDataSource;
import androidx.media3.exoplayer.ExoPlayer;
import androidx.media3.exoplayer.SimpleExoPlayer;
import androidx.media3.exoplayer.source.ProgressiveMediaSource;
import com.live.mylivewallpaper.base.BaseActivity;
import com.live.mylivewallpaper.data.LikeData;
import com.live.mylivewallpaper.data.ResultData;
import com.live.mylivewallpaper.databinding.ActivityVideoBinding;
import com.live.mylivewallpaper.help.Common;
import com.live.mylivewallpaper.help.Db;
@ -32,11 +27,8 @@ import com.live.mylivewallpaper.listener.OnVideoResultListener;
import com.live.mylivewallpaper.request.RetrofitManager;
import java.io.File;
import androidx.media3.exoplayer.SimpleExoPlayer;
import androidx.media3.exoplayer.source.MediaSource;
import androidx.media3.exoplayer.source.ProgressiveMediaSource;
public class VideoActivity extends BaseActivity<ActivityVideoBinding> implements SurfaceHolder.Callback {
public class VideoActivity extends BaseActivity<ActivityVideoBinding> {
private CountDownTimer countDownTimer;
@ -77,6 +69,7 @@ public class VideoActivity extends BaseActivity<ActivityVideoBinding> implements
image = intent.getStringExtra(KEY_image);
describe = intent.getStringExtra(KEY_image);
thumb = intent.getStringExtra(KEY_thumb);
vb.tvDescribe.setText(describe);
showLoading(true);
initFavorite();
@ -94,7 +87,7 @@ public class VideoActivity extends BaseActivity<ActivityVideoBinding> implements
@Override
public void onVideoResult(boolean success, String path) {
Common.logMsg("-------------success ="+success+"--path="+path);
Common.logMsg("-------------success =" + success + "--path=" + path);
runOnUiThread(new Runnable() {
@Override
public void run() {
@ -113,7 +106,7 @@ public class VideoActivity extends BaseActivity<ActivityVideoBinding> implements
}
private void initFavorite(){
private void initFavorite() {
boolean b = Db.queryIsLike(id);
vb.imFavorite.setSelected(b);
}
@ -137,19 +130,6 @@ public class VideoActivity extends BaseActivity<ActivityVideoBinding> implements
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onPause() {
super.onPause();
}
private void showLoading(boolean loading) {
if (loading) {
// vb.topLayout.setBackgroundColor(getColor(R.color.color_white_80));
@ -199,7 +179,7 @@ public class VideoActivity extends BaseActivity<ActivityVideoBinding> implements
vb.imFavorite.setSelected(!vb.imFavorite.isSelected());
boolean selected = vb.imFavorite.isSelected();
if (selected) {
LikeData likeData = new LikeData(describe, id, image, wallpaperType,thumb);
LikeData likeData = new LikeData(describe, id, image, wallpaperType, thumb);
Db.insertLike(likeData);
} else {
Db.deleteLike(id);
@ -229,41 +209,23 @@ public class VideoActivity extends BaseActivity<ActivityVideoBinding> implements
}
@Override
public void surfaceCreated(@NonNull SurfaceHolder holder) {
Common.logMsg("------------surfaceCreated");
mySurfaceHolder = holder;
checkPlay();
}
@Override
public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {
Common.logMsg("------------surfaceChanged");
}
@Override
public void surfaceDestroyed(@NonNull SurfaceHolder holder) {
Common.logMsg("------------surfaceDestroyed");
}
@OptIn(markerClass = UnstableApi.class)
private void playMedia3() {
try {
Uri uriFromFilePath = Common.getUriFromFilePath(this, filePath);
MediaItem mediaItem = MediaItem.fromUri(uriFromFilePath);
ProgressiveMediaSource mediaSource = createMediaSource(mediaItem);
exoPlayer.setMediaSource(mediaSource);
exoPlayer.prepare();
exoPlayer.play();
}catch (Exception e){
Common.logMsg("-------"+e.getMessage());
}
try {
Uri uriFromFilePath = Common.getUriFromFilePath(this, filePath);
MediaItem mediaItem = MediaItem.fromUri(uriFromFilePath);
ProgressiveMediaSource mediaSource = createMediaSource(mediaItem);
exoPlayer.setMediaSource(mediaSource);
exoPlayer.prepare();
exoPlayer.play();
} catch (Exception e) {
Common.logMsg("-------" + e.getMessage());
}
}
@OptIn(markerClass = UnstableApi.class)
public ProgressiveMediaSource createMediaSource(MediaItem mediaItem) {
DefaultDataSourceFactory dataSourceFactory = new DefaultDataSourceFactory(
DefaultDataSourceFactory dataSourceFactory = new DefaultDataSourceFactory(
this, "user-agent"
);
ProgressiveMediaSource.Factory factory = new ProgressiveMediaSource.Factory(dataSourceFactory);
@ -271,33 +233,25 @@ public class VideoActivity extends BaseActivity<ActivityVideoBinding> implements
}
private void setLiveWallpaper() {
if (mFile.exists()) {
Shared.INSTANCE.setVideo_path(filePath);
Common.logMsg("----------setVideo_path---filePath=" + filePath);
}
WallpaperManager instance = WallpaperManager.getInstance(VideoActivity.this);
WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
try {
wallpaperManager.clear();
Common.logMsg("----------clear=");
} catch (Exception e) {
Common.logMsg("---------e=" + e.getMessage());
}
ComponentName componentName = new ComponentName(VideoActivity.this, LiveWallpaperService.class);
WallpaperInfo wallpaperInfo = instance.getWallpaperInfo();
Intent intent = new Intent(WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER);
intent.putExtra(WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT, componentName);
startActivity(intent);
finish();
// if((wallpaperInfo!= null)&&wallpaperInfo.getComponent()!= componentName){
// Intent intent = new Intent(WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER);
// intent.putExtra(WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT, componentName);
// startActivity(intent);
// }else {
// Intent intent = new Intent(WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER);
// intent.putExtra(WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT, componentName);
// startActivity(intent);
// }
}

View File

@ -3,15 +3,14 @@ package com.live.mylivewallpaper.help
import android.content.Context
import android.net.Uri
import android.service.wallpaper.WallpaperService
import android.view.Surface
import android.view.SurfaceHolder
import androidx.annotation.OptIn
import androidx.media3.common.C
import androidx.media3.common.MediaItem
import androidx.media3.common.util.UnstableApi
import androidx.media3.datasource.DefaultDataSourceFactory
import androidx.media3.exoplayer.ExoPlayer
import androidx.media3.exoplayer.source.ProgressiveMediaSource
import com.live.mylivewallpaper.R
import java.io.File
@ -25,10 +24,71 @@ class LiveWallpaperService : WallpaperService() {
inner class VideoWallpaperEngine : Engine() {
private var exoPlayer: ExoPlayer? = null
@OptIn(UnstableApi::class)
private fun initExoPlay() {
exoPlayer = ExoPlayer.Builder(this@LiveWallpaperService).build()
exoPlayer?.repeatMode = ExoPlayer.REPEAT_MODE_ONE
update()
}
override fun onCreate(surfaceHolder: SurfaceHolder?) {
super.onCreate(surfaceHolder)
Common.logMsg("-----Engine-----onCreate")
initExoPlay()
}
@OptIn(UnstableApi::class)
override fun onSurfaceCreated(holder: SurfaceHolder?) {
super.onSurfaceCreated(holder)
val surface = holder?.surface
if (surface != null) {
exoPlayer?.setVideoSurface(surface)
exoPlayer?.videoScalingMode = C.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING
}
}
override fun onVisibilityChanged(visible: Boolean) {
super.onVisibilityChanged(visible)
Common.logMsg("-------Engine---onVisibilityChanged visible=$visible")
if (visible) {
update()
exoPlayer?.play()
} else {
exoPlayer?.pause()
}
}
override fun onSurfaceDestroyed(holder: SurfaceHolder?) {
super.onSurfaceDestroyed(holder)
Common.logMsg("------Engine----onSurfaceDestroyed---")
exoPlayer?.release()
}
override fun onDestroy() {
super.onDestroy()
Common.logMsg("-------Engine---onDestroy---")
}
@OptIn(UnstableApi::class)
override fun onSurfaceChanged(
holder: SurfaceHolder?,
format: Int,
width: Int,
height: Int
) {
super.onSurfaceChanged(holder, format, width, height)
}
@OptIn(UnstableApi::class)
private fun update() {
val uri = getVideoUrl(this@LiveWallpaperService)
// val test = "android.resource://" + packageName + "/" + R.raw.test1
if (uri != null) {
@ -40,81 +100,11 @@ class LiveWallpaperService : WallpaperService() {
exoPlayer?.prepare()
exoPlayer?.playWhenReady = true
}
}
override fun onCreate(surfaceHolder: SurfaceHolder?) {
super.onCreate(surfaceHolder)
Common.logMsg("-----Engine-----onCreate")
initExoPlay()
}
override fun onSurfaceCreated(holder: SurfaceHolder?) {
super.onSurfaceCreated(holder)
val surface = holder?.surface
if (surface != null) {
exoPlayer?.setVideoSurface(surface)
}
}
override fun onVisibilityChanged(visible: Boolean) {
super.onVisibilityChanged(visible)
Common.logMsg("-------Engine---onVisibilityChanged visible=$visible")
if (visible) {
exoPlayer?.play()
} else {
exoPlayer?.pause()
}
}
override fun onSurfaceDestroyed(holder: SurfaceHolder?) {
super.onSurfaceDestroyed(holder)
Common.logMsg("------Engine----onSurfaceDestroyed---")
}
override fun onDestroy() {
super.onDestroy()
Common.logMsg("-------Engine---onDestroy---")
exoPlayer?.release()
}
override fun onSurfaceChanged(
holder: SurfaceHolder?,
format: Int,
width: Int,
height: Int
) {
super.onSurfaceChanged(holder, format, width, height)
Common.logMsg("----------Engine---onSurfaceChanged")
}
}
private fun adjustSurfaceViewSize(surface: Surface, containerWidth: Int, containerHeight: Int) {
val videoWidth = 1920 // 假设视频宽度为 1920
val videoHeight = 1080 // 假设视频高度为 1080
// 计算视频的宽高比
val videoAspectRatio = videoWidth.toFloat() / videoHeight
var newWidth = containerWidth
var newHeight = (containerWidth / videoAspectRatio).toInt()
// 如果计算的高度大于容器高度,则调整为容器高度
if (newHeight > containerHeight) {
newHeight = containerHeight
newWidth = (containerHeight * videoAspectRatio).toInt()
}
// 更新 SurfaceView 的布局参数
// this.setLayoutParams(FrameLayout.LayoutParams(newWidth, newHeight))
}
private fun getVideoUrl(con: Context): Uri? {

View File

@ -36,7 +36,7 @@ public class MyPagingSource extends PagingSource<Integer, ResultData> {
int page = loadParams.getKey() != null ? loadParams.getKey() : 1;
List<ResultData> data = Db.queryData(wallpaperType,page);
Common.logMsg("-----load---------"+wallpaperType+"----page="+page+"'--data="+data.size());
// Common.logMsg("-----load---------"+wallpaperType+"----page="+page+"'--data="+data.size());
return new LoadResult.Page<>(
data,
page > 1 ? page - 1 : null,

View File

@ -8,18 +8,6 @@
android:background="@color/white"
tools:context=".activity.VideoActivity">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone">
<SurfaceView
android:id="@+id/surfaceVideo"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
<androidx.media3.ui.PlayerView
android:id="@+id/player_view"
android:layout_width="match_parent"
@ -37,7 +25,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:layout_marginTop="34dp"
android:layout_marginTop="37dp"
android:background="@drawable/bg_shape_favorite"
android:padding="11dp">

View File

@ -2,5 +2,4 @@
<wallpaper xmlns:android="http://schemas.android.com/apk/res/android"
android:description="@string/app_name"
android:thumbnail="@mipmap/ic_launcher">
</wallpaper>