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
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
package com.ycl.api.YS.playback.config;
 
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.IntByReference;
import com.ycl.api.YS.NetDemo;
import com.ycl.api.YS.lib.NetDEVSDKLib.NETDEV_VOD_PLAY_CTRL_E;
 
import javax.swing.*;
import java.util.regex.Pattern;
 
/**
 * 
 * @introduction Play local Record
 * @description Support IPC/NVR/VMS
 */
public class PlayBackPlayLocalFile {
    
    /**
     * 
     * @introduction Start play local file
     * @description Calling the interface of NETDEV_StartPlayMediaFile
     *
     */
    public static void startPlayLocalFile() {
        if(null != NetDemo.lpPlayHandle) {
            JOptionPane.showMessageDialog(null, "Please stop playing first.");
            return;
            }  
        
        if (null!=NetDemo.jTextFieldPlayLocalFile.getText()) {
            String pszFilename=NetDemo.jTextFieldPlayLocalFile.getText();
            JOptionPane.showMessageDialog(null, "Filename:"+pszFilename);
            NetDemo.lpPlayHandle=NetDemo.netdevsdk.NETDEV_OpenMediaFile(pszFilename);
            }
        
        if(NetDemo.lpPlayHandle==null) {
            NetDemo.jTextFieldPlayLocalFile.setText(null);
            JOptionPane.showMessageDialog(null, "File is not exist");
            return;
        }
        NetDemo.jButtonPlayLocalFile.setEnabled(false);
        NetDemo.jButtonStopLocalFile.setEnabled(true);
        IntByReference pdwTotalTime=new IntByReference();
 
        boolean bRet2= NetDemo.netdevsdk.NETDEV_GetMediaFileTime(NetDemo.lpPlayHandle, pdwTotalTime);
        if(bRet2) {
            JOptionPane.showMessageDialog(null, "File duration "+pdwTotalTime.getValue()+"s");
            Pointer lpPlayWnd=Native.getComponentPointer(NetDemo.panelPlayLabel);
            
            boolean bRet=NetDemo.netdevsdk.NETDEV_StartPlayMediaFile(NetDemo.lpPlayHandle,lpPlayWnd);
            if(bRet) {
                JOptionPane.showMessageDialog(null, "Play success ");
                NetDemo.netdevsdk.NETDEV_SetMuteStatus(NetDemo.lpPlayHandle,1);
                NetDemo.netdevsdk.NETDEV_OpenSound(NetDemo.lpPlayHandle);
            }else {
                JOptionPane.showMessageDialog(null, "Play media file failed. error code"+NetDemo.netdevsdk.NETDEV_GetLastError());
                System.out.println("NETDEV_StartPlayMediaFile failed"+NetDemo.netdevsdk.NETDEV_GetLastError());
            }
        }else {
            System.out.println("NETDEV_GetMediaFileTime failed"+NetDemo.netdevsdk.NETDEV_GetLastError());
            NetDemo.jButtonStopLocalFile.setEnabled(false);
            NetDemo.jButtonPlayLocalFile.setEnabled(false);
        }
    }
    
    /**
     * 
     * @introduction Stop play local file
     * @description
     *
     */
    public static void stopPlayLocalFile() {
        boolean bRet= NetDemo.netdevsdk.NETDEV_StopPlayMediaFile(NetDemo.lpPlayHandle);
        if(bRet) {
            JOptionPane.showMessageDialog(null, "Stop success");
            NetDemo.lpPlayHandle = null;
            NetDemo.jButtonPlayLocalFile.setEnabled(true);
            NetDemo.jButtonStopLocalFile.setEnabled(false);
            NetDemo.panelPlayLabel.repaint();
        }else {
            JOptionPane.showMessageDialog(null, "Stop playing media file failed. error code"+NetDemo.netdevsdk.NETDEV_GetLastError());
            System.out.println("NETDEV_StopPlayMediaFile failed"+NetDemo.netdevsdk.NETDEV_GetLastError());
        }
    }
    
    /**
     * 
     * @introduction Pause play the file
     * @description
     *
     */
    public static void pausePlayLocalFile() {
        if(NETDEV_VOD_PLAY_CTRL_E.NETDEV_PLAY_CTRL_PAUSE == NetDemo.PlayBackControlCmd){
            boolean bRet = NetDemo.netdevsdk.NETDEV_PlayBackControl(NetDemo.lpPlayHandle, NETDEV_VOD_PLAY_CTRL_E.NETDEV_PLAY_CTRL_RESUME, null);
            if(bRet){
                NetDemo.PlayBackControlCmd = NETDEV_VOD_PLAY_CTRL_E.NETDEV_PLAY_CTRL_RESUME;
                NetDemo.jButtonPlayLocalFilePause.setText("Pause");
            }
        }
        else{
            boolean bRet = NetDemo.netdevsdk.NETDEV_PlayBackControl(NetDemo.lpPlayHandle, NETDEV_VOD_PLAY_CTRL_E.NETDEV_PLAY_CTRL_PAUSE, null);
            if(bRet){
                NetDemo.PlayBackControlCmd = NETDEV_VOD_PLAY_CTRL_E.NETDEV_PLAY_CTRL_PAUSE;
                NetDemo.jButtonPlayLocalFilePause.setText("Resume");
            }
        }
    }
    
    /**
     * 
     * @introduction Open local Record file
     * @description Choose local file to play
     *
     */
    public static void openFile() {
        String path=null;
        JFileChooser jfierchoose=new JFileChooser();
        jfierchoose.setDialogTitle("请选择上传的文件");
        jfierchoose.setApproveButtonText("确定");
        jfierchoose.setFileSelectionMode(JFileChooser.FILES_ONLY);
        
        if(JFileChooser.APPROVE_OPTION==jfierchoose.showOpenDialog(NetDemo.jPanelliveVoice)) {
            try {
                path=jfierchoose.getSelectedFile().getPath();
            }catch(Exception ee) {
                return;
            }
        }
        if(path==null||path.equals("")) {
            System.out.println("Empty path");
            return;
        }
        
        String name=null;
        try {
            name=path.substring(path.lastIndexOf("."));
        }catch(Exception m1) {
            JOptionPane.showMessageDialog(null, "File does not exist");
            return;
        }
        
        String newpath=null;
        try {
            newpath=path.substring(path.lastIndexOf("."));
        }catch(Exception m2) {
            JOptionPane.showMessageDialog(null, "File does not exist");
            return;
        }
          
        if(newpath==null||newpath.equals("")) {
            JOptionPane.showMessageDialog(null, "Empty path");
            return;
        }
        
        String reg = "(mp4|MP4|FLV|AVI|RM|RMVB|WMV|MPEG|flv|avi|rm|rmvb|wmv|mpeg|nAVI|asf|mov|3gp|DivX|XviD|ts)";
        Pattern p = Pattern.compile(reg);
        boolean bRet = p.matcher(newpath).find();
        if(bRet) {
            JOptionPane.showMessageDialog(null, "Choose file"+name+" success!");
            NetDemo.jTextFieldPlayLocalFile.setText(path);
        }
        else {
            JOptionPane.showMessageDialog(null, "Invalid file type");
        }
    
    }
 
}