This commit is contained in:
yutou 2020-11-19 18:32:44 +08:00
parent 5d1114a2cb
commit 4e453392e7
6 changed files with 193 additions and 21 deletions

View File

@ -0,0 +1,65 @@
package com.yutou.nas_music_player.containers;
import android.content.ComponentName;
import android.content.Context;
import android.os.RemoteException;
import android.support.v4.media.MediaBrowserCompat;
import android.support.v4.media.session.MediaControllerCompat;
import com.yutou.nas_music_player.services.MusicService;
public class MediaBrowserHelper {
private MediaControllerCompat controllerCompat;
private MediaBrowserCompat mMediaBrowser;
private MediaBrowserConnectionCallback browserConnectionCallback;
private MediaControllerCallback controllerCallback;
private MediaBrowserSubscriptionCallback subscriptionCallback;
private Context context;
public MediaBrowserHelper(Context context) {
this.context = context;
browserConnectionCallback = new MediaBrowserConnectionCallback();
controllerCallback = new MediaControllerCallback();
subscriptionCallback=new MediaBrowserSubscriptionCallback();
mMediaBrowser = new MediaBrowserCompat(context, new ComponentName(context, MusicService.class), browserConnectionCallback, null);
mMediaBrowser.connect();
}
public void onStop(){
if(controllerCompat!=null){
controllerCompat.unregisterCallback(controllerCallback);
}
if(mMediaBrowser!=null&&mMediaBrowser.isConnected()){
mMediaBrowser.disconnect();
mMediaBrowser=null;
}
}
public MediaControllerCompat.TransportControls getTransportControls() {
return controllerCompat.getTransportControls();
}
private class MediaBrowserConnectionCallback extends MediaBrowserCompat.ConnectionCallback {
@Override
public void onConnected() {
super.onConnected();
try {
System.out.println("初始化:onConnected");
controllerCompat = new MediaControllerCompat(context, mMediaBrowser.getSessionToken());
controllerCompat.registerCallback(controllerCallback);
controllerCallback.onMetadataChanged(controllerCompat.getMetadata());
controllerCallback.onPlaybackStateChanged(controllerCompat.getPlaybackState());
mMediaBrowser.subscribe(mMediaBrowser.getRoot(), subscriptionCallback);
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
private class MediaControllerCallback extends MediaControllerCompat.Callback {
}
public class MediaBrowserSubscriptionCallback extends MediaBrowserCompat.SubscriptionCallback {
}
}

View File

@ -0,0 +1,4 @@
package com.yutou.nas_music_player.containers;
public class MediaNotificationManager {
}

View File

@ -1,6 +1,7 @@
package com.yutou.nas_music_player.containers; package com.yutou.nas_music_player.containers;
import android.media.MediaPlayer; import android.media.MediaPlayer;
import android.support.v4.media.MediaMetadataCompat;
import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
@ -17,12 +18,13 @@ public class MusicContainer {
private static MusicContainer container; private static MusicContainer container;
private MusicLibs libs; private MusicLibs libs;
private MediaPlayer mediaPlayer; private MediaPlayer mediaPlayer;
private int playIndex=0; private int playIndex = 0;
private MusicData nowPlayData = null;
private MusicContainer() { private MusicContainer() {
mediaPlayer=new MediaPlayer(); mediaPlayer = new MediaPlayer();
libs =new MusicLibs(); libs = new MusicLibs();
} }
public static MusicContainer getInstance() { public static MusicContainer getInstance() {
@ -31,18 +33,21 @@ public class MusicContainer {
} }
return container; return container;
} }
public void randomMusic(){
playIndex=new Random().nextInt(libs.mainData.size()); public void randomMusic() {
playIndex = new Random().nextInt(libs.mainData.size());
} }
public void playOfAllRandom(){
public void playOfAllRandom() {
randomMusic(); randomMusic();
play(libs.mainData.get(playIndex)); play(libs.mainData.get(playIndex));
} }
public void play(MusicData data){
String url= NetworkTool.NetworkAPI.HOME+ NetworkTool.NetworkAPI.MUSIC_PLAY public void play(MusicData data) {
+"?random=false&token="+ NetworkTool.NetworkAPI.HTTP_KEY+"&filePath="+data.getPlayFile(); String url = NetworkTool.NetworkAPI.HOME + NetworkTool.NetworkAPI.MUSIC_PLAY
+ "?random=false&token=" + NetworkTool.NetworkAPI.HTTP_KEY + "&filePath=" + data.getPlayFile();
try { try {
if(mediaPlayer.isPlaying()){ if (mediaPlayer.isPlaying()) {
mediaPlayer.stop(); mediaPlayer.stop();
mediaPlayer.reset(); mediaPlayer.reset();
} }
@ -53,13 +58,36 @@ public class MusicContainer {
e.printStackTrace(); e.printStackTrace();
} }
} }
public static MusicLibs getLibs(){
public MediaMetadataCompat getNowPlayMetadataCompat() {
if(nowPlayData==null){
return null;
}
return getMetadataCompat(nowPlayData);
}
public MediaMetadataCompat getMetadataCompat(MusicData data) {
MediaMetadataCompat.Builder builder = new MediaMetadataCompat.Builder();
builder.putString(MediaMetadataCompat.METADATA_KEY_MEDIA_ID, data.getTitle());
builder.putString(MediaMetadataCompat.METADATA_KEY_ARTIST, data.getArtist());
builder.putString(MediaMetadataCompat.METADATA_KEY_TITLE, data.getTitle());
builder.putString(MediaMetadataCompat.METADATA_KEY_ALBUM, data.getAlbum());
builder.putString(MediaMetadataCompat.METADATA_KEY_COMPOSER, data.getComposer());
builder.putLong(MediaMetadataCompat.METADATA_KEY_YEAR, Long.parseLong(data.getYear()));
builder.putLong(MediaMetadataCompat.METADATA_KEY_TRACK_NUMBER, Long.parseLong(data.getTrack()));
builder.putLong(MediaMetadataCompat.METADATA_KEY_DISC_NUMBER, Long.parseLong(data.getDisc_no()));
return builder.build();
}
public static MusicLibs getLibs() {
return getInstance().libs; return getInstance().libs;
} }
public static class MusicLibs{
public class MusicLibs {
private List<MusicData> mainData; private List<MusicData> mainData;
private MusicLibs(){
mainData=new ArrayList<>(); private MusicLibs() {
mainData = new ArrayList<>();
initData(); initData();
} }
@ -67,13 +95,14 @@ public class MusicContainer {
NetworkTool.init().httpGet(NetworkTool.NetworkAPI.MUSIC_ALL, new JSONObject(), new NetworkInterface() { NetworkTool.init().httpGet(NetworkTool.NetworkAPI.MUSIC_ALL, new JSONObject(), new NetworkInterface() {
@Override @Override
public void httpGetData(Object data, int state) { public void httpGetData(Object data, int state) {
JSONObject json=JSONObject.parseObject(data.toString()); JSONObject json = JSONObject.parseObject(data.toString());
if(json.getInteger("code")==0){ if (json.getInteger("code") == 0) {
mainData= JSONArray.parseArray(json.getJSONArray("data").toJSONString(),MusicData.class); mainData = JSONArray.parseArray(json.getJSONArray("data").toJSONString(), MusicData.class);
System.out.println(mainData.size()); System.out.println(mainData.size());
for (MusicData musicData : mainData) { if (mainData.size() > 0) {
System.out.println(musicData.getTitle()); nowPlayData = mainData.get(0);
} }
System.out.println("获取列表完成");
} }
} }

View File

@ -3,6 +3,8 @@ package com.yutou.nas_music_player.containers;
import android.util.Base64; import android.util.Base64;
import com.yutou.nas_music_player.tools.StringUtil;
public class MusicData { public class MusicData {
private String artist;//艺术家 private String artist;//艺术家
private String album;//专辑 private String album;//专辑
@ -53,6 +55,9 @@ public class MusicData {
} }
public String getYear() { public String getYear() {
if(StringUtil.isEmpty(year)){
year="8888";
}
return year; return year;
} }
@ -61,6 +66,9 @@ public class MusicData {
} }
public String getTrack() { public String getTrack() {
if(StringUtil.isEmpty(track)){
return "0";
}
return track; return track;
} }
@ -69,6 +77,9 @@ public class MusicData {
} }
public String getDisc_no() { public String getDisc_no() {
if(StringUtil.isEmpty(disc_no)){
return "0";
}
return disc_no; return disc_no;
} }

View File

@ -5,23 +5,74 @@ import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.os.IBinder; import android.os.IBinder;
import android.support.v4.media.MediaBrowserCompat; import android.support.v4.media.MediaBrowserCompat;
import android.support.v4.media.MediaMetadataCompat;
import android.support.v4.media.session.MediaSessionCompat;
import android.support.v4.media.session.PlaybackStateCompat;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.media.MediaBrowserServiceCompat; import androidx.media.MediaBrowserServiceCompat;
import com.yutou.nas_music_player.containers.MusicContainer;
import com.yutou.nas_music_player.containers.MusicData;
import com.yutou.nas_music_player.tools.AppData;
import java.util.ArrayList;
import java.util.List; import java.util.List;
public class MusicService extends MediaBrowserServiceCompat { public class MusicService extends MediaBrowserServiceCompat {
private MediaSessionCompat mediaSession;
private PlaybackStateCompat.Builder stateBuilder;
@Override
public void onCreate() {
super.onCreate();
mediaSession = new MediaSessionCompat(getApplicationContext(), "MEDIA_LOG");
mediaSession.setFlags(
MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS |
MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
stateBuilder = new PlaybackStateCompat.Builder()
.setActions(
PlaybackStateCompat.ACTION_PLAY |
PlaybackStateCompat.ACTION_PLAY_PAUSE);
mediaSession.setPlaybackState(stateBuilder.build());
mediaSession.setCallback(new MediaSessionCallback());
setSessionToken(mediaSession.getSessionToken());
}
@Nullable @Nullable
@Override @Override
public BrowserRoot onGetRoot(@NonNull String clientPackageName, int clientUid, @Nullable Bundle rootHints) { public BrowserRoot onGetRoot(@NonNull String clientPackageName, int clientUid, @Nullable Bundle rootHints) {
return new BrowserRoot("media_root_id",null); return new BrowserRoot("media_root_id", null);
} }
@Override @Override
public void onLoadChildren(@NonNull String parentId, @NonNull Result<List<MediaBrowserCompat.MediaItem>> result) { public void onLoadChildren(@NonNull String parentId, @NonNull Result<List<MediaBrowserCompat.MediaItem>> result) {
List<MediaBrowserCompat.MediaItem> mediaItems = new ArrayList<>();
MediaMetadataCompat compat=MusicContainer.getInstance().getNowPlayMetadataCompat();
System.out.println(compat);
if(compat!=null){
MediaBrowserCompat.MediaItem mediaItem=new MediaBrowserCompat.MediaItem(compat.getDescription(), MediaBrowserCompat.MediaItem.FLAG_PLAYABLE);
mediaItems.add(mediaItem);
}
result.sendResult(mediaItems);
}
public class MediaSessionCallback extends MediaSessionCompat.Callback {
@Override
public void onPlay() {
super.onPlay();
System.out.println("接收到播放指令");
MediaMetadataCompat metadataCompat=MusicContainer.getInstance().getNowPlayMetadataCompat();
mediaSession.setMetadata(metadataCompat);
mediaSession.setActive(true);
}
@Override
public void onPause() {
super.onPause();
}
} }
} }

View File

@ -7,21 +7,33 @@ import android.view.View;
import android.widget.ImageButton; import android.widget.ImageButton;
import com.yutou.nas_music_player.R; 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.containers.MusicContainer;
public class MainActivity extends AppCompatActivity { public class MainActivity extends AppCompatActivity {
private ImageButton play,previous,next; private ImageButton play,previous,next;
private MediaBrowserHelper browserHelper;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); setContentView(R.layout.activity_main);
MusicContainer.getInstance(); MusicContainer.getInstance();
play=findViewById(R.id.play); play=findViewById(R.id.play);
next=findViewById(R.id.next);
play.setOnClickListener(new View.OnClickListener() { play.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
MusicContainer.getInstance().playOfAllRandom(); browserHelper=new MediaBrowserHelper(MainActivity.this);
} }
}); });
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
browserHelper.getTransportControls().play();
}
});
// browserHelper=new MediaBrowserHelper(this);
} }
} }