package com.hdl.sdk.hdl_core.HDLAppliances.HDLAudio;
|
|
import android.util.Log;
|
|
import com.hdl.sdk.hdl_core.Config.Configuration;
|
import com.hdl.sdk.hdl_core.HDLDeviceManger.Bean.AppliancesInfo;
|
import com.hdl.sdk.hdl_core.HDLDeviceManger.Bean.UdpDataBean;
|
import com.hdl.sdk.hdl_core.HDLDeviceManger.Core.HDLCommand;
|
import com.hdl.sdk.hdl_core.HDLDeviceManger.EventBusEvent.AudioInfoEvent;
|
import com.hdl.sdk.hdl_core.Util.NetUtil.NetWorkUtil;
|
import com.hdl.sdk.hdl_core.Util.TransformUtil.StringUtil;
|
|
import org.greenrobot.eventbus.EventBus;
|
|
import java.util.ArrayList;
|
import java.util.List;
|
import java.util.Timer;
|
import java.util.TimerTask;
|
|
import static com.hdl.sdk.hdl_core.Util.TransformUtil.StringUtil.stringToAscii;
|
|
|
/**
|
* Created by Tommy on 2017/10/24.
|
*/
|
|
public class HDLAudio {
|
//*SsPLAYSTOP 播放/停止
|
//*SsPLAYPAUSE 播放/停止
|
//*SsPLAY 播放
|
//*SsSTOP 停止
|
//#SsDISPINFO,DUR1945,POS0,STATUS2
|
// DUR后数值:为歌曲播放总时间 (秒×10,实际时间的10倍)
|
// POS后数值:为歌曲已播放时间 (秒×10,实际时间的10倍)
|
// STATUS后数值:为1表示stop,为2表示play,为3表示pause
|
//*SsPREVLIST 上一列表
|
//*SsNEXTLIST<CR> 下一列表
|
//*SsPREV<CR> 上一首 s是源号(1-9)
|
//*SsNEXT<CR> 下一首 s是源号(1-9)
|
public static final int CALLBACK_SONG_NAME_LIST = 100;//获取音乐列表
|
public static final int CALLBACK_CURRENT_VOLUME = 101;//当前音量
|
public static final int CALLBACK_AUDIO_LIST_NUM = 102;//当前列表数
|
public static final int CALLBACK_CURRENT_LIST_NAME = 103;//当前列表名
|
public static final int CALLBACK_CURRENT_SONG_NUM = 104;//当前歌曲数
|
public static final int CALLBACK_CURRENT_SONG_NAME = 105;//当前歌曲名
|
public static final int CALLBACK_CURRENT_SONG_INFO = 106;//当前歌曲信息
|
public static final int CALLBACK_CURRENT_MODE = 107;//当前播放模式
|
|
public static final int GET_AUDIO_LIST = 200;
|
public static final int SET_AUDIO_VOL = 201;
|
public static final int SET_AUDIO_PLAYSTOP = 202;
|
public static final int SET_AUDIO_PLAYPAUSE = 203;
|
public static final int SET_CHOOSE_PLAY_SONG = 204;
|
public static final int SET_NEXT_SONG = 205;
|
public static final int SET_PRE_SONG = 206;
|
public static final int GET_AUDIO_MODE = 207;
|
public static final int SET_AUDIO_MODE_UP = 208;
|
public static final int SET_AUDIO_MODE_DOWN = 209;
|
public static final int GET_AUDIO_CURRRENT_INFO = 210;
|
public static final int SET_NEXT_LIST = 211;
|
public static final int SET_PRE_LIST = 212;
|
|
public static final int TEXT = 300;
|
|
public static boolean isSetPreNextList = false;
|
public static List<String> numStr = new ArrayList<>();
|
public static List<String> songNameList = new ArrayList<>();
|
public static int curListNum = 0;
|
private static Timer timer = null;
|
|
public static void parseAudioData(UdpDataBean getDatas, AppliancesInfo audioInfo) {
|
|
List<Integer> intAllData = new ArrayList<>();
|
List<Integer> intMusicData = new ArrayList<>();
|
List<Integer> intName = new ArrayList<>();
|
int posSTX = 0;
|
// String allData = "";
|
for (int i = 0; i < getDatas.addBytes.length; i++) {
|
// allData += (getDatas.AddBytes[i]&0xff)+",";
|
if ((getDatas.addBytes[i] & 0xff) == 2) {
|
posSTX = i;
|
}
|
if (posSTX == 0) {
|
intAllData.add(getDatas.addBytes[i] & 0xFF);
|
if ((getDatas.addBytes[i] & 0xff) != 44) {
|
intMusicData.add(getDatas.addBytes[i] & 0xFF);
|
}
|
|
} else {
|
if ((getDatas.addBytes[i] & 0xff) != 2 && (getDatas.addBytes[i] & 0xff) != 3) {
|
intName.add(getDatas.addBytes[i] & 0xFF);
|
}
|
if ((getDatas.addBytes[i] & 0xff) == 3) {
|
intName.remove(intName.size() - 1);
|
break;
|
}
|
}
|
}
|
// Log.i("djl","所有数据:"+allData);
|
for (int i = 0; i < intName.size(); i++) {
|
if (i % 2 != 0) {
|
intAllData.add(intName.get(i - 1) * 256 + intName.get(i));
|
}
|
}
|
|
String musicData = "";
|
for (int i = 0; i < intAllData.size(); i++) {
|
musicData += intAllData.get(i) + ",";
|
}
|
musicData = StringUtil.asciiToString(musicData);
|
// Log.i("djl","音乐ascii:"+musicData);
|
|
handleMusicData(getDatas, musicData, intMusicData, audioInfo);
|
|
}
|
|
private static void handleMusicData(final UdpDataBean getDatas, String musicData, List<Integer> intMusicData, AppliancesInfo audioInfo) {
|
Log.i("djl","musicData = "+musicData);
|
String[] arrMusicData = musicData.split(",");
|
if (isExitData(arrMusicData[0], "DISPLINE1")) {
|
String[] arr1 = arrMusicData[1].split(":");
|
if (arr1.length <= 1) {
|
return;
|
}
|
String[] arr2 = arr1[1].split("/");
|
int[] audioListInfo = new int[2];
|
audioListInfo[0] = Integer.parseInt(arr2[0]);
|
audioListInfo[1] = Integer.parseInt(arr2[1]);
|
|
if (isSetPreNextList) {
|
isSetPreNextList = false;
|
numStr.clear();
|
songNameList.clear();
|
AppliancesInfo info = new AppliancesInfo();
|
info.setDeviceSubnetID(getDatas.sourceSubnetID);
|
info.setDeviceDeviceID(getDatas.sourceDeviceID);
|
HDLCommand.audioCtrl(info, HDLAudio.GET_AUDIO_LIST, audioListInfo[0]);
|
}
|
|
EventBus.getDefault().post(new AudioInfoEvent(CALLBACK_AUDIO_LIST_NUM, audioListInfo, audioInfo));
|
} else if (isExitData(arrMusicData[0], "DISPLINE2")) {
|
EventBus.getDefault().post(new AudioInfoEvent(CALLBACK_CURRENT_LIST_NAME, arrMusicData[1], audioInfo));
|
} else if (isExitData(arrMusicData[0], "DISPLINE3")) {
|
String[] songNumInfoStr = new String[2];
|
songNumInfoStr[0] = "";
|
songNumInfoStr[1] = "";
|
for (int i = 2, len = arrMusicData[1].length(); i < len; i++) {
|
if (i == 2) {
|
songNumInfoStr[0] += arrMusicData[1].charAt(i);
|
} else if (i >= 4) {
|
songNumInfoStr[1] += arrMusicData[1].charAt(i) + "";
|
}
|
}
|
int[] songNumInfo = new int[2];
|
songNumInfo[0] = Integer.parseInt(songNumInfoStr[0]);
|
songNumInfo[1] = Integer.parseInt(songNumInfoStr[1]);
|
EventBus.getDefault().post(new AudioInfoEvent(CALLBACK_CURRENT_SONG_NUM, songNumInfo, audioInfo));
|
} else if (isExitData(arrMusicData[0], "DISPLINE4")) {
|
EventBus.getDefault().post(new AudioInfoEvent(CALLBACK_CURRENT_SONG_NAME, arrMusicData[1], audioInfo));
|
} else if (isExitData(arrMusicData[0], "DISPINFO")) {
|
String durStr = "";
|
String posStr = "";
|
String statusStr = "";
|
for (int i = 3, len = arrMusicData[1].length(); i < len; i++) {
|
if (Character.isDigit(arrMusicData[1].charAt(i))) {
|
durStr += arrMusicData[1].charAt(i);
|
}
|
}
|
|
for (int i = 3, len = arrMusicData[2].length(); i < len; i++) {
|
if (Character.isDigit(arrMusicData[2].charAt(i))) {
|
posStr += arrMusicData[2].charAt(i);
|
}
|
}
|
|
statusStr += arrMusicData[3].charAt(6);
|
|
int durInt = Integer.parseInt(durStr) / 10;
|
int posInt = Integer.parseInt(posStr) / 10;
|
int statusInt = Integer.parseInt(statusStr);
|
EventBus.getDefault().post(new AudioInfoEvent(CALLBACK_CURRENT_SONG_INFO, new int[]{durInt, posInt, statusInt}, audioInfo));
|
} else if (isExitData(arrMusicData[0], "DISPMODE")) {
|
String audioModeStr = arrMusicData[1].charAt(6) + "";
|
EventBus.getDefault().post(new AudioInfoEvent(CALLBACK_CURRENT_MODE, Integer.parseInt(audioModeStr), audioInfo));
|
} else if (isExitData(musicData, ",ON,SRC") || isExitData(musicData, ",OFF,SRC")) {
|
String vol = arrMusicData[3];
|
String volNum = "";
|
for (int i = 3, len = vol.length(); i < len; i++) {
|
if (Character.isDigit(vol.charAt(i))) {
|
volNum += vol.charAt(i);
|
}
|
}
|
EventBus.getDefault().post(new AudioInfoEvent(CALLBACK_CURRENT_VOLUME, 79 - (Integer.parseInt(volNum)), audioInfo));
|
} else if (isExitData(arrMusicData[0], "TYPE")) {
|
Log.i("djl","Type ----> musicData = "+musicData);
|
byte[] musicBytes = audioGetListParse(curListNum, 1);
|
sendAudioData(getDatas, musicBytes);
|
} else if (isExitData(arrMusicData[0], "LIST")) {
|
|
String num = "";
|
for (int i = 12; i < 15; i++) {
|
num += arrMusicData[0].charAt(i);
|
}
|
int intNum = Integer.parseInt(num);
|
if (curListNum == intNum) {//判断回复的数据是否为当前列表数
|
byte[] musicBytes = audioGetListParse(curListNum, 2);
|
sendAudioData(getDatas, musicBytes);
|
}
|
} else if (isExitData(arrMusicData[0], "SONG")) {
|
String num = "";
|
for (int i = 12; i < 15; i++) {
|
num += arrMusicData[0].charAt(i);
|
}
|
int intNum = Integer.parseInt(num);
|
if (curListNum == intNum) {
|
//判断回复的数据是否为当前列表数
|
if (timer == null) {
|
timer = new Timer();
|
} else {
|
timer.cancel();
|
timer = null;
|
timer = new Timer();
|
}
|
if (numStr.size() == 0) {
|
numStr.add(arrMusicData[0]);
|
songNameList.add(arrMusicData[1]);
|
} else {
|
if (!numStr.contains(arrMusicData[0])) {
|
numStr.add(arrMusicData[0]);
|
songNameList.add(arrMusicData[1]);
|
}
|
}
|
final byte[] musicBytes = new byte[intMusicData.size() + 1];
|
musicBytes[musicBytes.length - 1] = 13;
|
for (int i = 0, len = musicBytes.length - 1; i < len; i++) {
|
musicBytes[i] = (byte) intMusicData.get(i).intValue();
|
}
|
int num1 = musicBytes[musicBytes.length - 5] & 0xff;
|
int num2 = musicBytes[musicBytes.length - 4] & 0xff;
|
int num3 = musicBytes[musicBytes.length - 3] & 0xff;
|
int num4 = musicBytes[musicBytes.length - 2] & 0xff;
|
if (num4 == 48) {
|
if ((num3) == 57) {
|
num3 = 48;
|
if (num2 == 57) {
|
num2 = 48;
|
num1++;
|
} else {
|
num2++;
|
}
|
|
} else {
|
num3++;
|
}
|
musicBytes[0] = 42;
|
musicBytes[musicBytes.length - 5] = (byte) num1;
|
musicBytes[musicBytes.length - 4] = (byte) num2;
|
musicBytes[musicBytes.length - 3] = (byte) num3;
|
musicBytes[musicBytes.length - 2] = 49;
|
|
timer.schedule(new TimerTask() {
|
public void run() {
|
sendAudioData(getDatas, musicBytes);
|
}
|
}, 500, 300);
|
sendAudioData(getDatas, musicBytes);
|
} else {
|
EventBus.getDefault().post(new AudioInfoEvent(CALLBACK_SONG_NAME_LIST, songNameList, audioInfo));
|
AppliancesInfo info = new AppliancesInfo();
|
info.setDeviceSubnetID(getDatas.sourceSubnetID);
|
info.setDeviceDeviceID(getDatas.sourceDeviceID);
|
|
}
|
}
|
|
|
}
|
}
|
|
|
private static void sendAudioData(UdpDataBean getDatas, byte[] musicBytes) {
|
// String sendData = "";
|
// for(int i = 0,len = musicBytes.length;i<len;i++){
|
// sendData += musicBytes[i]+",";
|
// }
|
// sendData = StringUtil.asciiToString(sendData);
|
// Log.i("djl","发出数据:"+sendData);
|
HDLCommand.cusSendCommand(Configuration.AUDIO_MenuPlay_INSTRUCTION_COMMAND,
|
getDatas.sourceSubnetID, getDatas.sourceDeviceID, musicBytes, Configuration.PORT, NetWorkUtil.getLocalBroadCast());
|
}
|
|
private static boolean isExitData(String str, String type) {
|
return str.indexOf(type) != -1;
|
}
|
|
public static byte[] audioVolparse(int vol) {
|
if (vol > 79 || vol < 0) {
|
Log.i("djl", "非法数据");
|
return null;
|
} else {
|
int intAsc = 79 - vol;
|
String strAsc = "";
|
if (intAsc < 10) {
|
strAsc = "0" + intAsc;
|
} else {
|
strAsc += intAsc / 10;
|
strAsc += intAsc % 10;
|
}
|
String byteAsc = stringToAscii(strAsc);
|
String[] arrAsc = byteAsc.split(",");
|
int a1 = Integer.parseInt(arrAsc[0]);
|
int a2 = Integer.parseInt(arrAsc[1]);
|
return new byte[]{0x2A, 0x5A, 0x31, 0x56, 0x4F, 0x4C, (byte) a1, (byte) a2, 0x0D};
|
}
|
}
|
|
public static byte[] audioChooseSongParse(int value1, int value2) {
|
int listNum = value1;
|
int songNum = value2 + 1;
|
String songNumStrAsc = "";
|
String listNumStrAsc = "";
|
if (songNum < 10) {
|
songNumStrAsc = "00" + songNum;
|
} else if (songNum >= 10 && songNum < 100) {
|
songNumStrAsc = "0" + songNum;
|
} else {
|
songNumStrAsc += songNum;
|
}
|
|
if (listNum < 10) {
|
listNumStrAsc = "00" + listNum;
|
} else if (listNum >= 10 && listNum < 100) {
|
listNumStrAsc = "0" + listNum;
|
} else {
|
listNumStrAsc += listNum;
|
}
|
|
String songNumByteAsc = stringToAscii(songNumStrAsc);
|
String listNumByteAsc = stringToAscii(listNumStrAsc);
|
|
String[] songNumArrAsc = songNumByteAsc.split(",");
|
String[] listNumArrAsc = listNumByteAsc.split(",");
|
|
int a1 = Integer.parseInt(songNumArrAsc[0]);
|
int a2 = Integer.parseInt(songNumArrAsc[1]);
|
int a3 = Integer.parseInt(songNumArrAsc[2]);
|
|
int b1 = Integer.parseInt(listNumArrAsc[0]);
|
int b2 = Integer.parseInt(listNumArrAsc[1]);
|
int b3 = Integer.parseInt(listNumArrAsc[2]);
|
//*Z011SONG001
|
return new byte[]{0x2A, 0x5A, 0x30, 0x31, 0x31, 0x53, 0x4F, 0x4E, 0x47, 0x30
|
, 0x30, 0x31, (byte) b1, (byte) b2, (byte) b3, (byte) a1, (byte) a2, (byte) a3, 0x32, 0x0D};
|
}
|
|
private static byte[] audioGetListParse(int num, int type) {
|
String strAsc = "";
|
if (num < 10) {
|
strAsc = "00" + num;
|
} else if (num >= 10 && num < 100) {
|
strAsc = "0" + num;
|
} else {
|
strAsc += num;
|
}
|
String byteAsc = stringToAscii(strAsc);
|
String[] arrAsc = byteAsc.split(",");
|
int a1 = Integer.parseInt(arrAsc[0]);
|
int a2 = Integer.parseInt(arrAsc[1]);
|
int a3 = Integer.parseInt(arrAsc[2]);
|
if (type == 1) {
|
return new byte[]{0x2A, 0x5A, 0x30, 0x31, 0x31, 0x4C, 0x49, 0x53,
|
0x54, 0x30, 0x30, 0x31, (byte) a1, (byte) a2, (byte) a3, 0x31, 0x0D};
|
} else {
|
return new byte[]{42, 90, 48, 49, 49, 83, 79, 78, 71, 48, 48, 49, (byte) a1, (byte) a2, (byte) a3, 48, 48, 49, 49, 13};
|
}
|
}
|
}
|