基於外置UAC音頻設備作的一款錄音Android Demo

UAC音頻設備測試java

1,使用BuildLibUSB項目編譯libusb爲so文件android

2,在Android Studio 4.1.1中,新建一個項目,選擇empty activitygit

3,在app目錄中,新建jniLibs\libusb-1.0\lib目錄,拷貝第1步生成的so文件到jniLibs\libusb-1.0\lib目錄,分cpu類型存放app

4,在jniLibs\libusb-1.0目錄中,新建include目錄,從uildLibUSB項目中找到libusb.h並拷貝過來 5,在jniLibs\libusb-1.0目錄中,新建Android.mk文件,內容爲:ide

LOCAL_PATH := $(call my-dir)oop

include $(CLEAR_VARS)
LOCAL_MODULE := libusb-1.0測試

LOCAL_SRC_FILES := lib/$(TARGET_ARCH_ABI)/libusb-1.0.so
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include/libusb-1.0
LOCAL_EXPORT_LDLIBS := -lloggradle

include $(PREBUILT_SHARED_LIBRARY)
6,在BuildLibUSB項目中找到freedv.zip,解壓到app\jniLibs目錄中ui

7,新建java文件,內容爲:this

public class UsbAudio {
static {
System.loadLibrary("usbaudio");
}

public native boolean setup();
public native void close();
public native void loop();
public native boolean stop();
public native int measure();

}
8,修改調用so文件的c語言文件,替換com_luoxi_uac,com.luoxi.uac,com/luoxi/uac等包含包名的字符串爲本身的

9,新增MainActivity,並修改AndroidManifest.xml文件中的activity標籤爲:

<activity
        android:name="com.luoxi.uacdemo.MainActivity"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

10,修改app的build.gradle文件,在android域裏面新增NDK相關設置

ndkPath "E:\\android-ndk-r21b"

sourceSets {
    main {
        jniLibs.srcDirs = ["jniLibs"]
    }
}
externalNativeBuild {
    ndkBuild {
        path "jniLibs/Android.mk"
    }
}

11,拷貝usbhelper到java目錄下,usbhelper能夠從BuildLibUSB項目根目錄中找到

12,修改MainActivity的OnCreate方法,增長初始化代碼,相似於:

mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
    HashMap<String, UsbDevice> deviceList = mUsbManager.getDeviceList();
    Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
    while(deviceIterator.hasNext()){
        UsbDevice device = deviceIterator.next();
        UsbInterface intf = device.getInterface(0);
        if (intf.getInterfaceClass() == UsbConstants.USB_CLASS_AUDIO) {
            mAudioDevice = device;
        }
    }
    System.loadLibrary("usb-1.0");
    UsbHelper.useContext(getApplicationContext());
    mUsbAudio = new UsbAudio();
    AudioPlayback.setup();

13,在相應位置(例如在mainActivity的按鈕事件偵聽方法中)增長聲音捕獲代碼,例如:

try{
                if (mUsbAudio.setup() == true) {

                }
            }catch (Exception e){
                e.printStackTrace();
            }

            new Thread(new Runnable() {
                public void run() {
                    while (true) {
                        try {
                            mUsbAudio.loop();
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                    }
                }
            }).start();

14,在相應位置增長聲音捕獲中止代碼,例如:

mUsbAudio.stop();
            mUsbAudio.close();

15,在相應位置增長Android操做系統的權限申請代碼,例如:

mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
    IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
    mUsbPermissionReciever = new UsbReciever();
    registerReceiver(mUsbPermissionReciever, filter);
    if (mAudioDevice != null && mPermissionIntent != null) {
        mUsbManager.requestPermission(mAudioDevice, mPermissionIntent);
    }

16,在相應位置增長註銷代碼,例如:

unregisterReceiver(mUsbPermissionReciever);
    if (mUsbAudio != null) {
        mUsbAudio.stop();
        mUsbAudio.close();
    }

17,在相應位置增長USB設備變更廣播偵聽器,例如:

private class UsbReciever extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        UsbDevice device = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
        if (ACTION_USB_PERMISSION.equals(action)) {
            if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
                setDevice(device);
            }
        }
    }
}

18,處理其餘問題,後編譯運行
源代碼:https://gitee.com/radarfyh/multimedia-system/tree/master/TestUAC1

相關文章
相關標籤/搜索