1.喜马拉雅开放平台注册/审核后,在“我的应用”中创建应用
2.添加内容信息
3.我使用的是android studio androidmainfest中配置
权限:
service/receiver:
android {
...
productFlavors{
arm{
ndk{
abiFilters "armeabi" ,"armeabi-v7a"
}
}
}
}
-dontwarn okio.**
-keep class okio.** { *;}
-dontwarn okhttp3.**
-keep class okhttp3.** { *;}
-dontwarn com.google.gson.**
-keep class com.google.gson.** { *;}
-dontwarn android.support.**
-keep class android.support.** { *;}
-dontwarn com.ximalaya.ting.android.player.**
-keep class com.ximalaya.ting.android.player.** { *;}
-dontwarn com.ximalaya.ting.android.opensdk.**
-keep interface com.ximalaya.ting.android.opensdk.** {*;}
-keep class com.ximalaya.ting.android.opensdk.** { *; }
CommonRequest.getInstanse().init(context, appSecret);
private void initXmly() {
private RequestTracker requestTracker = new RequestTracker() {
@Override
public void onWaiting(RequestParams params) {
Logger.log("TingApplication : onWaiting " + params);
}
@Override
public void onStart(RequestParams params) {
Logger.log("TingApplication : onStart " + params);
}
@Override
public void onRequestCreated(UriRequest request) {
Logger.log("TingApplication : onRequestCreated " + request);
}
@Override
public void onSuccess(UriRequest request, Object result) {
Logger.log("TingApplication : onSuccess " + request + " result = " + result);
}
@Override
public void onRemoved(UriRequest request) {
Logger.log("TingApplication : onRemoved " + request);
}
@Override
public void onCancelled(UriRequest request) {
Logger.log("TingApplication : onCanclelled " + request);
}
@Override
public void onError(UriRequest request, Throwable ex, boolean isCallbackError) {
Logger.log("TingApplication : onError " + request + " ex = " + ex + " isCallbackError = " + isCallbackError);
}
@Override
public void onFinished(UriRequest request) {
Logger.log("TingApplication : onFinished " + request);
}
};
public class DeviceXmlyPlayService extends Service {
private XmPlayerManager mPlayerManager;
private final String TAG=DeviceXmlyPlayService.class.getSimpleName();
private Intent it;
public class MyPlayBinder extends Binder {
public DeviceXmlyPlayService getService(){
return DeviceXmlyPlayService.this;
}
}
private MyPlayBinder binder=new MyPlayBinder();
@Override
public void onCreate() {
super.onCreate();
initPlayData();
it=new Intent(CustomStaticCode.XMLY_PLAYER_RECEIVER);
}
private void initPlayData() {
mPlayerManager = XmPlayerManager.getInstance(this);
Notification mNotification = XmNotificationCreater.getInstanse(this).initNotification(this, DcviceStoryDetalActivity.class);
mNotification.icon=R.drawable.mzlogo;
mPlayerManager.init((int) System.currentTimeMillis(), mNotification);
mPlayerManager.addPlayerStatusListener(mPlayerStatusListener);
mPlayerManager.setOnConnectedListerner(new XmPlayerManager.IConnectListener() {
@Override
public void onConnected() {
mPlayerManager.setPlayMode(XmPlayListControl.PlayMode.PLAY_MODEL_LIST_LOOP);
// ToastUtils.toast("初始化播放器成功");
}
});
XmPlayerManager.getInstance(this).setCommonBusinessHandle(XmDownloadManager.getInstance());
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return binder;
}
public void playTracks(List
public class XMLYTingUtil {
private Handler handler;
private String TAG="XMLYTingUtilLOG";
public XMLYTingUtil(Context context, Handler handler) {
this.handler=handler;
}
//获取全部频道 (暂不用)
public void getCategories(){
CommonRequest.getCategories(null, new IDataCallBack() {
@Override
public void onSuccess(CategoryList object) {
List categories = object.getCategories();
for (int i = 0; i < categories.size(); i++) {
Category c=categories.get(i);
}
}
@Override
public void onError(int code, String message) {
Log.e(TAG, "onCreate: code = [" + code + "], message = [" + message + "]");
// LogUtils.log("code = [" + code + "], message = [" + message + "]");
}
});
}
/**
* 获取对应儿童节目下的便签
* @param type 0-专辑标签,1-声音标签
*/
public void getChildTag(String type){
Map mapx = new HashMap();
mapx.put(DTransferConstants.CATEGORY_ID, CustomStaticCode.CATEGORY_ID+"");
mapx.put(DTransferConstants.TYPE, type.equals("") ? "0":type);
CommonRequest.getTags(mapx, new IDataCallBack() {
@Override
public void onSuccess(TagList tagList) {
List tlist = tagList.getTagList();
Message msg=Message.obtain();
msg.obj=tlist;
msg.arg1=999;
msg.what=CustomStaticCode.MESSAGE_CHILD_TAG;
handler.sendMessage(msg);
}
@Override
public void onError(int i, String s) {
Message msg=Message.obtain();
msg.obj=s;
msg.arg1=i;
msg.what=CustomStaticCode.MESSAGE_CHILD_TAG;
handler.sendMessage(msg);
}
});
}
/**
* 获取标签下的专辑
* @param tagName 分类名称
* @param page 页码 默认为1
* @param calc 最火(1),最新(2),经典或播放最多(3) 默认最火
*/
public void getAlbumList(String tagName,String page,String calc){
Map map = new HashMap();
map.put(DTransferConstants.CATEGORY_ID, "" + CustomStaticCode.CATEGORY_ID);
map.put(DTransferConstants.TAG_NAME, tagName);
map.put(DTransferConstants.PAGE, page.equals("") ? "1":page);
map.put(DTransferConstants.CALC_DIMENSION,calc.equals("") ? "1":calc);
CommonRequest.getAlbumList(map, new IDataCallBack() {
@Override
public void onSuccess(AlbumList albumList) {
List albums = albumList.getAlbums();
Message msg=Message.obtain();
msg.obj=albums;
msg.arg1=999;
msg.what=CustomStaticCode.MESSAGE_ALBUM_TAG;
handler.sendMessage(msg);
}
@Override
public void onError(int i, String s) {
Message msg=Message.obtain();
msg.obj=s;
msg.arg1=i;
msg.what=CustomStaticCode.MESSAGE_ALBUM_TAG;
handler.sendMessage(msg);
}
});
}
/**
* 获取音频
* @param albumId 专辑ID
* @param sort 排序
* @param page 页码
*/
public void getTrack(String albumId,String sort,String page){
Map maps = new HashMap();
maps.put(DTransferConstants.ALBUM_ID, albumId);
maps.put(DTransferConstants.SORT, sort.equals("") ? "asc" : sort);
maps.put(DTransferConstants.PAGE, page.equals("") ? "1":page);
CommonRequest.getTracks(maps, new IDataCallBack() {
@Override
public void onSuccess(TrackList trackList) {
List
CATEGORY_ID=6 //儿童频道
如果想查询对应的频道分类ID的话 可以参考
getCategories这个方法来查阅对应id
private TabLayout dcStoryMainTablayout;
private ViewPager dcStoryMainViewpager;
private XMLYTingUtil xmUtil;
private ArrayList tlist;
private ImageView dcStoryMainPlay;
private ServiceConnection conn;
private DeviceXmlyPlayService mservice;
private UpdateImageReceiver updateImageReceiver;
private Handler handler=new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
int what = msg.what;
switch (what){
case CustomStaticCode.MESSAGE_CHILD_TAG:
int state = msg.arg1;
if(state==999){
tlist= (ArrayList) msg.obj;
initTablayout();
xmUtil=null;
}else{
ToastUtils.toast("错误信息:"+msg.obj+"\n错误码:"+state);
}
break;
}
return false;
}
});
@Override
public void initData() {
super.initData();
IntentFilter receiver =new IntentFilter();
receiver.addAction(CustomStaticCode.XMLY_PLAYER_RECEIVER);
updateImageReceiver = new UpdateImageReceiver();
MyBaseApplication.getApplication().registerReceiver(updateImageReceiver,receiver);
}
private void initPlay() {
conn=new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
DeviceXmlyPlayService.MyPlayBinder binder = (DeviceXmlyPlayService.MyPlayBinder) service;
mservice=binder.getService();
}
@Override
public void onServiceDisconnected(ComponentName name) {
toast("连接失败..");
}
};
a.bindService(new Intent(a, DeviceXmlyPlayService.class),conn, Service.BIND_AUTO_CREATE);
}
@Override
public View initView(LayoutInflater inflater, @Nullable ViewGroup container) {
View v=inflater.inflate(R.layout.activity_device_story_main,null);
xmUtil=new XMLYTingUtil(getActivity(),handler);
init(v);
return v;
}
private void init(View root) {
dcStoryMainTablayout = (TabLayout) root.findViewById(R.id.dc_story_main_tablayout);
dcStoryMainViewpager = (ViewPager) root.findViewById(R.id.dc_story_main_viewpager);
dcStoryMainPlay = (ImageView) root.findViewById(R.id.dc_story_main_play);
dcStoryMainViewpager.setOffscreenPageLimit(1);
xmUtil.getChildTag("0");
initPlay();
dcStoryMainPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int playListSize = mservice.getPlayListSize();
if(playListSize<=0){
toast("请选择播放目录后再播放音乐哟~");
return;
}
mservice.pauseTracks();
setImageState();
}
});
}
private void initTablayout(){
ArrayList fragmentArrayList=new ArrayList<>();
if(tlist!=null&&tlist.size()>0){
initFragmentList(fragmentArrayList);
StoryMainFragmentAdapter smadapter=new StoryMainFragmentAdapter(getActivity().getSupportFragmentManager(),fragmentArrayList,tlist);
dcStoryMainViewpager.setAdapter(smadapter);
dcStoryMainTablayout.setTabMode(TabLayout.MODE_SCROLLABLE);
dcStoryMainTablayout.setupWithViewPager(dcStoryMainViewpager);
dcStoryMainViewpager.setCurrentItem(0);
dcStoryMainTablayout.getTabAt(0).select();
}
}
private void initFragmentList(ArrayList fragmentArrayList){
// fragmentArrayList.add(new StoryHotFragment(tlist.get(0).getTagName()));
int count=tlist.size();
for (int i = 0; i < count; i++) {
String tag=tlist.get(i).getTagName();
// if(tag.indexOf("0-2")!=1|| tag.indexOf("付费")!=1) continue;
StoryItemFragment sf=new StoryItemFragment(tag);
fragmentArrayList.add(sf);
}
}
private void setImageState(){
boolean playing = mservice.isPlaying();
if(playing){
dcStoryMainPlay.setImageResource(R.drawable.story_top_pause3x);
}else{
dcStoryMainPlay.setImageResource(R.drawable.story_top_play3x);
}
}
class UpdateImageReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(action.equals(CustomStaticCode.XMLY_PLAYER_RECEIVER)){
Bundle status = intent.getExtras();
int state=status.getInt("status",Integer.MIN_VALUE);
if(state!=Integer.MIN_VALUE){
if(state==CustomStaticCode.XMLY_NOW_PLAY){
dcStoryMainPlay.setImageResource(R.drawable.story_top_pause3x);
}else if(state==CustomStaticCode.XMLY_NOW_PAUSE || state==CustomStaticCode.XMLY_NOW_STOP){
dcStoryMainPlay.setImageResource(R.drawable.story_top_play3x);
}
}
}
}
}
@Override
public void onDestroy() {
super.onDestroy();
if(updateImageReceiver!=null){
try {
a.unregisterReceiver(updateImageReceiver);
}catch (Exception e){
updateImageReceiver=null;
}
}
if(conn!=null){
try {
a.unbindService(conn);
}catch (Exception e){
conn=null;
}
}
}