Kaynağa Gözat

增加对电视的音量控制等功能

wjg 1 yıl önce
ebeveyn
işleme
791966c62e

+ 1 - 0
app/build.gradle.kts

@@ -63,6 +63,7 @@ dependencies {
     implementation(files("libs\\jackson-annotations-2.12.7.jar"))
     implementation(files("libs\\jackson-core-2.12.7.jar"))
     implementation(files("libs\\jackson-databind-2.12.7.1.jar"))
+    implementation(files("libs\\CH_TVOS.jar"))
     testImplementation("junit:junit:4.13.2")
     androidTestImplementation("androidx.test.ext:junit:1.1.5")
     androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")

BIN
app/libs/CH_TVOS.jar


+ 5 - 1
app/src/main/AndroidManifest.xml

@@ -8,9 +8,12 @@
     <uses-permission
         android:name="android.permission.WRITE_EXTERNAL_STORAGE"
         tools:ignore="ScopedStorage" />
-    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
+    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"
+        tools:ignore="ScopedStorage" />
+    <uses-permission android:name="android.permission.READ_MEDIA_VIDEO"/>
     <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
+    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>
 
     <application
         android:allowBackup="true"
@@ -21,6 +24,7 @@
         android:roundIcon="@mipmap/ic_launcher_round"
         android:supportsRtl="true"
         android:theme="@style/Theme.JedgeGLMDemo"
+        android:requestLegacyExternalStorage="true"
         tools:targetApi="31">
         <activity
             android:name=".MainActivity"

+ 1 - 1
app/src/main/assets/cfg/dataset/tvass/media_cmdlist.json

@@ -3,7 +3,7 @@
   "//,根据用户提供的信息提取关键字,并查找对应的影片信息",
   {"key": "miPlay", "disc": "用户要求或希望播放媒体,包括在线媒体和电视机本地存储的媒体内容。带有播放模式参数, \"mode\"=播放模式, 播放模式替换为对应的值"},
   "//根据已经命中的媒体信息,去播放队对应的影片或歌曲,并设置对应的播放参数",
-  {"key": "miSave", "disc": "用户侗族涉及希望记录其的个性化喜好"},
+  {"key": "miSave", "disc": "用户希望记录其的个性化喜好"},
   "//,根据已经播放的历史或内容,记住用户的一些播放参数",
   {"key": "miCopy", "disc": "文件拷贝,通常是从U盘到电视,或从电视到U盘拷贝。可选择额外参数是文件名,\"file\"=拷贝文件, 类型为字符串,U盘根目录或电视指定路径下的文件名"},
   {"key": "miQuitUDrive", "disc": "弹出U盘或USB移动硬盘操作,弹出U盘或插入操作。可选择额外参数是文件名,\"drive\"=弹出对象"}

+ 3 - 6
app/src/main/assets/cfg/dataset/tvass/tv_cmd_list.json

@@ -1,10 +1,7 @@
 [
-  {"key": "turnOn", "disc": "打开电视"},
-  {"key": "turnOff", "disc": "关闭电视"},
-  {"key": "switchChannel", "disc": "切换频道, 需要额外参数,频道编号,  key 为 channel, 整数值, 0-999"},
   {"key": "setVolume", "disc": "设置音量,需要额外参数, 音量大小, key 为 volume, 整数值, 范围0-100"},
   {"key": "setDisplay", "disc": "设置显示信息,需要额外参数, 亮度值 : key 为 lightness, 整数值, 范围从0-100"},
-  {"key": "switchSource", "disc": "切换源,需要额外参数 源类型:  key 为 channel, 可选值为下列字符串: HDMI1, HDMI2, HDMI3"},
-  {"key": "tvStatus", "disc": "查看电视当前的状态:频道设置,色彩模式设置,音量设置,分辨率设置,输入源设置,账号等信息"},
-  {"key": "tvHelp", "disc": "一般性介绍电视的主要功能,询问电视有哪些功能,电视助手能做些什么,咨询电视设备控制功能,主要涉及跟电视的声音,显示参数,显示和音量控制,输入输出设置,网络配置等相关的功能询问"}
+  {"key": "setSound", "disc": "设置声音模式,需要额外参数, 可选值为下列字符串: 标准,剧场,音乐,新闻"},
+  {"key": "setPicture", "disc": "设置图像模式,需要额外参数, 可选值为下列字符串: 标准,柔和,亮丽"},
+  {"key": "switchSource", "disc": "切换源,需要额外参数 源类型:  key 为 channel, 可选值为下列字符串: HDMI1, HDMI2, HDMI3, HDMI4"},
 ]

+ 106 - 0
app/src/main/java/com/ch/jedge/bots/tvass/JedgeTVAssistant.java

@@ -1,13 +1,21 @@
 package com.ch.jedge.bots.tvass;
 
+import android.content.Context;
+import android.media.AudioManager;
+import android.util.Log;
+
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
 import com.ch.jedge.jbot.intent.JDataGetter;
 import com.ch.jedge.jbot.intent.JbotApi;
 import com.ch.jedge.jbot.intent.JbotData;
 import com.ch.jedge.jbot.intent.JedgeJBotCBService;
 import com.ch.jedge.jbot.intent.bot.JBotAppBase;
+import com.ch.jedge.tvmufis.JeageTVBridge;
 import com.changhong.jedge.JMgbusModual;
 import com.changhong.jedge.JMgbusUtil;
 import com.changhong.jedge.MgbusApi;
+import com.changhong.qlib.QData;
 import com.changhong.qlib.intf.QIData;
 
 import java.util.List;
@@ -35,6 +43,10 @@ public class JedgeTVAssistant extends JBotAppBase {
             eMsg.removeKey("~c");
             //
             module.warnLog("Remote call %s", eMsg);
+            String cmd = eMsg.getString("command");
+            JSONArray param = eMsg.getJsonArray("param");
+            if("miPlay".equals(cmd))
+                playMedia(param);
             return JMgbusUtil.MgbusResult(200, "已知悉,但暂未实现完整功能");
         }
 
@@ -49,6 +61,19 @@ public class JedgeTVAssistant extends JBotAppBase {
             eMsg.removeKey("~c");
             //
             module.warnLog("Remote call %s", eMsg);
+            String cmd = eMsg.getString("command");
+            JSONArray param = eMsg.getJsonArray("param");
+            if("setVolume".equals(cmd))
+                setVolume(param);
+//            else if("switchSource".equals(cmd))
+//                switchSource(param);
+            else if("setDisplay".equals(cmd))
+                setDisplay(param);
+            else if("setSound".equals(cmd))
+                setSound(param);
+            else if("setPicture".equals(cmd))
+                setPicture(param);
+
             return JMgbusUtil.MgbusResult(200, "已知悉,但暂未实现完整功能");
         }
 
@@ -79,4 +104,85 @@ public class JedgeTVAssistant extends JBotAppBase {
         JedgeTVAssistant.JedgeTVAssService.cfg_path = cfgpath;
         run("tvass", JedgeTVAssService.class, args);
     }
+
+    private static void setVolume(JSONArray param) {
+        if(param.isEmpty())
+            return;
+
+        int cnt = param.size();
+        for(int i=0; i<cnt; i+=2){
+            Object obj = param.get(i);
+            if (obj instanceof String) {
+                if("volume".equals(obj)) {
+                    int volume = param.getIntValue(i+1);
+                    JeageTVBridge tvBridge = JeageTVBridge.getInstance();
+                    tvBridge.TVsetVolume(volume);
+                    return;
+                }
+            }
+        }
+    }
+
+//    private static void switchSource(JSONArray param) {
+//        if(param.isEmpty())
+//            return;
+//
+//        Object obj = param.get(0);
+//        if (obj instanceof String) {
+//            String source = obj.toString();
+//            JeageTVBridge tvBridge = JeageTVBridge.getInstance();
+//            tvBridge.TVsetInputSource(source);
+//        }
+//    }
+    private static void setDisplay(JSONArray param) {
+        if(param.isEmpty())
+            return;
+
+        int cnt = param.size();
+        for(int i=0; i<cnt; i+=2){
+            Object obj = param.get(i);
+            if (obj instanceof String) {
+                if("lightness".equals(obj)) {
+                    int value = param.getIntValue(i+1);
+                    JeageTVBridge tvBridge = JeageTVBridge.getInstance();
+                    tvBridge.TVsetBrightness(value);
+                    return;
+                }
+            }
+        }
+    }
+
+    private static void setSound(JSONArray param) {
+        if(param.isEmpty())
+            return;
+
+        Object obj = param.get(0);
+        if (obj instanceof String) {
+            String mode = obj.toString();
+            JeageTVBridge tvBridge = JeageTVBridge.getInstance();
+            tvBridge.TVsetSoundMode(mode);
+        }
+    }
+    private static void setPicture(JSONArray param) {
+        if(param.isEmpty())
+            return;
+
+        Object obj = param.get(0);
+        if (obj instanceof String) {
+            String mode = obj.toString();
+            JeageTVBridge tvBridge = JeageTVBridge.getInstance();
+            tvBridge.TVsetPictureMode(mode);
+        }
+    }
+    private static void playMedia(JSONArray param) {
+        if(param.isEmpty())
+            return;
+
+//        Object obj = param.get(0);
+//        if (obj instanceof String) {
+//            String mode = obj.toString();
+            JeageTVBridge tvBridge = JeageTVBridge.getInstance();
+            tvBridge.TVplayVideo("");
+//        }
+    }
 }

