接听电话时触发音频文件

发布于 2024-11-15 19:51:24 字数 109 浏览 5 评论 0原文

有没有办法在接听电话时启动音频文件,而不是在通话中播放(以便另一方可以听到),而仅在通话扬声器中播放(以便只有我们这边可以听到)。

听起来很奇怪,我知道,但它是一个更大的应用程序的一部分。

Is there a way to launch an audio file when answering a call to be played NOT into the call (so the other side could hear), but only in the call speaker (so only our side could hear).

Sounds strange, I know but it is part of a much larger app.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怀念你的温柔 2024-11-22 19:51:24

首先,您需要设置一个 BroadcastReceiver (让我们称之为“CallReceiver”),以及了解手机状态的权限(直观上,添加的权限是android.permission.READ_PHONE_STATE)。

像这样注册您的 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);

我希望这有帮助!

First of all, you'll need to set up a BroadcastReceiver (let's call it "CallReceiver"), and permission to know about the phone state (intuitively, the permission to add is android.permission.READ_PHONE_STATE).

Register your CallReceiver action like this.

<receiver android:name=".CallReceiver" android:enabled="true">
    <intent-filter>
        <action android:name="android.intent.action.PHONE_STATE"></action>
    </intent-filter>
</receiver>

At your CallReceiver, you may decide upon which actions should your audio play back (incoming/outcoming/phone ringing...), so just read the EXTRA_STATE, and getCallState() (check out the TelephonyManager docs).

About the audio, you will need to use the AudioManager, and set the "in call" mode of playback before playing the sound.

private AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);  
am.setMode(AudioManager.MODE_IN_CALL); 
am.setSpeakerphoneOn(false);

I hope this helps!

梦晓ヶ微光ヅ倾城 2024-11-22 19:51:24
  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 ");
  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 ");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文