2 u013513228 u013513228 于 2016.04.15 22:05 提问

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);
}
}

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

2个回答

CSDNXIAON
CSDNXIAON   2016.04.15 22:12

android 听筒播放模式
Android Receiver(听筒模式和扬声器模式,铃音模式)
----------------------同志你好,我是CSDN问答机器人小N,奉组织之命为你提供参考答案,编程尚未成功,同志仍需努力!

Wei_Smile
Wei_Smile   2016.04.15 22:13
 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
Wei_Smile 回复wsad145: 给你找了一个别人写的听筒模式,你看看是不是你哪里没写对
一年多之前 回复
u013513228
u013513228 听筒模式下 没有声音
一年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!