2015-06-10 145 views
1

在我的应用程序中,有一个使用设备音频插孔的读卡器。现在,我的问题是我想在内置扬声器发出声音时读卡器在设备的HeadPhone插孔中完好无损。安卓耳机插入耳机插孔时通过内置扬声器发出的音频

以下是我已经试过代码:使用setMode方法

mAudioManager.setMode(AudioManager.MODE_IN_CALL); 
       mAudioManager.setSpeakerphoneOn(true); 
       float maxVolume = mAudioManager 
         .getStreamMaxVolume(AudioManager.STREAM_RING); 
       soundPool.play(soundID, maxVolume, maxVolume, 1, 0, 1f); 

1)使用反射方法

Class audioSystemClass; 
       try { 
        audioSystemClass = Class 
          .forName("android.media.AudioSystem"); 

        Method setForceUse = audioSystemClass.getMethod(
          "setForceUse", int.class, int.class); 
        // First 1 == FOR_MEDIA, second 1 == FORCE_SPEAKER. To go 
        // back to the default 
        // behavior, use FORCE_NONE (0). 
        setForceUse.invoke(null, 1, 1); 
        final float maxVolume = mAudioManager 
          .getStreamMaxVolume(AudioManager.STREAM_MUSIC); 
        soundPool.play(soundID, maxVolume, maxVolume, 1, 0, 1f); 


       } catch (ClassNotFoundException e) { 
        Log.e("Test", e.toString()); 
        e.printStackTrace(); 
       } catch (NoSuchMethodException e) { 
        Log.e("Test", e.toString()); 
        e.printStackTrace(); 
       } catch (IllegalAccessException e) { 
        Log.e("Test", e.toString()); 
        e.printStackTrace(); 
       } catch (IllegalArgumentException e) { 
        Log.e("Test", e.toString()); 
        e.printStackTrace(); 
       } catch (InvocationTargetException e) { 
        Log.e("Test", e.toString()); 
        e.printStackTrace(); 
       } 

2)但是,这些都码在只运行具有默认FM应用程序的设备。但我想在所有设备中具有此功能。

请分享您的经验!

+0

也许[这](http://stackoverflow.com/questions/2917184/forcing-sound-output-through-speaker-in-android)会帮助你,它比你尝试什么略有不同。 – Strider

回答

0

另外,如果你初始化你的Soundpool到STREAM_VOICE_CALL,如:

SoundPool spool = new SoundPool(1,AudioManager.STREAM_VOICE_CALL, 0)

然后还你的音频应改为与任何您所提到的方式扬声器。我尝试过,甚至在没有默认FM的情况下在手机上工作。

+0

感谢哥们....它的工作。 – lifelongLearner

+0

嘿,你可以提出任何可能的原因,为什么上面的代码没有在三星4.4.2版本上运行,但它在Nexus设备上工作? – lifelongLearner

+0

在三星设备中,他们可能已经调整了音频参数,这就是音频api行为不同的原因。但它只是我的猜测。通过将soundpool设置为stream_voice调用,它可能适用于大部分手机,因为您重写了gsm调用行为! – Saurav

0

你的第二种方法setMode保持正确的手机与默认的Fm播放器。

但是对于没有预设FM的设备,我想下面的代码:

mAudioManager.setMode(AudioManager.MODE_IN_COMMUNICATION); 
mAudioManager.setSpeakerphoneOn(true); 

此代码工作得很好的手机,但没有默认FM player.I使用氧气OS一万普拉斯设备和工作的罚款。希望这可以帮助。

0

在这里,我发起了一个SoundPool并加载了一声蜂鸣声。

SoundPool soundPool = new SoundPool(10, AudioManager.STREAM_VOICE_CALL, 0); 

      soundPool 
        .setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() { 

         @Override 
         public void onLoadComplete(SoundPool soundPool, 
                int sampleId, int status) { 
          loaded = true; 
         } 
        }); 
      soundID = soundPool.load(ApplicationChexology.getContext(), R.raw.beep_single, 1); 

现在,加载声音后,我将AudioManager设置为MODE_IN_COMMUNICATION并播放声音。播放声音时,将音频管理器的模式设置为MODE_NORMAL。

audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION); 
audioManager.setSpeakerphoneOn(true); 

float maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC); 
     if (loaded) 
     soundPool.play(soundID, maxVolume, maxVolume, 1, 0, 1f); 

     Timer timer = new Timer(); 

     TimerTask delayedThreadStartTask = new TimerTask() { 
      @Override 
      public void run() { 
      audioManager.setMode(AudioManager.MODE_NORMAL); 
      audioManager.setSpeakerphoneOn(false); 

       }}; 
    timer.schedule(delayedThreadStartTask, 500); 
相关问题