优化:点击媒体通知可以跳到播放界面

优化:播放界面支持左右手势播放
修复:播放界面状态栏沉浸模式
修复:添加到队列时播放文件夹导致闪退
修复:媒体通知栏数据不准确的问题
修复:部分图片过大时导致闪退
This commit is contained in:
Yutousama 2021-08-17 05:45:05 +08:00
parent f38486e59a
commit 6d2426d35c
8 changed files with 101 additions and 36 deletions

View File

@ -25,6 +25,7 @@
<activity
android:name=".views.MainActivity"
android:fitsSystemWindows="true"
android:launchMode="singleInstance"
android:theme="@style/Theme.AppCompat.DayNight.NoActionBar" />
<activity
android:name=".views.PlayLibsActivity"

View File

@ -360,7 +360,6 @@ public class MusicLibsAdapter extends ArrayAdapter<MusicData> {
public void httpGetData(Object data, int state) {
List<MusicData> list = (List<MusicData>) data;
for (MusicData musicData : list) {
System.out.println("加入到临时列表:" + musicData.getTitle());
MusicContainer.getInstance().addTmpList(musicData);
hud.dismiss();
}

View File

@ -26,6 +26,7 @@ import com.yutou.nas_music_player.services.MusicService;
import com.yutou.nas_music_player.views.MainActivity;
public class MediaNotificationManager {
public static MediaNotificationManager manager;
public static final int NOTIFICATION_ID = 412;
private static final String CHANNEL_ID = MyApplication.application.getPackageName() + ".music_channel";
private MusicService mService;
@ -35,8 +36,15 @@ public class MediaNotificationManager {
private NotificationCompat.Action mPrevAction;
private NotificationManager mNotificationManager;
public static MediaNotificationManager getInstance(){
return manager;
}
public MusicService getService(){
return mService;
}
public MediaNotificationManager(MusicService musicService) {
this.mService = musicService;
manager=this;
mNotificationManager =
(NotificationManager) mService.getSystemService(Context.NOTIFICATION_SERVICE);
@ -105,6 +113,7 @@ public class MediaNotificationManager {
if(metadata.getBitmap(MediaMetadataCompat.METADATA_KEY_DISPLAY_ICON)!=null){
builder.setLargeIcon(metadata.getBitmap(MediaMetadataCompat.METADATA_KEY_DISPLAY_ICON));
}
builder.setOngoing(true);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentIntent(mService.mediaSession.getController().getSessionActivity());
builder.setContentTitle(description.getTitle());
@ -130,7 +139,7 @@ public class MediaNotificationManager {
CharSequence name = "NAS音乐播放器";
// The user-visible description of the channel.
String description = "NAS音乐播放器通知栏";
int importance = NotificationManager.IMPORTANCE_LOW;
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
// Configure the notification channel.
mChannel.setDescription(description);
@ -138,9 +147,9 @@ public class MediaNotificationManager {
// Sets the notification light color for notifications posted to this
// channel, if the device supports this feature.
mChannel.setLightColor(Color.RED);
mChannel.enableVibration(true);
mChannel.setVibrationPattern(
new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
mChannel.enableVibration(false);
/* mChannel.setVibrationPattern(
new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});*/
mNotificationManager.createNotificationChannel(mChannel);
}
}

View File

@ -1,15 +1,13 @@
package com.yutou.nas_music_player.containers;
import static android.media.MediaPlayer.SEEK_PREVIOUS_SYNC;
import android.content.Context;
import android.content.DialogInterface;
import android.media.MediaPlayer;
import android.os.Build;
import android.support.v4.media.MediaMetadataCompat;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
@ -33,12 +31,9 @@ import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import static android.media.MediaPlayer.SEEK_PREVIOUS_SYNC;
public class MusicContainer {
public static final int PLAY_MODEL_RANDOM = 0;//随机
@ -148,6 +143,9 @@ public class MusicContainer {
public static MusicContainer getInstance(MusicLibsInitInterface initInterface) {
if (container == null) {
container = new MusicContainer(initInterface);
}else{
if(initInterface!=null)
initInterface.init();
}
return container;
}
@ -253,6 +251,10 @@ public class MusicContainer {
}, 0, 1000);
}
ConfigTools.getPreferences().edit().putString(ConfigTools.previous_music, JSONObject.toJSONString(data)).apply();
MediaNotificationManager manager=MediaNotificationManager.getInstance();
if(manager!=null){
manager.getService().updateNotification();
}
} catch (IOException e) {
e.printStackTrace();
}
@ -311,7 +313,7 @@ public class MusicContainer {
builder.putLong(MediaMetadataCompat.METADATA_KEY_DISC_NUMBER, Long.parseLong(data.getDisc_no()));
builder.putLong(MediaMetadataCompat.METADATA_KEY_DURATION, ((int) (data.getDurationAsDouble() * 1000)));
if (data.getImg(-1, -1) != null) {
builder.putBitmap(MediaMetadataCompat.METADATA_KEY_DISPLAY_ICON, data.getImg(-1, -1));
builder.putBitmap(MediaMetadataCompat.METADATA_KEY_DISPLAY_ICON, data.getImg(1024, 1024));
}
builder.putString("md5", data.getMd5());
metadataCompat = builder.build();
@ -454,6 +456,9 @@ public class MusicContainer {
}
public void addTmpList(MusicData data) {
if(data.getTitle().equals("返回")&&data.getTrack().equals("-1")||data.isDir()){
return;
}
tmpList.add(data);
}

View File

@ -1,6 +1,8 @@
package com.yutou.nas_music_player.services;
import android.app.Notification;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.AudioManager;
import android.media.MediaPlayer;
@ -18,6 +20,7 @@ import androidx.media.MediaBrowserServiceCompat;
import com.yutou.nas_music_player.containers.MediaNotificationManager;
import com.yutou.nas_music_player.containers.MusicContainer;
import com.yutou.nas_music_player.Datas.MusicData;
import com.yutou.nas_music_player.views.MainActivity;
import java.util.ArrayList;
import java.util.List;
@ -49,6 +52,9 @@ public class MusicService extends MediaBrowserServiceCompat {
.setActions(MEDIA_SESSION_ACTIONS);
mediaSession.setPlaybackState(stateBuilder.build());
mediaSession.setCallback(new MediaSessionCallback());
Intent intent=new Intent(this, MainActivity.class);
PendingIntent pi=PendingIntent.getActivity(this,233,intent,PendingIntent.FLAG_UPDATE_CURRENT);
mediaSession.setSessionActivity(pi);
setSessionToken(mediaSession.getSessionToken());
MusicContainer.getInstance().addCompletionListener(new PlayCompletionListener());
}
@ -119,23 +125,23 @@ public class MusicService extends MediaBrowserServiceCompat {
}
return actions;
}
public void updateNotification() {
MediaMetadataCompat metadataCompat = MusicContainer.getInstance().getNowPlayMetadataCompat();
System.out.println("更新通知栏:"+metadataCompat.getString(MediaMetadataCompat.METADATA_KEY_MEDIA_ID));
mediaSession.setMetadata(metadataCompat);
mediaSession.setActive(true);
Notification notification =
notificationManager.getNotification(
metadataCompat, builderState(0).build(), getSessionToken());
notificationManager.getManager()
.notify(MediaNotificationManager.NOTIFICATION_ID, notification);
}
public class MediaSessionCallback extends MediaSessionCompat.Callback {
MusicReceiver receiver = new MusicReceiver();;
IntentFilter intentFilter = new IntentFilter(AudioManager.ACTION_AUDIO_BECOMING_NOISY);;
public void updateNotification() {
MediaMetadataCompat metadataCompat = MusicContainer.getInstance().getNowPlayMetadataCompat();
System.out.println("更新通知栏:"+metadataCompat.getString(MediaMetadataCompat.METADATA_KEY_MEDIA_ID));
mediaSession.setMetadata(metadataCompat);
mediaSession.setActive(true);
Notification notification =
notificationManager.getNotification(
metadataCompat, builderState(0).build(), getSessionToken());
notificationManager.getManager()
.notify(MediaNotificationManager.NOTIFICATION_ID, notification);
}
@Override
public void onPlay() {

View File

@ -3,6 +3,7 @@ package com.yutou.nas_music_player.tools;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.util.Base64;
import android.widget.Toast;
@ -14,9 +15,8 @@ import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.yutou.nas_music_player.MyApplication;
import com.yutou.nas_music_player.R;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.security.MessageDigest;
public class AppTools {
@ -43,8 +43,18 @@ public class AppTools {
if(path==null){
return null;
}
File img=new File(path);
if(img.length()>3145728){
Bitmap bitmap=BitmapFactory.decodeFile(path);
return compressQuality(bitmap);
}
return BitmapFactory.decodeFile(path);
}
private static Bitmap compressQuality(Bitmap bm) {
Matrix matrix = new Matrix();
matrix.setScale(0.5f, 0.5f);
return Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);
}
public static String getMD5(String data) {
try {
MessageDigest digest = MessageDigest.getInstance("MD5");

View File

@ -1,9 +1,5 @@
package com.yutou.nas_music_player.views;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityOptionsCompat;
import androidx.core.util.Pair;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
@ -13,28 +9,31 @@ import android.support.v4.media.MediaMetadataCompat;
import android.support.v4.media.session.MediaControllerCompat;
import android.support.v4.media.session.MediaSessionCompat;
import android.support.v4.media.session.PlaybackStateCompat;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityOptionsCompat;
import androidx.core.util.Pair;
import com.alibaba.fastjson.JSON;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.kaopiz.kprogresshud.KProgressHUD;
import com.yutou.nas_music_player.Datas.MusicData;
import com.yutou.nas_music_player.R;
import com.yutou.nas_music_player.containers.MediaBrowserHelper;
import com.yutou.nas_music_player.containers.MusicContainer;
import com.yutou.nas_music_player.Datas.MusicData;
import com.yutou.nas_music_player.tools.AppData;
import com.yutou.nas_music_player.tools.AppTools;
import com.yutou.nas_music_player.tools.ConfigTools;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Timer;
@ -52,10 +51,10 @@ public class MainActivity extends AppCompatActivity {
private TextView positionTime, durationTime, bitRate;
private SeekBar seekBar;
private Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
super.onCreate(savedInstanceState);
setBarColor(android.R.color.transparent);
setContentView(R.layout.activity_main);
@ -301,6 +300,41 @@ public class MainActivity extends AppCompatActivity {
finish();
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if(keyCode==KeyEvent.KEYCODE_BACK){
moveTaskToBack(true);
return false;
}
return super.onKeyUp(keyCode, event);
}
float downX=0;
float downY=0;
@Override
public boolean onTouchEvent(MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_DOWN){
downX=event.getRawX();
downY=event.getRawY();
}else if (event.getAction()==MotionEvent.ACTION_UP){
pass(event.getRawX(),event.getRawY());
}
return super.onTouchEvent(event);
}
private void pass(float upX,float upY){
boolean isOkY=Math.abs(upY-downY)<=200;
if(upX-downX>200&&isOkY){
previous.callOnClick();
}else if (upX-downX<-200&&isOkY){
next.callOnClick();
}
}
private class PlayListener extends MediaControllerCompat.Callback {
@SuppressLint("SetTextI18n")
@Override

View File

@ -22,6 +22,7 @@ public class OpenActivity extends AppCompatActivity {
@Override
public void init() {
Intent intent=new Intent(OpenActivity.this,MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
OpenActivity.this.finish();
}