update
This commit is contained in:
parent
5d1114a2cb
commit
4e453392e7
@ -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 {
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,4 @@
|
||||
package com.yutou.nas_music_player.containers;
|
||||
|
||||
public class MediaNotificationManager {
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
package com.yutou.nas_music_player.containers;
|
||||
|
||||
import android.media.MediaPlayer;
|
||||
import android.support.v4.media.MediaMetadataCompat;
|
||||
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
@ -18,6 +19,7 @@ public class MusicContainer {
|
||||
private MusicLibs libs;
|
||||
private MediaPlayer mediaPlayer;
|
||||
private int playIndex = 0;
|
||||
private MusicData nowPlayData = null;
|
||||
|
||||
|
||||
private MusicContainer() {
|
||||
@ -31,13 +33,16 @@ public class MusicContainer {
|
||||
}
|
||||
return container;
|
||||
}
|
||||
|
||||
public void randomMusic() {
|
||||
playIndex = new Random().nextInt(libs.mainData.size());
|
||||
}
|
||||
|
||||
public void playOfAllRandom() {
|
||||
randomMusic();
|
||||
play(libs.mainData.get(playIndex));
|
||||
}
|
||||
|
||||
public void play(MusicData data) {
|
||||
String url = NetworkTool.NetworkAPI.HOME + NetworkTool.NetworkAPI.MUSIC_PLAY
|
||||
+ "?random=false&token=" + NetworkTool.NetworkAPI.HTTP_KEY + "&filePath=" + data.getPlayFile();
|
||||
@ -53,11 +58,34 @@ public class MusicContainer {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
public static class MusicLibs{
|
||||
|
||||
public class MusicLibs {
|
||||
private List<MusicData> mainData;
|
||||
|
||||
private MusicLibs() {
|
||||
mainData = new ArrayList<>();
|
||||
initData();
|
||||
@ -71,9 +99,10 @@ public class MusicContainer {
|
||||
if (json.getInteger("code") == 0) {
|
||||
mainData = JSONArray.parseArray(json.getJSONArray("data").toJSONString(), MusicData.class);
|
||||
System.out.println(mainData.size());
|
||||
for (MusicData musicData : mainData) {
|
||||
System.out.println(musicData.getTitle());
|
||||
if (mainData.size() > 0) {
|
||||
nowPlayData = mainData.get(0);
|
||||
}
|
||||
System.out.println("获取列表完成");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -3,6 +3,8 @@ package com.yutou.nas_music_player.containers;
|
||||
|
||||
import android.util.Base64;
|
||||
|
||||
import com.yutou.nas_music_player.tools.StringUtil;
|
||||
|
||||
public class MusicData {
|
||||
private String artist;//艺术家
|
||||
private String album;//专辑
|
||||
@ -53,6 +55,9 @@ public class MusicData {
|
||||
}
|
||||
|
||||
public String getYear() {
|
||||
if(StringUtil.isEmpty(year)){
|
||||
year="8888";
|
||||
}
|
||||
return year;
|
||||
}
|
||||
|
||||
@ -61,6 +66,9 @@ public class MusicData {
|
||||
}
|
||||
|
||||
public String getTrack() {
|
||||
if(StringUtil.isEmpty(track)){
|
||||
return "0";
|
||||
}
|
||||
return track;
|
||||
}
|
||||
|
||||
@ -69,6 +77,9 @@ public class MusicData {
|
||||
}
|
||||
|
||||
public String getDisc_no() {
|
||||
if(StringUtil.isEmpty(disc_no)){
|
||||
return "0";
|
||||
}
|
||||
return disc_no;
|
||||
}
|
||||
|
||||
|
@ -5,14 +5,41 @@ import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.os.IBinder;
|
||||
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.Nullable;
|
||||
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;
|
||||
|
||||
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
|
||||
@Override
|
||||
@ -22,6 +49,30 @@ public class MusicService extends MediaBrowserServiceCompat {
|
||||
|
||||
@Override
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -7,21 +7,33 @@ import android.view.View;
|
||||
import android.widget.ImageButton;
|
||||
|
||||
import com.yutou.nas_music_player.R;
|
||||
import com.yutou.nas_music_player.containers.MediaBrowserHelper;
|
||||
import com.yutou.nas_music_player.containers.MusicContainer;
|
||||
|
||||
public class MainActivity extends AppCompatActivity {
|
||||
private ImageButton play,previous,next;
|
||||
private MediaBrowserHelper browserHelper;
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_main);
|
||||
MusicContainer.getInstance();
|
||||
play=findViewById(R.id.play);
|
||||
next=findViewById(R.id.next);
|
||||
play.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
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);
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user