LuajavaBridge 声音捕捉

首先通过luajavabridge,从Lua的代码中切换到Android下面,调用Android的声音捕捉相关代码,然后android端将是否成功捕捉到一定大小的声音结果返回给Lua。

Lua端的相关调用操作:

 1 local aboutBtn = ui.newImageMenuItem({
 2         image = aboutImgPath,
 3         imageSelected = aboutImgSelectedPath,
 4         x = CommonAPI:getFixedX(180) -CommonAPI:getCenterX(),
 5         y = CommonAPI:getFixedY(80) -CommonAPI:getCenterY(),
 6         listener = function()  
 7              -- call Java method
 8             local javaClassName = "com.quick_x.sample.luajavabridge.Luajavabridge"
 9             local javaMethodName = "catchVoice"
10             local javaParams = {
11                 device.cachePath,
12                 "I‘m great !",
13                 function(event)
14                     printf("Java method callback value is [%s]", event)
15                     textInput:setText(string.format("%s", event))
16                 end
17             }
18             local javaMethodSig = "(Ljava/lang/String;Ljava/lang/String;I)V"
19             luaj.callStaticMethod(javaClassName, javaMethodName, javaParams, javaMethodSig)
20         end
21     })

Android下面的操作:(    使用MediaRecorder来记录声音,注意这里的线程切换跟官方给的方式不一样,不过是可行的。
    开启一个独立的线程CheckMicophoneVolume,用来监听外界声音的大小,达到一定分贝,触发函数,返回给Lua结果。
    使用完MediaRecoder以后,记得stop(),然后释放它。不然第二次通过Lua来调用的时候会报错、)

----------------------------------------------------

注意我这里的cachePath是通过Lua传过来的。
cachePath的值是大概这样的:/data/data/项目包名/files/
我的就是:/data/data/com.quick_x.sample.luajavabridge/files/

---------------------------------------------------------------

 1 public class Luajavabridge extends Cocos2dxActivity {
 2     static private Luajavabridge s_instance;
 3 
 4     static int callbackFunction;
 5     static MediaRecorder mRecorder;
 6 
 7     @Override
 8     public void onCreate(Bundle savedInstanceState) {
 9         super.onCreate(savedInstanceState);
10         s_instance = this;
11     }
12 
13     static {
14         System.loadLibrary("game");
15     }
16 
17     public static void catchVoice(final String cachePath, final String message,
18             final int luaCallbackFunction) {
19         callbackFunction = luaCallbackFunction;
20 
21         s_instance.runOnUiThread(new Runnable() {
22             @Override
23             public void run() {
24                 try {
25                     File soundFile = new File(cachePath+"audio.3gp");
26                     
27                     if(mRecorder == null){
28                         mRecorder = new MediaRecorder();
29                         mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
30                         mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
31                         mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
32                         mRecorder.setOutputFile(soundFile.getAbsolutePath());
33                     }
34                     mRecorder.prepare();
35                     mRecorder.start();
36                 } catch (IOException e) {
37                     e.printStackTrace();
38                 }
39 
40                 CheckMicophoneVolume thread = new CheckMicophoneVolume();
41                 thread.start();
42             }
43         });
44     }
45 
46     public static void catchVoiceSuccess() {
47         mRecorder.stop();
48         mRecorder = null;
49         s_instance.runOnGLThread(new Runnable() {
50             @Override
51             public void run() {
52                 Cocos2dxLuaJavaBridge.callLuaFunctionWithString(
53                         callbackFunction, "声音捕捉成功");
54                 Cocos2dxLuaJavaBridge.releaseLuaFunction(callbackFunction);
55             }
56         });
57     }
58 
59     private static class CheckMicophoneVolume extends Thread {
60         private volatile boolean running = true;
61 
62         public void exit() {
63             running = false;
64         }
65 
66         @Override
67         public void run() {    
68             while (running) {
69                 try {
70                     Thread.sleep(200);
71                 } catch (InterruptedException e) {
72                     e.printStackTrace();
73                 }
74             
75                 if (mRecorder == null || !running) {
76                     break;
77                 }
78         
79                 int x = mRecorder.getMaxAmplitude();
80                 Log.v("X_value", ""+x);
81                 if (x != 0) {
82                     int f = (int) (10 * Math.log(x) / Math.log(10));
83                     Log.v("value", ""+f);
84                     if (f > 40) {
85                         catchVoiceSuccess();
86                         exit();
87                     }
88                 }
89             }
90         }
91     }
92 }

注意添加权限文件:

    <uses-permission android:name="android.permission.RECORD_AUDIO"/>
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>  
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

LuajavaBridge 声音捕捉,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。