【问题标题】:Trigger an audio file when call is answered接听电话时触发音频文件
【发布时间】:2011-06-18 11:59:33
【问题描述】:

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

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

【问题讨论】:

  • 您好,我开发了以下应用程序,它做同样的事情,如果它解决了您的问题,请检查此应用程序,然后我可以将源代码发送给您。 market.android.com/…
  • @KPBird,这看起来可以解决问题。如果您确实将代码发送给我们进行审查,我将不胜感激。谢谢你的提议。
  • 你好我今天上传代码...
  • @KPBird 您好,我想在我的应用程序中执行相同的任务。你能告诉我如何实现这个吗?实际上我的需求是“假设我们在手机中录制了文件,当我们给任何人打电话时,他一接电话就应该听我们录制的录音文件”。请尽快重播我。谢谢
  • @KPBird :如果您提供一些逻辑提示并分享您的代码,我将不胜感激:)

标签: android audio phone-call


【解决方案1】:

首先,您需要设置一个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);

我希望这会有所帮助!

【讨论】:

    【解决方案2】:
      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 ");
    

    【讨论】:

      猜你喜欢
      • 2011-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-11
      • 1970-01-01
      • 1970-01-01
      • 2012-10-07
      相关资源
      最近更新 更多