u013513228
小胖_
采纳率0%
2016-04-15 14:05 阅读 2.5k

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

20

@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条回答 默认 最新

  • Wei_Smile 李牧羊 2016-04-15 14: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
    
        }
    
    }
    
    点赞 评论 复制链接分享

相关推荐