-2

Here is my code I want turn on headphone and Speaker both at same time Kindly edit my code as i'm beginner in programming also I try to use many times AudioManager.Mode_Stream but when I do I hear nothing

  package meracal.myapplicationlive;

import android.content.Context;
import android.media.audiofx.AcousticEchoCanceler;
import android.provider.MediaStore;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.media.*;
import android.view.View;
import android.widget.Button;


public class MainActivity extends ActionBarActivity  {
    Button start, stop;
    boolean isRecording = false;
    AudioManager am = null;
    AudioRecord record = null;
    AudioTrack track = null;
    MediaPlayer mplayer = null;
    MediaRecorder mrecord = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        initRecordAndTrack();

        am = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
        am.setMode(AudioManager.STREAM_RING);
        am.setSpeakerphoneOn(true);
        am.setBluetoothScoOn(true);
        am.startBluetoothSco();
        am.setBluetoothScoOn(true);

        (new Thread() {
            @Override
            public void run() {
                recordAndPlay();
            }
        }).start();

         start = (Button) findViewById(R.id.btnstart);
        start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isRecording) {
                    startRecordAndPlay();
                }
            }
        });
         stop = (Button) findViewById(R.id.btnsstop);
        stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isRecording) {
                    stopRecordAndPlay();
                }
            }
        });
    }

    private void initRecordAndTrack() {
        int min = AudioRecord.getMinBufferSize(44100, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
        record = new AudioRecord(MediaRecorder.AudioSource.MIC, 44100, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT,
                min);

        track = new AudioTrack(AudioManager.MODE_IN_COMMUNICATION, 44100, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT, min,
                AudioTrack.MODE_STREAM);
    }

    private void recordAndPlay() {
        short[] lin = new short[1024];
        int num = 0;
        am.setMode(AudioManager.MODE_IN_COMMUNICATION);
        am.setSpeakerphoneOn(true);
        while (true) {
            if (isRecording) {
                num = record.read(lin, 0, 1024);
                track.write(lin, 0, num);

            }
        }
    }

    private void startRecordAndPlay() {
        record.startRecording();
        track.play();

        am.setSpeakerphoneOn(true);
        isRecording = true;
    }

    private void stopRecordAndPlay() {
        record.stop();
        track.pause();
        isRecording = false;
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267

2 Answers2

0

This code here would probably work to force the audio through the speakers:

audioManager.setMode(AudioManager.MODE_IN_CALL);
audioManager.setSpeakerphoneOn(true);
alex23434
  • 389
  • 5
  • 12
0

This worked for me

First, I added permissions in Manifest.xml

<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />

Then, I used this:

AudioManager m_amAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);

    m_amAudioManager.setMode(AudioManager.MODE_RINGTONE | AudioManager.MODE_IN_CALL);
    m_amAudioManager.setSpeakerphoneOn(true);