修复播放时的bfqkz.list为空null的闪退BUG

This commit is contained in:
muqing 2023-12-27 10:12:44 +08:00
parent 9b7de6544d
commit 4dbf60876c
9 changed files with 109 additions and 108 deletions

View File

@ -1,5 +1,6 @@
package com.muqingbfq;
import java.util.ArrayList;
import java.util.Objects;
public class MP3 {

View File

@ -31,7 +31,7 @@ public class MediaPlayer extends android.media.MediaPlayer {
public Runnable updateSeekBar = new Runnable() {
@Override
public void run() {
if (isPlaying() && Media.lrcview != null) {
if (isPlaying() && bfq.lrcview != null) {
long position = getCurrentPosition();
Media.setProgress((int) position);
}
@ -41,37 +41,25 @@ public class MediaPlayer extends android.media.MediaPlayer {
@SuppressLint("UnsafeOptInUsageError")
public MediaPlayer() {
setOnErrorListener(new OnErrorListener() {
@Override
public boolean onError(android.media.MediaPlayer mediaPlayer, int i, int i1) {
setOnErrorListener((mediaPlayer, i, i1) -> {
if (bfqkz.list.isEmpty()) {
return false;
}
//针对错误进行相应的处理
bfqkz.list.remove(bfqkz.xm);
bfqkz.xm = bfqkz.list.get(bfqkz.getmti(bfqkz.ms));
new Thread() {
@Override
public void run() {
super.run();
bfqkz.mp3(com.muqingbfq.api.
new bfqkz.mp3(com.muqingbfq.api.
url.hq(bfqkz.xm));
}
}.start();
return false;
}
});
setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(android.media.MediaPlayer mediaPlayer) {
setOnCompletionListener(mediaPlayer -> {
if (bfqkz.list.isEmpty()) {
return;
}
int i = bfqkz.getmti(bfqkz.ms);
bfqkz.xm = bfqkz.list.get(i);
new Thread() {
@Override
public void run() {
super.run();
bfqkz.mp3(com.muqingbfq.api.
new bfqkz.mp3(com.muqingbfq.api.
url.hq(bfqkz.xm));
}
}.start();
}
});
setAudioAttributes(new AudioAttributes
.Builder()
@ -110,7 +98,7 @@ public class MediaPlayer extends android.media.MediaPlayer {
start();
main.handler.post(() -> {
bfui();
if (bfq.inflate != null) {
if (bfq.binding != null) {
main.handler.removeCallbacks(updateSeekBar); // 在播放开始时启动更新进度
long duration = getDuration();
Media.setMax((int) getDuration());
@ -121,6 +109,7 @@ public class MediaPlayer extends android.media.MediaPlayer {
}
// 在这里将进度更新到UI上
});
wj.setMP3ToFile(bfqkz.xm);
}
public void DataSource(String path) throws Exception {
@ -167,7 +156,7 @@ public class MediaPlayer extends android.media.MediaPlayer {
public void bfui() {
setTX();
String name = xm.name, zz = bfqkz.xm.zz;
if (bfq.inflate != null) {
if (bfq.binding != null) {
Media.setProgress(0);
bfq.setname(name);
bfq.setzz(zz);

View File

@ -39,7 +39,7 @@ public class url extends Thread {
public static String hq(MP3 x) {
getLrc(x.id);
if (Media.lrcview != null) {
if (bfq.lrcview != null) {
Media.loadLyric();
}
try {

View File

@ -11,6 +11,7 @@ import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
@ -38,32 +39,56 @@ import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import me.wcy.lrcview.LrcView;
public class bfq extends AppCompatActivity {
@SuppressLint("StaticFieldLeak")
public static ActivityBfqBinding inflate;
public static ActivityBfqBinding binding;
public static String lrc;
public static LrcView lrcview;
private void setLrc(){
lrcview = binding.lrcView;
lrcview.setCurrentColor(ContextCompat.getColor(this,R.color.text));
lrcview.setLabel(getString(R.string.app_name));
lrcview.setCurrentTextSize(TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics()));
// lrcView.setLrcPadding(16);
lrcview.setCurrentTextSize(TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_SP, 20, getResources().getDisplayMetrics()));
lrcview.setTimelineTextColor(ContextCompat.getColor(this,R.color.text_tm));
lrcview.setDraggable(true, (view, time) -> {
bfqkz.mt.seekTo(Math.toIntExact(time));
return false;
});
if (!gj.isTablet(this)) {
lrcview.setOnTapListener((view, x, y) -> {
View kp = binding.kp1;
if (kp.getVisibility() == View.VISIBLE) {
kp.setVisibility(View.GONE);
} else {
kp.setVisibility(View.VISIBLE);
}
});
} else {
lrcview.setOnTapListener((view, x, y) -> {
});
}
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
inflate = ActivityBfqBinding.inflate(getLayoutInflater());
new Media(inflate);
Media.lrcview.setCurrentColor(ContextCompat.getColor(this,R.color.text));
Media.lrcview.setLabel(getString(R.string.app_name));
Media.lrcview.setCurrentTextSize(TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics()));
// lrcView.setLrcPadding(16);
Media.lrcview.setCurrentTextSize(TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_SP, 20, getResources().getDisplayMetrics()));
Media.lrcview.setTimelineTextColor(ContextCompat.getColor(this,R.color.text_tm));
LinearLayout root = inflate.getRoot();
binding = ActivityBfqBinding.inflate(getLayoutInflater());
setLrc();
new Media(binding);
LinearLayout root = binding.getRoot();
TypedValue typedValue = new TypedValue();
home.appCompatActivity.getTheme().resolveAttribute(android.R.attr.windowBackground, typedValue, true);
// 设置背景颜色
root.setBackgroundColor(typedValue.data);
setContentView(root);
Toolbar toolbar = inflate.toolbar;
Toolbar toolbar = binding.toolbar;
toolbar.setNavigationOnClickListener(view1 -> finish());
toolbar.setOnMenuItemClickListener(item -> {
if (item.getItemId() == R.id.fx && bfqkz.xm != null) {
@ -75,18 +100,18 @@ public class bfq extends AppCompatActivity {
return false;
});
bfq_an.kz kz = new bfq_an.kz();
inflate.kg.setOnClickListener(kz);
inflate.xyq.setOnClickListener(kz);
inflate.syq.setOnClickListener(kz);
inflate.bfqListMp3.
binding.kg.setOnClickListener(kz);
binding.xyq.setOnClickListener(kz);
binding.syq.setOnClickListener(kz);
binding.bfqListMp3.
setOnClickListener(view1 -> com.muqingbfq.fragment.bflb_db.start(this));
inflate.control.setOnClickListener(new bfq_an.control(inflate.control));
binding.control.setOnClickListener(new bfq_an.control(binding.control));
if (bfqkz.mt != null && bfqkz.mt.isPlaying()) {
inflate.kg.setImageResource(R.drawable.bf);
binding.kg.setImageResource(R.drawable.bf);
}
text();
inflate.like.setOnClickListener(view1 -> {
binding.like.setOnClickListener(view1 -> {
try {
Gson gson = new Gson();
Type type = new TypeToken<List<MP3>>() {
@ -97,12 +122,12 @@ public class bfq extends AppCompatActivity {
}
if (bfqkz.like_bool) {
list.remove(bfqkz.xm);
inflate.like
binding.like
.setImageTintList(ContextCompat.getColorStateList(bfq.this, R.color.text));
} else {
if (!list.contains(bfqkz.xm)) {
list.add(bfqkz.xm);
inflate.like.setImageTintList(ContextCompat.
binding.like.setImageTintList(ContextCompat.
getColorStateList(bfq.this, android.R.color.holo_red_dark));
}
}
@ -112,7 +137,7 @@ public class bfq extends AppCompatActivity {
e.printStackTrace();
}
});
inflate.download.setOnClickListener(view -> {
binding.download.setOnClickListener(view -> {
if (wj.cz(wj.mp3 + bfqkz.xm.id)) {
gj.ts(this, "你已经下载过这首歌曲了");
return;
@ -149,17 +174,17 @@ public class bfq extends AppCompatActivity {
}
public static void setname(String str) {
if (inflate == null) {
if (binding == null) {
return;
}
inflate.name.setText(str);
binding.name.setText(str);
}
public static void setzz(String str) {
if (inflate == null) {
if (binding == null) {
return;
}
inflate.zz.setText(str);
binding.zz.setText(str);
}
public static Bitmap bitmap;
@ -172,10 +197,10 @@ public class bfq extends AppCompatActivity {
}
public static void kgsetImageResource(int a) {
if (inflate == null) {
if (binding == null) {
return;
}
inflate.kg.setImageResource(a);
binding.kg.setImageResource(a);
}
@Override
@ -187,8 +212,8 @@ public class bfq extends AppCompatActivity {
@SuppressLint("ClickableViewAccessibility")
public void text() {
inflate.toolbar.setOnTouchListener((view, motionEvent) -> {
LinearLayout root = inflate.getRoot();
binding.toolbar.setOnTouchListener((view, motionEvent) -> {
LinearLayout root = binding.getRoot();
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN:
downY = motionEvent.getRawY();
@ -208,7 +233,7 @@ public class bfq extends AppCompatActivity {
root.setTranslationY(dy);
break;
case MotionEvent.ACTION_UP:
if (inflate.getRoot().getY() > main.g - main.g / 1.5) {
if (binding.getRoot().getY() > main.g - main.g / 1.5) {
finish();
return true;
}
@ -227,14 +252,14 @@ public class bfq extends AppCompatActivity {
});
}
public static void setlike(boolean bool) {
if (inflate == null) {
if (binding == null) {
return;
}
int color = R.color.text;
if (bool) {
color = android.R.color.holo_red_dark;
}
inflate.like.setImageTintList(ContextCompat.
getColorStateList(inflate.getRoot().getContext(), color));
binding.like.setImageTintList(ContextCompat.
getColorStateList(binding.getRoot().getContext(), color));
}
}

View File

@ -34,6 +34,9 @@ public class bfq_an {
}
public static void syq() {
if (bfqkz.list.isEmpty()) {
return;
}
bfqkz.mt.pause();
int i = bfqkz.list.indexOf(bfqkz.xm) - 1;
if (i < 0) {
@ -44,7 +47,7 @@ public class bfq_an {
}
public static void xyq() {
if (bfqkz.list == null) {
if (bfqkz.list.isEmpty()) {
return;
}
bfqkz.mt.pause();

View File

@ -4,14 +4,12 @@ import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Intent;
import android.net.Uri;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.media.MediaMetadataCompat;
import android.support.v4.media.session.MediaSessionCompat;
import android.support.v4.media.session.PlaybackStateCompat;
import android.text.TextUtils;
import android.view.KeyEvent;
import androidx.annotation.Nullable;
@ -67,8 +65,8 @@ public class bfqkz extends Service {
if (bfqkz.lishi_list.size() >= 100) {
bfqkz.lishi_list.remove(0);
}
if (bfqkz.lishi_list.size() > 0) {
bfqkz.lishi_list.remove(bfqkz.xm);
if (!bfqkz.lishi_list.contains(bfqkz.xm)) {
bfqkz.lishi_list.add(0, bfqkz.xm);
wj.xrwb(wj.gd + "mp3_hc.json", new com.google.gson.Gson().toJson(bfqkz.lishi_list));
}
@ -116,7 +114,7 @@ public class bfqkz extends Service {
class callback extends MediaSessionCompat.Callback {
@Override
public boolean onMediaButtonEvent(Intent mediaButtonEvent) {
KeyEvent event = (KeyEvent) mediaButtonEvent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
mediaButtonEvent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
return true;
}
@ -144,6 +142,7 @@ public class bfqkz extends Service {
}
}
@SuppressLint("SwitchIntDef")
@Override
public void onPlayFromUri(Uri uri, Bundle extras) {
try {
@ -174,12 +173,14 @@ public class bfqkz extends Service {
public IBinder onBind(Intent intent) {
return null;
}
/*
public class MyBinder extends Binder {
bfqkz getService() {
return bfqkz.this;
}
}
*/
public static void updateNotification() {
try {

View File

@ -28,7 +28,6 @@ public class Media{
private static TextView time_a, time_b;
@SuppressLint("StaticFieldLeak")
private static SeekBar tdt;
public static LrcView lrcview;
public static void setTime_a(String str) {
if (time_a == null) {
@ -50,7 +49,7 @@ public class Media{
public static void setProgress(int progress) {
tdt.setProgress(progress);
lrcview.updateTime(progress);
bfq.lrcview.updateTime(progress);
}
public static void setbf(boolean bool) {
@ -67,20 +66,17 @@ public class Media{
public Media(ActivityBfqBinding binding) {
imageView = binding.cardview;
lrcview = binding.lrcView;
tdt = binding.tdt;
tdt.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
setTime_b(bfq_an.getTime(progress));
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// 拖动条移动中
main.handler.removeCallbacks(bfqkz.mt.updateSeekBar);
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// 播放音乐到指定位置
@ -91,20 +87,7 @@ public class Media{
time_a = binding.timeA;
time_b = binding.timeB;
//初始化歌词组件
lrcview.setDraggable(true, (view, time) -> {
bfqkz.mt.seekTo(Math.toIntExact(time));
return false;
});
if (!isTablet(binding.getRoot().getContext())) {
lrcview.setOnTapListener((view, x, y) -> {
View kp = binding.kp1;
if (kp.getVisibility() == View.VISIBLE) {
kp.setVisibility(View.GONE);
} else {
kp.setVisibility(View.VISIBLE);
}
});
}
//初始化播放器列表
if (bfqkz.xm != null) {
// main.handler.removeCallbacks(bfqkz.mt.updateSeekBar); // 在播放开始时启动更新进度
@ -118,14 +101,9 @@ public class Media{
}
}
private boolean isTablet(Context context) {
return (context.getResources().
getConfiguration().screenLayout &
Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE;
}
public static void loadLyric() {
if (lrcview == null || com.muqingbfq.bfq.lrc == null) {
if (bfq.lrcview == null || com.muqingbfq.bfq.lrc == null) {
return;
}
JSONObject jsonObject;
@ -137,7 +115,7 @@ public class Media{
} catch (Exception e) {
gj.sc(e);
}
lrcview.loadLrc(a, b);
bfq.lrcview.loadLrc(a, b);
}

View File

@ -6,15 +6,12 @@ import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
@ -22,19 +19,14 @@ import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;
import com.muqingbfq.databinding.ActivityHomeBinding;
import com.muqingbfq.fragment.Media;
import com.muqingbfq.fragment.bfq_db;
import com.muqingbfq.fragment.gd;
import com.muqingbfq.fragment.gd_adapter;
import com.muqingbfq.fragment.wode;
import com.muqingbfq.mq.gj;
import com.muqingbfq.mq.wj;
import java.util.ArrayList;
import java.util.List;
import me.wcy.lrcview.LrcView;
public class home extends AppCompatActivity {
@SuppressLint("StaticFieldLeak")
public static AppCompatActivity appCompatActivity;
@ -150,7 +142,6 @@ public class home extends AppCompatActivity {
String jsonList = new com.google.gson.Gson().toJson(bfqkz.list);
editor.putString("listData", jsonList);
editor.apply();
wj.setMP3ToFile(bfqkz.xm);
}

View File

@ -6,6 +6,7 @@ import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.net.Uri;
import android.util.DisplayMetrics;
import android.util.Log;
import android.widget.Toast;
@ -29,6 +30,18 @@ public class gj {
}
public static boolean isTablet(Context context) {
boolean b;
DisplayMetrics dm = context.getResources().getDisplayMetrics();
main.k = dm.widthPixels;
main.g = dm.heightPixels;
if (main.k > main.g) {
b = true;
} else {
b = false;
}
return b;
}
public static void sc(Object a) {
if (a == null) {
a = "null";