接听电话时触发音频文件

| 在接听要播放的电话时,是否有一种方法可以启动音频文件,而不是打入通话中(因此对方可以听到),而只能在通话扬声器中(因此只有我们这一边可以听到)。 听起来很奇怪,我知道,但这是更大的应用程序的一部分。     
已邀请:
        首先,您需要设置一个BroadcastReceiver(我们将其称为\“ CallReceiver \”),并具有了解电话状态的权限(直观地,添加权限为“ 0”)。 像这样注册您的CallReceiver操作。
<receiver android:name=\".CallReceiver\" android:enabled=\"true\">
    <intent-filter>
        <action android:name=\"android.intent.action.PHONE_STATE\"></action>
    </intent-filter>
</receiver>
在CallReceiver上,您可以决定应播放音频的哪些动作(传入/传出/电话响铃...),因此只需阅读EXTRA_STATE和getCallState()(请参阅TelephonyManager文档)。 关于音频,在播放声音之前,您将需要使用AudioManager,并设置播放的“通话中”模式。
private AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);  
am.setMode(AudioManager.MODE_IN_CALL); 
am.setSpeakerphoneOn(false);
我希望这有帮助!     
        
  private void PlayShortAudioFileViaAudioTrack(String filePath) throws IOException {
 // We keep temporarily filePath globally as we have only two sample sounds now..
         if (filePath == null)
             return;

 //Reading the file..
         byte[] byteData = null;
         File file = null;
         file = new File(filePath); // for ex. path= \"/sdcard/samplesound.pcm\" or \"/sdcard/samplesound.wav\"
         byteData = new byte[(int) file.length()];
         FileInputStream in = null;
         try {
             in = new FileInputStream(file);
             in.read(byteData);
             in.close();

         } catch (FileNotFoundException e) {
 // TODO Auto-generated catch block
             e.printStackTrace();
         }
 // Set and push to audio track..
         int intSize = android.media.AudioTrack.getMinBufferSize(8000, AudioFormat.CHANNEL_CONFIGURATION_MONO,
                 AudioFormat.ENCODING_PCM_8BIT);
         AudioTrack at = new AudioTrack(AudioManager.STREAM_VOICE_CALL, 8000, AudioFormat.CHANNEL_CONFIGURATION_MONO,
                 AudioFormat.ENCODING_PCM_8BIT, intSize, AudioTrack.MODE_STREAM);
         if (at != null) {
             at.play();
 // Write the byte array to the track
             at.write(byteData, 0, byteData.length);
             at.stop();
             at.release();
         } else
             Log.d(\"TCAudio\", \"audio track is not initialised \");
    

要回复问题请先登录注册