Play audio file during a call, however the person

2019-03-22 07:50发布

问题:

I want to play audio file during a call and i'm able to play it but when i make a call it play file only on that device on which i have installed the application. here is my sample code by which i have played the audio file.

 public void play() {

    bufferSize = AudioTrack.getMinBufferSize(16000,
            AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT);

    AudioTrack at = null;
    at = new AudioTrack(AudioManager.STREAM_VOICE_CALL,
            16000, AudioFormat.CHANNEL_CONFIGURATION_MONO,
            AudioFormat.ENCODING_PCM_16BIT, bufferSize, AudioTrack.MODE_STREAM);

    String filePath = Environment.getExternalStorageDirectory()
            .getAbsolutePath();
    String file = filePath + "/" + "myfile.wav";

            int i = 0;
    byte[] s = new byte[bufferSize];
    try {
        FileInputStream fin = new FileInputStream(file);
        BufferedInputStream bis = new BufferedInputStream(fin, 44000);
        DataInputStream dis = new DataInputStream(bis);

        at.play();

        while ((i = dis.read(s, 0, bufferSize)) > -1) {
            at.write(s, 0, i);

        }

        at.stop();
        at.release();
        dis.close();
        fin.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

This code works fine with me. But i want that this file would be able to hear by the other end person who make call. how could i do it??? Any idea?? i know android API doesn't allow to do it..but anyone who did it?? please give only genuine and accurate answer not assumptions.