fuliqi
2024-08-29 4163c93761115c7524ef74a557a1f5e01eafb429
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
package com.ycl.api.YS.liveview.information;
 
import com.sun.jna.Pointer;
import com.ycl.api.YS.NetDemo;
import com.ycl.api.YS.lib.NetDEVSDKLib.NETDEV_AUDIO_SAMPLE_PARAM_S;
 
import javax.swing.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
 
/**
 * 
 * @introduction LiveVoice
 * @description Support IPC/NVR/VMS
 */
public class LiveVoice {
    /**
     * 
     * @introduction Choose voice file
     * @description Choose local voice file
     *
     */
    public static void chooseVoiceFile() {
        String path=null;
        JFileChooser jfierchoose=new JFileChooser();
        jfierchoose.setFileSelectionMode(JFileChooser.FILES_ONLY);
        if(JFileChooser.APPROVE_OPTION==jfierchoose.showOpenDialog(NetDemo.jPanelliveVoice)) {
            path=jfierchoose.getSelectedFile().getPath();
        }
        if(path==null||path.equals("")) {
            System.out.println("File is null");
            return;
        }
        String newpath=null;
        try {
            newpath=path.substring(path.lastIndexOf("."));
        }catch(Exception y) {
            JOptionPane.showMessageDialog(null, "File is not exist");
            return;
        }
        
        if(newpath==null||newpath.equals("")) {
            System.out.println("File is null");
            return;
        }
        if(!(newpath.equals(".pcm"))) {
            JOptionPane.showMessageDialog(null, "Only support pcm file");
            return;
        }else {
            JOptionPane.showMessageDialog(null, "Choose file is ok");
            NetDemo.jTextFieldLiveVoice.setText(path);
            JOptionPane.showMessageDialog(null, "Automatic acquistion");
            NetDemo.jButtonLiveGetVoice.doClick();
        }
    
    }
 
    /**
     * 
     * @introduction Start voice acquisition
     * @description Only support pcm file
     *
     */
    public static void voiceAcquisition() {
        if(null == NetDemo.lpUserID) {
            JOptionPane.showMessageDialog(null, "Please Login device first. error code"+NetDemo.netdevsdk.NETDEV_GetLastError());
            return;
            }
        if(NetDemo.ChannelID == 0){
            JOptionPane.showMessageDialog(null, "Please select the channel first.");
            return;
        }
 
        String path=NetDemo.jTextFieldLiveVoice.getText();   
        if(path==null||path.equals("")) {
            JOptionPane.showMessageDialog(null, "File is null");
            return;
        }
        
        String fileSuffix=null;
        try {
            fileSuffix=path.substring(path.lastIndexOf("."));
        }catch(Exception t) {
            JOptionPane.showMessageDialog(null, "File is not exist");
            return;
        }
        if(!(fileSuffix.equals(".pcm"))) {
            JOptionPane.showMessageDialog(null, "File type is error, only support pcm file");
            return;
        }
        
        Pointer lpPlayHandle= NetDemo.netdevsdk.NETDEV_StartInputVoiceSrv(NetDemo.lpUserID, NetDemo.ChannelID);
        File file=new File(path);
        try {
            InputStream input=new FileInputStream(file);
            int dwBufLen=640;
            byte[] szBuff = new byte[dwBufLen];
            NETDEV_AUDIO_SAMPLE_PARAM_S pstVoiceParam=new NETDEV_AUDIO_SAMPLE_PARAM_S();
            while(input.read(szBuff,0,dwBufLen)!=-1) {
                boolean bRet= NetDemo.netdevsdk.NETDEV_InputVoiceData(lpPlayHandle,szBuff, dwBufLen,pstVoiceParam);
                if(!bRet) {
                    input.close();
                    JOptionPane.showMessageDialog(null, "InputVoice fail");
                    NetDemo.jTextFieldLiveVoice.setText(null);
                    System.out.printf("NETDEV_InputVoiceData failed:%d\n", NetDemo.netdevsdk.NETDEV_GetLastError());
                    return;
                }
                Thread.sleep(40);
            }
            
            input.close();
            JOptionPane.showMessageDialog(null, "Success completed");
            NetDemo.jTextFieldLiveVoice.setText(null);
            boolean bRet=NetDemo.netdevsdk.NETDEV_StopInputVoiceSrv(lpPlayHandle);
            if(bRet != true) {
                System.out.printf("NETDEV_StopInputVoiceSrv failed, error:" + NetDemo.netdevsdk.NETDEV_GetLastError());
                return;
                }
        } catch (IOException | InterruptedException e) {
            JOptionPane.showMessageDialog(null, "File does not exist or has been destoryed");
            NetDemo.jTextFieldLiveVoice.setText(null);
        }
    }
}