JLChen
2021-11-05 de72a7843ceb868c89fc11983e315849caa28573
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
package com.hdl.sdk.ttl.HDLAppliances.HDLAudio;
 
import android.util.Log;
 
import com.hdl.sdk.ttl.Config.Configuration;
import com.hdl.sdk.ttl.HDLDeviceManger.Bean.AppliancesInfo;
import com.hdl.sdk.ttl.HDLDeviceManger.Bean.UdpDataBean;
import com.hdl.sdk.ttl.HDLDeviceManger.Core.HDLCommand;
import com.hdl.sdk.ttl.HDLDeviceManger.EventBusEvent.AudioInfoEvent;
import com.hdl.sdk.ttl.Utils.HDLUtlis.HDLStringUtils;
 
import org.greenrobot.eventbus.EventBus;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
import static com.hdl.sdk.ttl.Utils.HDLUtlis.HDLStringUtils.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 = HDLStringUtils.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));
 
            //2019-8-7 方案调整
            String arrMusicDataStr = arrMusicData[1].toString();
            String[] arrMusicDataStrArray = arrMusicDataStr.split("/");
            if(arrMusicDataStrArray.length > 1){
                int[] songNumInfo = new int[2];
                String regEx="[^0-9]";
                Pattern mPattern = Pattern.compile(regEx);
                Matcher mMatcher0 = mPattern.matcher(arrMusicDataStrArray[0]);
                songNumInfo[0] = Integer.parseInt(mMatcher0.replaceAll("").trim());
                Matcher mMatcher1 = mPattern.matcher(arrMusicDataStrArray[1]);
                songNumInfo[1] = Integer.parseInt(mMatcher1.replaceAll("").trim());
                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 = HDLStringUtils.asciiToString(sendData);
//        Log.i("djl","发出数据:"+sendData);
        HDLCommand.cusSendCommand(Configuration.AUDIO_MenuPlay_INSTRUCTION_COMMAND,
                getDatas.sourceSubnetID, getDatas.sourceDeviceID, musicBytes);
    }
 
    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};
        }
    }
}