fix:添加到列表播放时通知栏没更新

This commit is contained in:
Yutousama 2022-06-12 23:52:49 +08:00
parent 3aef3c1504
commit 578f756635
6 changed files with 47 additions and 21 deletions

View File

@ -378,7 +378,7 @@ public class MusicLibsAdapter extends RecyclerView.Adapter<MusicLibsAdapter.View
public void bind(MusicData data) {
title.setText(data.getTitle());
artist.setText(data.getArtist());
icon.setImageResource(R.drawable.ic_dir);
//icon.setImageResource(R.drawable.ic_dir);
collection.setVisibility(View.VISIBLE);
if (!data.isDir()) {
message.setText(data.getBitRate() + "kbps | " + data.getSampleRate() + "hz | " + data.getEncodingType());
@ -476,6 +476,9 @@ public class MusicLibsAdapter extends RecyclerView.Adapter<MusicLibsAdapter.View
@Override
public void httpGetData(Object data, int state) {
Glide.with(MyApplication.application).load((Bitmap) data)
.placeholder(holder.icon.getDrawable())
.dontAnimate()
.skipMemoryCache(false)
.apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(25, 3)))
.into(holder.icon);
}
@ -519,7 +522,8 @@ public class MusicLibsAdapter extends RecyclerView.Adapter<MusicLibsAdapter.View
AppTools.toast(i + "首歌已添加到队列中");
popupWindow.dismiss();
MusicContainer.getInstance().setPlayModel(MusicContainer.PLAY_MODEL_ORDER);
MusicContainer.getInstance().playNext();
browserHelper.getTransportControls().play();
browserHelper.getTransportControls().skipToNext();
}
@Override

View File

