插入耳机时强制音频到扬声器?

Posted

技术标签:

【中文标题】插入耳机时强制音频到扬声器?【英文标题】:Force audio to speaker when headphone is plugged in? 【发布时间】:2015-11-08 17:41:14 【问题描述】:

这是我想要同时打开耳机和扬声器的代码 请编辑我的代码,因为我是编程的初学者,我也尝试多次使用 AudioManager.Mode_Stream 但当我这样做时我什么也听不到

  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;
    

【问题讨论】:

有一个硬件旁路。我不认为你能解决这个问题。 你知道如何绕过硬件吗?或如何更改音频管理器的音频系统类?? 【参考方案1】:

这里的代码可能会强制音频通过扬声器:

audioManager.setMode(AudioManager.MODE_IN_CALL);
audioManager.setSpeakerphoneOn(true);

【讨论】:

【参考方案2】:

这对我有用

首先,我在 Manifest.xml 中添加了权限

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

然后,我使用了这个:

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

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

【讨论】:

以上是关于插入耳机时强制音频到扬声器?的主要内容,如果未能解决你的问题,请参考以下文章

Android,插入耳机后如何将音频路由到扬声器?

AVplayer 音频路由到扬声器和耳机

电脑显示未插入扬声器和耳机?

iPhone音频播放:强制通过内部扬声器?

Android蓝牙耳机/扬声器音频切换

Android蓝牙耳机/扬声器音频切换