android 听筒模式下 无法播放声音 20C

@Override
public void onSensorChanged(SensorEvent event) {
float range = event.values[0];
if (range == mSensor.getMaximumRange()){
Toast.makeText(this, "正常模式", Toast.LENGTH_LONG).show();
audioManager.setMode(AudioManager.MODE_NORMAL);
}else {
Toast.makeText(this, "听筒模式", Toast.LENGTH_LONG).show();
audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
}
}

 在听筒模式下   播放录音  但是没有声音    当切换成正常模式的时候   声音播放出来了

1个回答

 public class MainActivity extends Activity implements SensorEventListener {
    private AudioManager audioManager;
    private SensorManager mSensorManager;
    private Sensor mSensor;

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

    private void init() {
        audioManager = (AudioManager) this
                .getSystemService(Context.AUDIO_SERVICE);
        mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);

        MediaPlayer mPlayer = new MediaPlayer();
        try {
            mPlayer.reset();
            mPlayer.setDataSource(new FileInputStream("/sdcard/snow.mp3").getFD());
            mPlayer.prepare();
            mPlayer.start();
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    protected void onResume() {
        mSensorManager.registerListener(this, mSensor,
                SensorManager.SENSOR_DELAY_NORMAL);
        super.onResume();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        mSensorManager.unregisterListener(this);
        super.onPause();
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        // TODO Auto-generated method stub
        float range = event.values[0];

        if (range == mSensor.getMaximumRange()) {
            Toast.makeText(this, "正常模式", Toast.LENGTH_LONG).show();
             audioManager.setMode(AudioManager.MODE_NORMAL);
        } else {
            Toast.makeText(this, "听筒模式", Toast.LENGTH_LONG).show();
             audioManager.setMode(AudioManager.MODE_IN_CALL);
        }
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        // TODO Auto-generated method stub

    }

}
Wei_Smile
李牧羊 回复wsad145: 给你找了一个别人写的听筒模式,你看看是不是你哪里没写对
4 年多之前 回复
u013513228
小胖_ 听筒模式下 没有声音
4 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问