@ -28,10 +28,12 @@ import java.util.ArrayList;
import java.util.List;
public class MusicService extends MediaBrowserServiceCompat {
private final static String TAG="MusicService";
public MediaSessionCompat mediaSession;
private PlaybackStateCompat.Builder stateBuilder;
private MediaNotificationManager notificationManager;
private int mState = PlaybackStateCompat.STATE_PLAYING;
private String playMd5="";
private static final long MEDIA_SESSION_ACTIONS = PlaybackStateCompat.ACTION_PLAY
| PlaybackStateCompat.ACTION_PAUSE
@ -61,12 +63,19 @@ public class MusicService extends MediaBrowserServiceCompat {
MusicContainer.getInstance().addCompletionListener(new PlayCompletionListener());
MediaBrowserHelper helper = new MediaBrowserHelper(this);
helper.regPlayListener(new MediaControllerCompat.Callback() {
private String compat_md5="";
private MediaMetadataCompat compat;
@Override
public void onMetadataChanged(MediaMetadataCompat metadata) {
super.onMetadataChanged(metadata);
if(metadata==null){
return;
}
String md5=metadata.getString("md5");
if(compat_md5.equals(md5)){
return;
}
compat_md5=md5;
Notification notification = notificationManager.getNotification(metadata, builderState(0).build(), getSessionToken());
notificationManager.getManager()
.notify(MediaNotificationManager.NOTIFICATION_ID, notification);
@ -107,7 +116,12 @@ public class MusicService extends MediaBrowserServiceCompat {
}
public void seekToNotification(long pos) {
MusicData data = MusicContainer.getInstance().getNowPlayData();
if(!playMd5.equals(data.getMd5())){
playMd5=data.getMd5();
mediaSession.setMetadata(MusicContainer.getInstance().getNowPlayMetadataCompat());
updateNotification();
}
mediaSession.setPlaybackState(new PlaybackStateCompat.Builder()
.setActions(MEDIA_SESSION_ACTIONS)
.setState(mState, pos, 1.0f, SystemClock.elapsedRealtime()).build());
@ -155,7 +169,7 @@ public class MusicService extends MediaBrowserServiceCompat {
MediaMetadataCompat metadataCompat = MusicContainer.getInstance().getNowPlayMetadataCompat();
System.out.println("更新通知栏:" + metadataCompat.getString(MediaMetadataCompat.METADATA_KEY_MEDIA_ID));
mediaSession.setMetadata(metadataCompat);
//mediaSession.setMetadata(metadataCompat);
mediaSession.setActive(true);
Notification notification =
notificationManager.getNotification(
@ -166,10 +180,7 @@ public class MusicService extends MediaBrowserServiceCompat {
public class MediaSessionCallback extends MediaSessionCompat.Callback {
MusicReceiver receiver = new MusicReceiver();
;
IntentFilter intentFilter = new IntentFilter(AudioManager.ACTION_AUDIO_BECOMING_NOISY);
;
@Override
public void onPlay() {
@ -191,6 +202,7 @@ public class MusicService extends MediaBrowserServiceCompat {
super.onPause();
mState = PlaybackStateCompat.STATE_PAUSED;
MusicContainer.getInstance().pause();
//MusicService.this.onSeekTo(-1);
}

View File

@ -322,8 +322,8 @@ public class MainActivity extends AppCompatActivity {
@Override
public void onPlaybackStateChanged(PlaybackStateCompat state) {
super.onPlaybackStateChanged(state);
//System.out.println("播放状态变化:" + state.getPosition() + " " + seekBar.getMax());
if (state.getState() == PlaybackStateCompat.STATE_PLAYING) {
//System.out.println("[MainActivity]播放状态变化:" + state.getPosition() + " " + seekBar.getMax()+" state.getState() = "+state.getState()+" isPlay = "+browserHelper.isPlayer());
if (state.getState() == PlaybackStateCompat.STATE_PLAYING || browserHelper.isPlayer()) {
int duration = (int) (state.getPosition() / 1000);
int minutes = duration / 60;
int seconds = duration % 60;
@ -343,7 +343,7 @@ public class MainActivity extends AppCompatActivity {
super.onMetadataChanged(metadata);
String md5 = null;
try {
System.out.println("播放歌曲变换:" + metadata.getString("md5"));
System.out.println("[MainActivity]播放歌曲变换:" + metadata.getString("md5"));
md5 = metadata.getString("md5");
} catch (Exception ignored) {
}

View File

@ -178,6 +178,7 @@ public class PlayLibsActivity extends AppCompatActivity {
private void setImage(Bitmap data) {
Glide.with(this).load(data)
.placeholder(album_image.getDrawable())
.apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(25, 3)))
.into(album_image);
}
@ -217,7 +218,7 @@ public class PlayLibsActivity extends AppCompatActivity {
@Override
public void onPlaybackStateChanged(PlaybackStateCompat state) {
super.onPlaybackStateChanged(state);
if (state.getState() == PlaybackStateCompat.STATE_PLAYING) {
if (state.getState() == PlaybackStateCompat.STATE_PLAYING || browserHelper.isPlayer()) {
progressBar.setProgress((int) (state.getPosition()));
play.setImageResource(android.R.drawable.ic_media_pause);
} else {

View File

@ -1,11 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipChildren="false"
android:descendantFocusability="blocksDescendants"
android:orientation="vertical">
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
@ -36,7 +38,8 @@
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
android:orientation="horizontal"
tools:visibility="visible">
<LinearLayout
android:layout_width="120dp"
@ -55,8 +58,7 @@
android:shadowDy="5"
android:shadowRadius="1"
android:text="TextView"
android:textColor="@color/colorBlack"
/>
android:textColor="@color/colorBlack" />
<cc.ibooker.ztextviewlib.MarqueeTextView
android:id="@+id/artist"
@ -70,8 +72,7 @@
android:shadowRadius="1"
android:text="TextView"
android:textColor="@color/colorBlack"
android:textSize="10sp"
/>
android:textSize="10sp" />
<cc.ibooker.ztextviewlib.MarqueeTextView
android:id="@+id/message"
@ -85,8 +86,7 @@
android:shadowRadius="1"
android:text="TextView"
android:textColor="@color/colorBlack"
android:textSize="8sp"
/>
android:textSize="8sp" />
</LinearLayout>
<LinearLayout
@ -95,7 +95,8 @@
android:layout_height="match_parent"
android:layout_gravity="end"
android:gravity="end"
android:orientation="horizontal">
android:orientation="horizontal"
tools:visibility="visible">
<ImageButton
android:id="@+id/dislike"
@ -122,7 +123,15 @@
android:scaleType="fitCenter"
app:srcCompat="@drawable/ic_top" />
</LinearLayout>
</LinearLayout>
<Button
android:id="@+id/button"
android:layout_width="60dp"
android:layout_height="match_parent"
android:text="Button" />
</LinearLayout>
</LinearLayout>

View File

@ -7,7 +7,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:7.2.0'
classpath 'com.android.tools.build:gradle:7.2.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.21"