如何使用zxing库启用蜂鸣声?

时间:2018-04-02 16:29:35

标签: android

我用过的实现是

implementation 'me.dm7.barcodescanner:zxing:1.9'

我没有使用IntentIntegrator,所以请给我一些建议

3 个答案:

答案 0 :(得分:1)

以上评论中提供的代码会获得默认的铃声:

Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(ba.context, notification);
r.play();

相反,您应该通过向项目中的原始文件夹添加声音来将Uri添加到您想要的声音中:

@Override
public void handleResult(Result rawResult) {
    try {
        Uri beepSound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + File.pathSeparator + File.separator + getPackageName() + "/raw/beep.wav");
        Ringtone r = RingtoneManager.getRingtone(context, beepSound);
        r.play();

            ... other barcode scanning result handling
    } catch (Exception e) ....

或使用ToneGenerator:

@Override
public void handleResult(Result rawResult) {
    try {
        ToneGenerator tg = new ToneGenerator(AudioManager.STREAM_NOTIFICATION, 100);
        tg.startTone(ToneGenerator.TONE_PROP_BEEP);

        ... other barcode scanning result handling
    } catch (Exception e) ....

答案 1 :(得分:0)

尝试

IntentIntegrator.forFragment(this) 
                .setBeepEnabled(true);

答案 2 :(得分:0)

@Rajan Mourya,将您的代码更改为此

try {
        Uri sound = Uri.parse("android.resource://" + MainActivity.context.getPackageName() + "/" + R.raw.t_tone);
        Ringtone r = RingtoneManager.getRingtone(MainActivity.context, sound);
        r.play();
    } catch (Exception e){

    }

其中raw是在res下包含您的声音文件的已创建文件夹