问题的标题可能看起来重复,但我的问题有时是问题并导致故障.插入耳机时,我使用以下代码播放扬声器.
AudioManager audioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);
audioManager.setMode(AudioManager.STREAM_MUSIC);
audioManager.setSpeakerphoneOn(true);
if(! Globals.mediaPlayer.isPlaying()){
Globals.mediaPlayer.start();
}
Run Code Online (Sandbox Code Playgroud)
上面的代码通过以下方式播放音频:1.很少次,它播放完美.2.大多数时候,它在背景中播放循环声.几次,它什么都不玩.
插入耳机时似乎系统声音播放没有错误.例如 - 设置铃声正确播放相应的铃声,没有任何故障.请帮我理解如何通过插入耳机的扬声器正确播放声音.
在我们的应用程序中,我想使用Android v4.2或更高版本连接到之前配对的A2DP蓝牙扬声器并直接播放音频.
我可以使用此代码成功创建A2DP配置文件对象以启动该过程:
/* Manifest permissions */
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
Run Code Online (Sandbox Code Playgroud)
// Get the default adapter
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
// Establish connection to the proxy.
mBluetoothAdapter.getProfileProxy(this, mProfileListener, BluetoothProfile.A2DP)
Run Code Online (Sandbox Code Playgroud)
并且以下监听器响应连接:
private BluetoothProfile.ServiceListener mProfileListener = new BluetoothProfile.ServiceListener() {
public void onServiceConnected(int profile, BluetoothProfile proxy) {
if (profile == BluetoothProfile.A2DP) {
mBluetoothSpeaker = (BluetoothA2dp) proxy;
// no devices are connected
List<BluetoothDevice> connectedDevices = mBluetoothSpeaker.getConnectedDevices();
//the one paired (and disconnected) speaker is returned here
int[] statesToCheck = {BluetoothA2dp.STATE_DISCONNECTED};
List<BluetoothDevice> disconnectedDevices = mBluetoothSpeaker.getDevicesMatchingConnectionStates(statesToCheck);
BluetoothDevice …Run Code Online (Sandbox Code Playgroud)