如何使用接近传感器和音频管理器来减少Android设备的音量

时间:2014-09-17 03:39:20

标签: android audio proximity

我想在应用程序中实现一项功能,当用户在他的Android手机中播放音频时,它会更接近他的耳朵,音量应该减少,音频应该通过耳机播放。 (类似于当用户收听语音笔记并将其移近耳朵时应用程序正在执行的操作。)请在以下代码中建议出现问题?谢谢你的帮助。

public class SensorActivity extends Activity implements SensorEventListener {
private SensorManager mSensorManager;
private Sensor mSensor;
ImageView iv;
Button b;
MediaPlayer player;
private AudioManager mAudioManager;
public boolean isHeadsetOn;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sensor);
    mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
    iv = (ImageView) findViewById(R.id.image);
    b = (Button) findViewById(R.id.audio);
    mAudioManager = (AudioManager) SensorActivity.this.getSystemService(Context.AUDIO_SERVICE);
    mAudioManager.setMode(AudioManager.STREAM_MUSIC);

    b.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if (player != null && player.isPlaying()) {
                player.stop();
                player.release();
            } else {
                AssetFileDescriptor afd;
                try {
                    afd = getAssets().openFd("Kraddy Android Porn.mp3");
                    player = new MediaPlayer();
                    player.setDataSource(afd.getFileDescriptor());
                    player.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);
                    player.prepare();

                    player.start();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

        }
    });
}

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

}

protected void onPause() {
    super.onPause();

    try {
        // unregisterReceiver(mReciever);
        mSensorManager.unregisterListener(this);
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (player != null && player.isPlaying()) {
        player.stop();
        player.release();
    }
}

public void onAccuracyChanged(Sensor sensor, int accuracy) {
}

public void onSensorChanged(SensorEvent event) {
    Log.e("distance", String.valueOf(event.values[0]));
    if (event.values[0] == 0) {
        iv.setImageResource(R.drawable.near);
        mAudioManager.setSpeakerphoneOn(false);
    } else {
        iv.setImageResource(R.drawable.far);
        mAudioManager.setSpeakerphoneOn(true);

    }
}

}

1 个答案:

答案 0 :(得分:1)

以下代码完全符合我在帖子中提到的要求。

package com.example.proximitysensordemo;

import java.io.IOException;

import android.app.Activity;
import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class SensorActivity extends Activity implements SensorEventListener {
    private SensorManager mSensorManager;
    private Sensor mSensor;
    ImageView iv;
    Button b;
    MediaPlayer player;
    private AudioManager mAudioManager;
    public boolean isHeadsetOn;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sensor);
    mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
    iv = (ImageView) findViewById(R.id.image);
    b = (Button) findViewById(R.id.audio);
    mAudioManager=(AudioManager)getSystemService(Context.AUDIO_SERVICE);

    b.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if (player != null && player.isPlaying()) {
                player.pause();
            } else {
                AssetFileDescriptor afd;
                try {
                    if (player == null) {
                        afd = getAssets().openFd("Kraddy Android Porn.mp3");
                        player = new MediaPlayer();
                        player.setDataSource(afd.getFileDescriptor());
                        player.setAudioStreamType(AudioManager.STREAM_MUSIC);
                        player.prepare();
                        player.start();
                    } else {
                        player.start();
                    }

                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

        }
    });
}

protected void onResume() {
    super.onResume();
    mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_FASTEST);
    mAudioManager.setMode(AudioManager.MODE_IN_CALL);
}

protected void onPause() {
    super.onPause();
    mAudioManager.setMode(AudioManager.MODE_NORMAL);
    try {
        // unregisterReceiver(mReciever);
        mSensorManager.unregisterListener(this);
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (player != null) {
        player.stop();
        player.release();
        player = null;
    }
}

@Override
protected void onStop() {
    // TODO Auto-generated method stub
    super.onStop();
}

public void onAccuracyChanged(Sensor sensor, int accuracy) {
}

public void onSensorChanged(SensorEvent event) {
    Log.e("distance", String.valueOf(event.values[0]));
    Log.e("MaximumRange", String.valueOf(mSensor.getMaximumRange()));

    if (event.values[0] < mSensor.getMaximumRange()) {
        iv.setImageResource(R.drawable.near);
        mAudioManager.setSpeakerphoneOn(false);

    } else {
        iv.setImageResource(R.drawable.far);
        mAudioManager.setSpeakerphoneOn(true);
    }
}

}

希望这会有所帮助。谢谢。
PS:不要忘记取消注册传感器,因为android系统在使用后不会自动释放传感器。如果您忘记这样做,音乐播放器和其他使用音乐播放器的应用程序将表现得很奇怪。