Trigger an audio file when call is answered

后端 未结 2 1665
没有蜡笔的小新
没有蜡笔的小新 2020-12-16 03:40

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 co

2条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-16 04:19

      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 ");
    

提交回复
热议问题