下面列出了android.media.AudioManager#STREAM_VOICE_CALL 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static void logAudioStateVolume(String tag, AudioManager audioManager) {
final int[] streams = {
AudioManager.STREAM_VOICE_CALL,
AudioManager.STREAM_MUSIC,
AudioManager.STREAM_RING,
AudioManager.STREAM_ALARM,
AudioManager.STREAM_NOTIFICATION,
AudioManager.STREAM_SYSTEM
};
Logging.d(tag, "Audio State: ");
// Some devices may not have volume controls and might use a fixed volume.
boolean fixedVolume = isVolumeFixed(audioManager);
Logging.d(tag, " fixed volume=" + fixedVolume);
if (!fixedVolume) {
for (int stream : streams) {
StringBuilder info = new StringBuilder();
info.append(" " + streamTypeToString(stream) + ": ");
info.append("volume=").append(audioManager.getStreamVolume(stream));
info.append(", max=").append(audioManager.getStreamMaxVolume(stream));
logIsStreamMute(tag, audioManager, stream, info);
Logging.d(tag, info.toString());
}
}
}
private static String streamTypeToString(int streamtype) {
switch (streamtype) {
case AudioManager.STREAM_ALARM:
return "ALARM";
case AudioManager.STREAM_DTMF:
return "DTMF";
case AudioManager.STREAM_MUSIC:
return "MUSIC";
case AudioManager.STREAM_NOTIFICATION:
return "NOTIFICATION";
case AudioManager.STREAM_RING:
return "RING";
case AudioManager.STREAM_SYSTEM:
return "SYSTEM";
case AudioManager.STREAM_VOICE_CALL:
return "VOICE_CALL";
default:
return "Unknown stream type; " + streamtype;
}
}
@Override protected int[] getStreamIcons(StreamControl sc) {
if (sc.streamType == STREAM_BLUETOOTH_SCO)
return new int[] { R.drawable.oppo_ic_audio_bt, R.drawable.oppo_ic_audio_bt_mute };
switch (sc.streamType) {
case AudioManager.STREAM_ALARM:
return new int[] { R.drawable.oppo_ic_audio_alarm, R.drawable.oppo_ic_audio_alarm_mute };
case AudioManager.STREAM_RING:
return new int[] { R.drawable.oppo_ic_audio_ring_notif, R.drawable.oppo_ic_audio_ring_notif_mute };
case AudioManager.STREAM_NOTIFICATION:
return new int[] { R.drawable.oppo_ic_audio_notification, R.drawable.oppo_ic_audio_notification_mute };
case AudioManager.STREAM_MUSIC:
return new int[] { R.drawable.oppo_ic_audio_media, R.drawable.oppo_ic_audio_media_mute };
case AudioManager.STREAM_VOICE_CALL:
return new int[] { R.drawable.oppo_ic_audio_phone, R.drawable.oppo_ic_audio_phone };
default:
return new int[] { R.drawable.oppo_ic_audio_vol, R.drawable.oppo_ic_audio_vol_mute };
}
}
private static String streamTypeToString(int stream) {
switch (stream) {
case AudioManager.STREAM_VOICE_CALL:
return "STREAM_VOICE_CALL";
case AudioManager.STREAM_MUSIC:
return "STREAM_MUSIC";
case AudioManager.STREAM_RING:
return "STREAM_RING";
case AudioManager.STREAM_ALARM:
return "STREAM_ALARM";
case AudioManager.STREAM_NOTIFICATION:
return "STREAM_NOTIFICATION";
case AudioManager.STREAM_SYSTEM:
return "STREAM_SYSTEM";
default:
return "STREAM_INVALID";
}
}
/**
* Get the stream id for in call track. Can differ on some devices. Current
* device for which it's different :
*
* @return
*/
public static int getInCallStream(boolean requestBluetooth) {
/* Archos 5IT */
if (Build.BRAND.equalsIgnoreCase("archos")
&& Build.DEVICE.equalsIgnoreCase("g7a")) {
// Since archos has no voice call capabilities, voice call stream is
// not implemented
// So we have to choose the good stream tag, which is by default
// falled back to music
return AudioManager.STREAM_MUSIC;
}
if (requestBluetooth) {
return 6; /* STREAM_BLUETOOTH_SCO -- Thx @Stefan for the contrib */
}
// return AudioManager.STREAM_MUSIC;
return AudioManager.STREAM_VOICE_CALL;
}
@Override
public int getIconResource() {
switch (volumeStreamType) {
case AudioManager.STREAM_MUSIC:
return R.drawable.quantum_ic_music_note_white_24;
case AudioManager.STREAM_VOICE_CALL:
return R.drawable.quantum_ic_phone_white_24;
case AudioManager.STREAM_RING:
return R.drawable.quantum_ic_vibration_white_24;
case AudioManager.STREAM_ALARM:
return R.drawable.quantum_ic_alarm_white_24;
case AudioManager.STREAM_ACCESSIBILITY:
return R.drawable.quantum_ic_accessibility_new_white_24;
}
return 0;
}
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() != Sensor.TYPE_PROXIMITY) return;
if (mediaPlayer == null || mediaPlayer.getPlaybackState() != Player.STATE_READY) return;
int streamType;
if (event.values[0] < 5f && event.values[0] != proximitySensor.getMaximumRange()) {
streamType = AudioManager.STREAM_VOICE_CALL;
} else {
streamType = AudioManager.STREAM_MUSIC;
}
if (streamType == AudioManager.STREAM_VOICE_CALL &&
mediaPlayer.getAudioStreamType() != streamType &&
!audioManager.isWiredHeadsetOn())
{
double position = mediaPlayer.getCurrentPosition();
double duration = mediaPlayer.getDuration();
double progress = position / duration;
if (wakeLock != null) wakeLock.acquire();
stop();
try {
play(progress, true);
} catch (IOException e) {
Log.w(TAG, e);
}
} else if (streamType == AudioManager.STREAM_MUSIC &&
mediaPlayer.getAudioStreamType() != streamType &&
System.currentTimeMillis() - startTime > 500)
{
if (wakeLock != null) wakeLock.release();
stop();
notifyOnStop();
}
}
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() != Sensor.TYPE_PROXIMITY) {
return;
}
if (AudioPlayer.player == null || !AudioPlayer.player.isPlaying()) {
return;
}
final int streamType;
if (event.values[0] < 5f && event.values[0] != proximitySensor.getMaximumRange()) {
streamType = AudioManager.STREAM_VOICE_CALL;
} else {
streamType = AudioManager.STREAM_MUSIC;
}
messageAdapter.setVolumeControl(streamType);
double position = AudioPlayer.player.getCurrentPosition();
double duration = AudioPlayer.player.getDuration();
double progress = position / duration;
if (AudioPlayer.player.getAudioStreamType() != streamType) {
synchronized (AudioPlayer.LOCK) {
AudioPlayer.player.stop();
AudioPlayer.player.release();
AudioPlayer.player = null;
try {
ViewHolder currentViewHolder = getCurrentViewHolder();
if (currentViewHolder != null) {
messageAdapter.getActivity().setVolumeControlStream(streamType);
play(currentViewHolder, currentlyPlayingMessage, streamType == AudioManager.STREAM_VOICE_CALL, progress);
}
} catch (Exception e) {
Log.d(Config.LOGTAG, "AudioPlayer Exception: " + e);
}
}
}
}
public SignalAudioManager(@NonNull Context context) {
this.context = context.getApplicationContext();
this.incomingRinger = new IncomingRinger(context);
this.outgoingRinger = new OutgoingRinger(context);
this.soundPool = new SoundPool(1, AudioManager.STREAM_VOICE_CALL, 0);
this.connectedSoundId = this.soundPool.load(context, R.raw.webrtc_completed, 1);
this.disconnectedSoundId = this.soundPool.load(context, R.raw.webrtc_disconnected, 1);
}
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() != Sensor.TYPE_PROXIMITY) {
return;
}
if (AudioPlayer.player == null || !AudioPlayer.player.isPlaying()) {
return;
}
final int streamType;
if (event.values[0] < 5f && event.values[0] != proximitySensor.getMaximumRange()) {
streamType = AudioManager.STREAM_VOICE_CALL;
} else {
streamType = AudioManager.STREAM_MUSIC;
}
messageAdapter.setVolumeControl(streamType);
double position = AudioPlayer.player.getCurrentPosition();
double duration = AudioPlayer.player.getDuration();
double progress = position / duration;
if (AudioPlayer.player.getAudioStreamType() != streamType) {
synchronized (AudioPlayer.LOCK) {
AudioPlayer.player.stop();
AudioPlayer.player.release();
AudioPlayer.player = null;
try {
ViewHolder currentViewHolder = getCurrentViewHolder();
if (currentViewHolder != null) {
play(currentViewHolder, currentlyPlayingMessage, streamType == AudioManager.STREAM_VOICE_CALL, progress);
}
} catch (Exception e) {
Log.w(Config.LOGTAG, e);
}
}
}
}
protected int getUserSettingAudioStreamType() {
// 听筒模式/扬声器模式
if (isEarPhoneModeEnable) {
return AudioManager.STREAM_VOICE_CALL;
} else {
return AudioManager.STREAM_MUSIC;
}
}
private static int toLegacyStreamType(AudioAttributes aa) {
// flags to stream type mapping
if ((aa.getFlags() & AudioAttributes.FLAG_AUDIBILITY_ENFORCED)
== AudioAttributes.FLAG_AUDIBILITY_ENFORCED) {
return STREAM_SYSTEM_ENFORCED;
}
if ((aa.getFlags() & FLAG_SCO) == FLAG_SCO) {
return STREAM_BLUETOOTH_SCO;
}
// usage to stream type mapping
switch (aa.getUsage()) {
case AudioAttributes.USAGE_MEDIA:
case AudioAttributes.USAGE_GAME:
case AudioAttributes.USAGE_ASSISTANCE_ACCESSIBILITY:
case AudioAttributes.USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
return AudioManager.STREAM_MUSIC;
case AudioAttributes.USAGE_ASSISTANCE_SONIFICATION:
return AudioManager.STREAM_SYSTEM;
case AudioAttributes.USAGE_VOICE_COMMUNICATION:
return AudioManager.STREAM_VOICE_CALL;
case AudioAttributes.USAGE_VOICE_COMMUNICATION_SIGNALLING:
return AudioManager.STREAM_DTMF;
case AudioAttributes.USAGE_ALARM:
return AudioManager.STREAM_ALARM;
case AudioAttributes.USAGE_NOTIFICATION_RINGTONE:
return AudioManager.STREAM_RING;
case AudioAttributes.USAGE_NOTIFICATION:
case AudioAttributes.USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
case AudioAttributes.USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
case AudioAttributes.USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
case AudioAttributes.USAGE_NOTIFICATION_EVENT:
return AudioManager.STREAM_NOTIFICATION;
case AudioAttributes.USAGE_UNKNOWN:
default:
return AudioManager.STREAM_MUSIC;
}
}
@Override
public boolean onInitRenderer() {
int bytesPerFrame = getRendererFormat().getChannelCount() * (BITS_PER_SAMPLE / 8);
readByteBuffer = ByteBuffer.allocateDirect(bytesPerFrame * (getRendererFormat().getSampleRate() / BUFFERS_PER_SECOND));
int channelConfig = channelCountToConfiguration(getRendererFormat().getChannelCount());
int minBufferSize = AudioRecord.getMinBufferSize(getRendererFormat().getSampleRate(), channelConfig, android.media.AudioFormat.ENCODING_PCM_16BIT);
audioTrack = new AudioTrack(AudioManager.STREAM_VOICE_CALL, getRendererFormat().getSampleRate(), channelConfig,
android.media.AudioFormat.ENCODING_PCM_16BIT, minBufferSize, AudioTrack.MODE_STREAM);
keepAliveRendererRunnable = true;
return true;
}
@Override
public void onCallHangUp() {
WebRtcPhone.getInstance().AnswerCall();
disconnect();
ToneGenerator tg2 = new ToneGenerator(AudioManager.STREAM_VOICE_CALL, 100);
tg2.startTone(ToneGenerator.TONE_PROP_BEEP2);
}
protected void createSliders() {
LOGI(TAG, "createSliders()");
StreamResources[] STREAMS = StreamResources.STREAMS;
Context mContext = getContext();
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Resources res = mContext.getResources();
for (int i = 0; i < STREAMS.length; i++) {
StreamResources streamRes = STREAMS[i];
int streamType = streamRes.getStreamType();
StreamControl sc = new StreamControl();
sc.streamType = streamType;
sc.group = (ViewGroup) inflater.inflate(getItemLayout(), null);
sc.group.setTag(sc);
sc.icon = (ImageView) sc.group.findViewById(R.id.stream_icon);
sc.icon.setTag(sc);
sc.icon.setContentDescription(res.getString(streamRes.getDescRes()));
sc.iconRes = streamRes.getIconRes();
sc.iconMuteRes = streamRes.getIconMuteRes();
int[] icons = _getStreamIcons(sc);
sc.icon.setImageResource(icons[0]);
sc.icon.setOnClickListener(getStreamClickListener());
sc.seekbarView = (SeekBar) sc.group.findViewById(android.R.id.progress);
int plusOne = (streamType == STREAM_BLUETOOTH_SCO ||
streamType == AudioManager.STREAM_VOICE_CALL) ? 1 : 0;
setProgressColor(sc.seekbarView, color);
sc.seekbarView.setMax(getStreamMaxVolume(streamType) + plusOne);
sc.seekbarView.setOnSeekBarChangeListener(volumeSeekListener);
sc.seekbarView.setTag(sc);
onCreateStream(sc);
mStreamControls.put(streamType, sc);
}
}
@Override
public SoundPool createSoundPool() {
return new SoundPool(1, AudioManager.STREAM_VOICE_CALL, 0);
}
@SuppressWarnings("deprecation") // Deprecated in API level 25.
private static AudioTrack createAudioTrackOnLowerThanLollipop(
int sampleRateInHz, int channelConfig, int bufferSizeInBytes) {
return new AudioTrack(AudioManager.STREAM_VOICE_CALL, sampleRateInHz, channelConfig,
AudioFormat.ENCODING_PCM_16BIT, bufferSizeInBytes, AudioTrack.MODE_STREAM);
}
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() != Sensor.TYPE_PROXIMITY)
return;
if (mediaPlayer == null || !mediaPlayer.isPlaying())
return;
int streamType;
if (event.values[0] < 5f && event.values[0] != proximitySensor.getMaximumRange()) {
streamType = AudioManager.STREAM_VOICE_CALL;
} else {
streamType = AudioManager.STREAM_MUSIC;
}
if (streamType == AudioManager.STREAM_VOICE_CALL &&
mediaPlayer.getAudioStreamType() != streamType &&
!audioManager.isWiredHeadsetOn()) {
double position = mediaPlayer.getCurrentPosition();
double duration = mediaPlayer.getDuration();
double progress = position / duration;
Logger.d("AudioSlidePlayer onSensorChanged, replay");
if (wakeLock != null)
wakeLock.acquire();
stop(true);
try {
if (forSensor) {
play(progress, true);
} else if (wakeLock != null) {
wakeLock.release();
}
} catch (IOException e) {
Log.w(TAG, e);
}
} else if (streamType == AudioManager.STREAM_MUSIC &&
mediaPlayer.getAudioStreamType() != streamType &&
System.currentTimeMillis() - startTime > 500) {
Logger.d("AudioSlidePlayer onSensorChanged, stop");
if (wakeLock != null)
wakeLock.release();
stop();
notifyOnStop();
}
}
@Override
public void onCreate() {
super.onCreate();
if (BuildVars.LOGS_ENABLED) {
FileLog.d("=============== VoIPService STARTING ===============");
}
try {
AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 && am.getProperty(AudioManager.PROPERTY_OUTPUT_FRAMES_PER_BUFFER)!=null) {
int outFramesPerBuffer = Integer.parseInt(am.getProperty(AudioManager.PROPERTY_OUTPUT_FRAMES_PER_BUFFER));
TgVoip.setBufferSize(outFramesPerBuffer);
} else {
TgVoip.setBufferSize(AudioTrack.getMinBufferSize(48000, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT) / 2);
}
cpuWakelock = ((PowerManager) getSystemService(POWER_SERVICE)).newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "telegram-voip");
cpuWakelock.acquire();
btAdapter=am.isBluetoothScoAvailableOffCall() ? BluetoothAdapter.getDefaultAdapter() : null;
IntentFilter filter = new IntentFilter();
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
if(!USE_CONNECTION_SERVICE){
filter.addAction(ACTION_HEADSET_PLUG);
if(btAdapter!=null){
filter.addAction(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED);
filter.addAction(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED);
}
filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
}
registerReceiver(receiver, filter);
soundPool = new SoundPool(1, AudioManager.STREAM_VOICE_CALL, 0);
spConnectingId = soundPool.load(this, R.raw.voip_connecting, 1);
spRingbackID = soundPool.load(this, R.raw.voip_ringback, 1);
spFailedID = soundPool.load(this, R.raw.voip_failed, 1);
spEndId = soundPool.load(this, R.raw.voip_end, 1);
spBusyId = soundPool.load(this, R.raw.voip_busy, 1);
am.registerMediaButtonEventReceiver(new ComponentName(this, VoIPMediaButtonReceiver.class));
if(!USE_CONNECTION_SERVICE && btAdapter!=null && btAdapter.isEnabled()){
int headsetState=btAdapter.getProfileConnectionState(BluetoothProfile.HEADSET);
updateBluetoothHeadsetState(headsetState==BluetoothProfile.STATE_CONNECTED);
//if(headsetState==BluetoothProfile.STATE_CONNECTED)
// am.setBluetoothScoOn(true);
for (StateListener l : stateListeners)
l.onAudioSettingsChanged();
}
} catch (Exception x) {
if (BuildVars.LOGS_ENABLED) {
FileLog.e("error initializing voip controller", x);
}
callFailed();
}
}
@Subscribe
public void onAudioRouteChangeEvent(AudioRouteChangeEvent event) {
int streamType = event.frontSpeaker ? AudioManager.STREAM_VOICE_CALL : AudioManager.USE_DEFAULT_STREAM_TYPE;
setVolumeControlStream(streamType);
}