+ 4 - 2
app/src/main/java/com/ch/jedge/glm/JedgeGlmBridgeService.java

@@ -76,8 +76,10 @@ public class JedgeGlmBridgeService extends JMgbusService {
         quit_ = false;
 
         zhipuConfig = new ConfigV3();
-        zhipuConfig.setApiKey("297ba886b74433514543eac6ff485ddf"); // QIData_DefaultConfig.getString("ApiKey");
-        zhipuConfig.setApiSecret("yKwpPFiUmreQd5Vq");              // QIData_DefaultConfig.getString("ApiSecret");
+//        zhipuConfig.setApiKey("297ba886b74433514543eac6ff485ddf"); // QIData_DefaultConfig.getString("ApiKey");
+//        zhipuConfig.setApiSecret("yKwpPFiUmreQd5Vq");              // QIData_DefaultConfig.getString("ApiSecret");
+        zhipuConfig.setApiKey("233bd55079e5f14ca868d0c1c92b52cb");
+        zhipuConfig.setApiSecret("i8gP3u3LQB58KDGM");
         zhipuConfig.setCache(LocalCache.getInstance());
         zhipuConfig.setDisableTokenCache(false);
 

+ 160 - 0
app/src/main/java/com/ch/jedge/tvmufis/JeageTVBridge.java

@@ -1,16 +1,37 @@
 package com.ch.jedge.tvmufis;
 
+import android.content.Context;
+import android.media.AudioManager;
+import android.media.MediaPlayer;
+import android.util.Log;
+
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
 import com.ch.jedge.llm.filter.glm.service.TVMufisService;
 import com.changhong.qlib.QData;
 import com.changhong.qlib.intf.QIData;
+import com.changhong.tvos.common.*;
+import com.changhong.tvos.common.exception.IllegalValueException;
+import com.changhong.tvos.common.exception.SourceNotFoundException;
+import com.changhong.tvos.common.exception.TVManagerNotInitException;
+import com.changhong.tvos.model.ChOsType;
+import com.changhong.tvos.model.EnumInputSource;
+import com.changhong.tvos.model.EnumResource;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.List;
 
 public class JeageTVBridge {
 
     private static JeageTVBridge bridge = null;
     private TVMufisService tvMufisService;
 
+    private ISourceManager srcm;
+    private MediaPlayerListenner mPlayerListerner;
+    private MediaPlayer mplayer;
+
     private JeageTVBridge(){
 
     }
@@ -59,4 +80,143 @@ public class JeageTVBridge {
         }
     }
 
+    public void TVsetVolume(int volume) {
+        if((volume<0)||(volume>100))
+            return;
+
+        AudioManager am = (AudioManager) tvMufisService.getSystemService(Context.AUDIO_SERVICE);
+//        int volume = am.getStreamVolume(AudioManager.STREAM_MUSIC);
+//        Log.d("TESTWWW", "getVolume 1");
+        am.setStreamVolume(AudioManager.STREAM_MUSIC, volume, AudioManager.FLAG_SHOW_UI);
+//        volume = am.getStreamVolume(AudioManager.STREAM_MUSIC);
+//        Log.d("TESTWWW", "getVolume 2");
+    }
+
+    public void TVsetBrightness(int value) {
+        if((value<0)||(value>100))
+            return;
+
+        TVManager tvm = TVManager.getInstance(tvMufisService);
+        try {
+            PictureManager pm = tvm.getPictureManager();
+//            pm.setBrightness(value);
+            pm.setBackLight(value);
+        } catch (TVManagerNotInitException | IllegalValueException e) {
+            throw new RuntimeException(e);
+        }
+    }
+    public void TVsetSoundMode(String mode) {
+        TVManager tvm = TVManager.getInstance(tvMufisService);
+        boolean ret = false;
+        try {
+            SoundManager sm = tvm.getSoundManager();
+            if("标准".equals(mode))
+                ret = sm.setSoundMode(ChOsType.ENUMSoundMode.CH_SOUND_MODE_STANDARD);
+            else if("剧场".equals(mode))
+                ret = sm.setSoundMode(ChOsType.ENUMSoundMode.CH_SOUND_MODE_UMAX);
+            else if("音乐".equals(mode))
+                ret = sm.setSoundMode(ChOsType.ENUMSoundMode.CH_SOUND_MODE_MUSIC);
+            else if("新闻".equals(mode))
+                ret = sm.setSoundMode(ChOsType.ENUMSoundMode.CH_SOUND_MODE_NEWS);
+
+            Log.d("TTTTTT", "ret is "+ret);
+        } catch (TVManagerNotInitException | IllegalValueException e) {
+            throw new RuntimeException(e);
+        }
+    }
+    public void TVsetPictureMode(String mode) {
+        TVManager tvm = TVManager.getInstance(tvMufisService);
+        boolean ret = false;
+        try {
+            PictureManager pm = tvm.getPictureManager();
+            if("标准".equals(mode))
+                ret = pm.setPictureMode(ChOsType.ENUMPictureMode.E_PICTURE_MODE_STANDARD);
+            else if("柔和".equals(mode))
+                ret = pm.setPictureMode(ChOsType.ENUMPictureMode.E_PICTURE_MODE_READING);
+            else if("亮丽".equals(mode))
+                ret = pm.setPictureMode(ChOsType.ENUMPictureMode.E_PICTURE_MODE_DOLBY_VIVID);
+
+            Log.d("TTTTTT", "ret is "+ret);
+        } catch (TVManagerNotInitException | IllegalValueException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+//    public void TVsetInputSource(String source) {
+//        TVManager tvm = TVManager.getInstance(tvMufisService);
+//        boolean ret = false;
+//        try {
+////            ISourceManager sm = tvm.getSourceManager();
+//            ITVPlayer itvPlayer = tvm.getTVPlayer();
+//            if("HDMI".equals(source)||"HDMI1".equals(source))
+//                ret = itvPlayer.startInputSource(EnumInputSource.E_INPUT_SOURCE_HDMI, -1);
+////                sm.selectInputSource(EnumInputSource.E_INPUT_SOURCE_HDMI);
+//            else if("HDMI2".equals(source))
+//                ret = itvPlayer.startInputSource(EnumInputSource.E_INPUT_SOURCE_HDMI2, -1);
+////                sm.selectInputSource(EnumInputSource.E_INPUT_SOURCE_HDMI2);
+//            else if("HDMI3".equals(source))
+//                ret = itvPlayer.startInputSource(EnumInputSource.E_INPUT_SOURCE_HDMI3, -1);
+////                sm.selectInputSource(EnumInputSource.E_INPUT_SOURCE_HDMI3);
+//            else if("HDMI4".equals(source))
+//                 ret = itvPlayer.startInputSource(EnumInputSource.E_INPUT_SOURCE_HDMI4, -1);
+////                sm.selectInputSource(EnumInputSource.E_INPUT_SOURCE_HDMI4);
+//
+//            Log.d("TTTTTT", "ret is "+ret);
+//        } catch (TVManagerNotInitException e) {
+//            throw new RuntimeException(e);
+//        }
+//    }
+    public void TVplayVideo(String name) {
+//        List<HashMap<String, String>> mapList = USBFileManager.GetVideoFile(tvMufisService);
+//        if (mapList.isEmpty())
+//            return;
+//
+//        String file = "";
+//        if(name.isEmpty())
+//            file = mapList.get(0).get("path");
+//        else {
+//            for(HashMap<String, String>hmap : mapList){
+//                String fname = hmap.get("name");
+//                if(name.equals(fname)) {
+//                    file = hmap.get("path");
+//                    break;
+//                }
+//            }
+//        }
+//
+//        if(file.isEmpty())
+//            return;
+//        String file = "/storage/8688-F2B5/vedio/视频2.mp4";
+        String file = "/sdcard/Movies/5.mp4";
+        TVManager tvm = TVManager.getInstance(tvMufisService);
+        try {
+
+            srcm = tvm.getSourceManager();
+            srcm.requestResource(EnumResource.CH_RESOURCE_MMP, "player");
+            mPlayerListerner = new MediaPlayerListenner();
+            srcm.registerResourceListener(mPlayerListerner);
+            if(mplayer != null) {
+                mplayer.reset();
+            }
+            else
+                mplayer = new MediaPlayer();
+
+            mplayer.setDataSource(file);
+            mplayer.prepare();
+            mplayer.start();
+        } catch (TVManagerNotInitException | IOException e) {
+            throw new RuntimeException(e);
+        }
+    }
+    class MediaPlayerListenner implements IResourceManager.IResourceListener {
+        @Override
+        public void onResourceLose() {
+            if(mplayer != null){
+                mplayer.stop();
+                mplayer.release();
+                mplayer = null;
+            }
+            srcm.unRegisterResourceListener(mPlayerListerner);
+        }
+    }
 }

+ 82 - 0
app/src/main/java/com/ch/jedge/tvmufis/USBFileManager.java

@@ -0,0 +1,82 @@
+package com.ch.jedge.tvmufis;
+
+import android.content.Context;
+import android.os.Environment;
+import android.os.storage.StorageManager;
+
+import java.io.File;
+import java.lang.reflect.InvocationTargetException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class USBFileManager {
+    private static List<String> usbPaths = new ArrayList<>();
+    private static List<HashMap<String, String>> videoFiles = new ArrayList<>();
+
+    USBFileManager(){
+
+    }
+
+    private static void getUsbPath(Context context) {
+        StorageManager stMgr = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
+        Class<StorageManager> srgMgrClass = StorageManager.class;
+        try {
+            String[] paths = (String[]) srgMgrClass.getMethod("getVolumePaths")
+                    .invoke(stMgr);
+            for (String path : paths) {
+                Object volumeState = srgMgrClass.getMethod("getVolumeState", String.class)
+                        .invoke(stMgr, path);
+                if (!path.contains("emulated") && Environment.MEDIA_MOUNTED.equals(volumeState))
+                    usbPaths.add(path);
+            }
+        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    public static List<HashMap<String, String>> GetVideoFile(Context context) {
+        if(!videoFiles.isEmpty())
+            return videoFiles;
+
+        if(usbPaths.isEmpty())
+            getUsbPath(context);
+
+        if(!usbPaths.isEmpty()) {
+            String usbDrivePath = usbPaths.get(0);
+            File usbDirectory = new File(usbDrivePath);
+            if (usbDirectory.exists() && usbDirectory.isDirectory()) {
+                // 递归遍历文件夹并查找视频文件
+                searchForVideos(usbDirectory);
+            }
+        }
+
+        return videoFiles;
+    }
+
+    private static void searchForVideos(File directory) {
+        File[] files = directory.listFiles();
+        if (files != null) {
+            for (File file : files) {
+                if (file.isDirectory()) {
+                        // 递归遍历子目录
+                    searchForVideos(file);
+                }
+                else {
+                        // 检查文件扩展名以确定是否为视频文件
+                    String fileName = file.getName().toLowerCase();
+                    if (fileName.endsWith(".mp4") || fileName.endsWith(".mkv") || fileName.endsWith(".avi")) {
+                        HashMap<String, String> hmap = new HashMap<>();
+                        String fname = file.getName();
+                        int dotIndex = fname.indexOf('.');
+                        String name = fname.substring(0, dotIndex);
+                        hmap.put("name", name);
+                        hmap.put("path", file.getAbsolutePath());
+                        videoFiles.add(hmap);
+                    }
+                }
+            }
+        }
+    }
+}