Java Code Examples for android.media.AudioManager#requestAudioFocus()
The following examples show how to use
android.media.AudioManager#requestAudioFocus() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MediaPlayerListener.java From android-chromium with BSD 2-Clause "Simplified" License | 6 votes |
@CalledByNative private static MediaPlayerListener create(int nativeMediaPlayerListener, Context context, MediaPlayerBridge mediaPlayerBridge) { final MediaPlayerListener listener = new MediaPlayerListener(nativeMediaPlayerListener, context); mediaPlayerBridge.setOnBufferingUpdateListener(listener); mediaPlayerBridge.setOnCompletionListener(listener); mediaPlayerBridge.setOnErrorListener(listener); mediaPlayerBridge.setOnPreparedListener(listener); mediaPlayerBridge.setOnSeekCompleteListener(listener); mediaPlayerBridge.setOnVideoSizeChangedListener(listener); AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); am.requestAudioFocus( listener, AudioManager.STREAM_MUSIC, // Request permanent focus. AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK); return listener; }
Example 2
Source File: Util.java From Popeens-DSub with GNU General Public License v3.0 | 6 votes |
@TargetApi(8) public static void requestAudioFocus(final Context context, final AudioManager audioManager) { if(Build.VERSION.SDK_INT >= 26) { if(audioFocusRequest == null) { AudioAttributes playbackAttributes = new AudioAttributes.Builder() .setUsage(AudioAttributes.USAGE_MEDIA) .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC) .build(); audioFocusRequest = new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN) .setAudioAttributes(playbackAttributes) .setOnAudioFocusChangeListener(getAudioFocusChangeListener(context, audioManager)) .setWillPauseWhenDucked(true) .build(); audioManager.requestAudioFocus(audioFocusRequest); } } else if (Build.VERSION.SDK_INT >= 8 && focusListener == null) { audioManager.requestAudioFocus(focusListener = getAudioFocusChangeListener(context, audioManager), AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); } }
Example 3
Source File: AudioFocusDelegate.java From 365browser with Apache License 2.0 | 5 votes |
private boolean requestAudioFocusInternal() { AudioManager am = (AudioManager) ContextUtils.getApplicationContext().getSystemService( Context.AUDIO_SERVICE); int result = am.requestAudioFocus(this, AudioManager.STREAM_MUSIC, mFocusType); return result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED; }
Example 4
Source File: MainActivity.java From QuickLyric with GNU General Public License v3.0 | 5 votes |
@SuppressLint("InlinedApi") public static void resync(Context context) { AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); am.requestAudioFocus(null, // Use the music stream. AudioManager.STREAM_SYSTEM, // Request permanent focus. AudioManager.AUDIOFOCUS_GAIN_TRANSIENT); am.abandonAudioFocus(null); }
Example 5
Source File: BackgroundAudioService.java From CodenameOne with GNU General Public License v2.0 | 5 votes |
private boolean successfullyRetrievedAudioFocus() { AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); int result = audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); return result == AudioManager.AUDIOFOCUS_GAIN; }
Example 6
Source File: VideoConsumptionExampleWithExoPlayerFragment.java From leanback-showcase with Apache License 2.0 | 5 votes |
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ExoPlayerAdapter playerAdapter = new ExoPlayerAdapter(getActivity()); playerAdapter.setAudioStreamType(AudioManager.USE_DEFAULT_STREAM_TYPE); mMediaPlayerGlue = new VideoMediaPlayerGlue(getActivity(), playerAdapter); mMediaPlayerGlue.setHost(mHost); AudioManager audioManager = (AudioManager) getActivity() .getSystemService(Context.AUDIO_SERVICE); if (audioManager.requestAudioFocus(mOnAudioFocusChangeListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN) != AudioManager.AUDIOFOCUS_REQUEST_GRANTED) { Log.w(TAG, "video player cannot obtain audio focus!"); } mMediaPlayerGlue.setMode(PlaybackControlsRow.RepeatAction.NONE); MediaMetaData intentMetaData = getActivity().getIntent().getParcelableExtra( VideoExampleActivity.TAG); if (intentMetaData != null) { mMediaPlayerGlue.setTitle(intentMetaData.getMediaTitle()); mMediaPlayerGlue.setSubtitle(intentMetaData.getMediaArtistName()); mMediaPlayerGlue.getPlayerAdapter().setDataSource( Uri.parse(intentMetaData.getMediaSourcePath())); } else { mMediaPlayerGlue.setTitle("Diving with Sharks"); mMediaPlayerGlue.setSubtitle("A Googler"); mMediaPlayerGlue.getPlayerAdapter().setDataSource(Uri.parse(URL)); } PlaybackSeekDiskDataProvider.setDemoSeekProvider(mMediaPlayerGlue); playWhenReady(mMediaPlayerGlue); setBackgroundType(BG_LIGHT); }
Example 7
Source File: WebPlayerView.java From Telegram with GNU General Public License v2.0 | 5 votes |
private void checkAudioFocus() { if (!hasAudioFocus) { AudioManager audioManager = (AudioManager) ApplicationLoader.applicationContext.getSystemService(Context.AUDIO_SERVICE); hasAudioFocus = true; if (audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN) == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) { audioFocus = 2; } } }
Example 8
Source File: SignalAudioManager.java From bcm-android with GNU General Public License v3.0 | 5 votes |
public void initializeAudioForCall() { AudioManager audioManager = AppUtil.INSTANCE.getAudioManager(context); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { audioManager.requestAudioFocus(null, AudioManager.STREAM_VOICE_CALL, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE); } else { audioManager.requestAudioFocus(null, AudioManager.STREAM_VOICE_CALL, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT); } }
Example 9
Source File: Audio.java From CodenameOne with GNU General Public License v2.0 | 5 votes |
public Audio(Activity activity, MediaPlayer player, InputStream stream, Runnable onComplete) { this.activity = activity; this.player = player; this.stream = stream; if (onComplete != null) { addCompletionHandler(onComplete); } bindPlayerCleanupOnComplete(); AudioManager audioManager = (AudioManager) activity.getSystemService(Context.AUDIO_SERVICE); int result = audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); }
Example 10
Source File: MediaViewerActivity.java From Pix-Art-Messenger with GNU General Public License v3.0 | 5 votes |
private void requestAudioFocus() { AudioManager am = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE); if (am != null) { am.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT); } }
Example 11
Source File: VideoConsumptionExampleWithExoPlayerFragment.java From tv-samples with Apache License 2.0 | 5 votes |
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ExoPlayerAdapter playerAdapter = new ExoPlayerAdapter(getActivity()); playerAdapter.setAudioStreamType(AudioManager.USE_DEFAULT_STREAM_TYPE); mMediaPlayerGlue = new VideoMediaPlayerGlue(getActivity(), playerAdapter); mMediaPlayerGlue.setHost(mHost); AudioManager audioManager = (AudioManager) getActivity() .getSystemService(Context.AUDIO_SERVICE); if (audioManager.requestAudioFocus(mOnAudioFocusChangeListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN) != AudioManager.AUDIOFOCUS_REQUEST_GRANTED) { Log.w(TAG, "video player cannot obtain audio focus!"); } mMediaPlayerGlue.setMode(PlaybackControlsRow.RepeatAction.NONE); MediaMetaData intentMetaData = getActivity().getIntent().getParcelableExtra( VideoExampleActivity.TAG); if (intentMetaData != null) { mMediaPlayerGlue.setTitle(intentMetaData.getMediaTitle()); mMediaPlayerGlue.setSubtitle(intentMetaData.getMediaArtistName()); mMediaPlayerGlue.getPlayerAdapter().setDataSource( Uri.parse(intentMetaData.getMediaSourcePath())); } else { mMediaPlayerGlue.setTitle("Diving with Sharks"); mMediaPlayerGlue.setSubtitle("A Googler"); mMediaPlayerGlue.getPlayerAdapter().setDataSource(Uri.parse(URL)); } PlaybackSeekDiskDataProvider.setDemoSeekProvider(mMediaPlayerGlue); playWhenReady(mMediaPlayerGlue); setBackgroundType(BG_LIGHT); }
Example 12
Source File: VideoConsumptionExampleFragment.java From tv-samples with Apache License 2.0 | 5 votes |
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mMediaPlayerGlue = new VideoMediaPlayerGlue(getActivity(), new MediaPlayerAdapter(getActivity())); mMediaPlayerGlue.setHost(mHost); AudioManager audioManager = (AudioManager) getActivity() .getSystemService(Context.AUDIO_SERVICE); if (audioManager.requestAudioFocus(mOnAudioFocusChangeListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN) != AudioManager.AUDIOFOCUS_REQUEST_GRANTED) { Log.w(TAG, "video player cannot obtain audio focus!"); } mMediaPlayerGlue.setMode(PlaybackControlsRow.RepeatAction.NONE); MediaMetaData intentMetaData = getActivity().getIntent().getParcelableExtra( VideoExampleActivity.TAG); if (intentMetaData != null) { mMediaPlayerGlue.setTitle(intentMetaData.getMediaTitle()); mMediaPlayerGlue.setSubtitle(intentMetaData.getMediaArtistName()); mMediaPlayerGlue.getPlayerAdapter().setDataSource( Uri.parse(intentMetaData.getMediaSourcePath())); } else { mMediaPlayerGlue.setTitle("Diving with Sharks"); mMediaPlayerGlue.setSubtitle("A Googler"); mMediaPlayerGlue.getPlayerAdapter().setDataSource(Uri.parse(URL)); } PlaybackSeekDiskDataProvider.setDemoSeekProvider(mMediaPlayerGlue); playWhenReady(mMediaPlayerGlue); setBackgroundType(BG_LIGHT); }
Example 13
Source File: WearService.java From LibreAlarm with GNU General Public License v3.0 | 4 votes |
private void runTextToSpeech(PredictionData data) { if (!PreferencesUtil.getBoolean(this, getString(R.string.pref_key_text_to_speech))) return; boolean alarmOnly = PreferencesUtil.getBoolean(this, getString(R.string.pref_key_text_to_speech_only_alarm)); if (alarmOnly && AlertRules.checkDontPostpone(this, data) == AlertRules.Danger.NOTHING) return; AudioManager manager = (AudioManager) getSystemService(AUDIO_SERVICE); manager.requestAudioFocus(mAudioFocusChangeListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK); String message; if (data.errorCode == PredictionData.Result.OK) { boolean isMmol = PreferencesUtil.getBoolean(this, getString(R.string.pref_key_mmol), true); String glucose = data.glucose(isMmol); AlgorithmUtil.TrendArrow arrow = AlgorithmUtil.getTrendArrow(data); String trend; switch (arrow) { case UP: trend = getString(R.string.text_to_speech_trend_up); break; case DOWN: trend = getString(R.string.text_to_speech_trend_down); break; case SLIGHTLY_UP: trend = getString(R.string.text_to_speech_trend_slightly_up); break; case SLIGHTLY_DOWN: trend = getString(R.string.text_to_speech_trend_slightly_down); break; case FLAT: trend = getString(R.string.text_to_speech_trend_flat); break; case UNKNOWN: default: trend = getString(R.string.text_to_speech_trend_unknown); break; } message = getString(R.string.text_to_speech_message, glucose, trend); } else { message = getString(R.string.text_to_speech_error); } if (Build.VERSION.SDK_INT < 21) { HashMap<String, String> map = new HashMap<>(); map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "glucose-speech"); mTextToSpeech.speak(message, TextToSpeech.QUEUE_FLUSH, map); } else { mTextToSpeech.speak(message, TextToSpeech.QUEUE_FLUSH, null, "glucose-speech"); } }
Example 14
Source File: VoIPBaseService.java From TelePlus-Android with GNU General Public License v2.0 | 4 votes |
protected void configureDeviceForCall() { needPlayEndSound = true; AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE); if(!USE_CONNECTION_SERVICE){ am.setMode(AudioManager.MODE_IN_COMMUNICATION); am.requestAudioFocus(this, AudioManager.STREAM_VOICE_CALL, AudioManager.AUDIOFOCUS_GAIN); if(isBluetoothHeadsetConnected() && hasEarpiece()){ switch(audioRouteToSet){ case AUDIO_ROUTE_BLUETOOTH: if(!bluetoothScoActive){ needSwitchToBluetoothAfterScoActivates=true; try { am.startBluetoothSco(); } catch (Throwable ignore) { } }else{ am.setBluetoothScoOn(true); am.setSpeakerphoneOn(false); } break; case AUDIO_ROUTE_EARPIECE: am.setBluetoothScoOn(false); am.setSpeakerphoneOn(false); break; case AUDIO_ROUTE_SPEAKER: am.setBluetoothScoOn(false); am.setSpeakerphoneOn(true); break; } }else if(isBluetoothHeadsetConnected()){ am.setBluetoothScoOn(speakerphoneStateToSet); }else{ am.setSpeakerphoneOn(speakerphoneStateToSet); } }/*else{ if(isBluetoothHeadsetConnected() && hasEarpiece()){ switch(audioRouteToSet){ case AUDIO_ROUTE_BLUETOOTH: systemCallConnection.setAudioRoute(CallAudioState.ROUTE_BLUETOOTH); break; case AUDIO_ROUTE_EARPIECE: systemCallConnection.setAudioRoute(CallAudioState.ROUTE_WIRED_OR_EARPIECE); break; case AUDIO_ROUTE_SPEAKER: systemCallConnection.setAudioRoute(CallAudioState.ROUTE_SPEAKER); break; } }else{ if(hasEarpiece()) systemCallConnection.setAudioRoute(!speakerphoneStateToSet ? CallAudioState.ROUTE_WIRED_OR_EARPIECE : CallAudioState.ROUTE_SPEAKER); else systemCallConnection.setAudioRoute(!speakerphoneStateToSet ? CallAudioState.ROUTE_WIRED_OR_EARPIECE : CallAudioState.ROUTE_BLUETOOTH); } }*/ updateOutputGainControlState(); audioConfigured=true; SensorManager sm = (SensorManager) getSystemService(SENSOR_SERVICE); Sensor proximity = sm.getDefaultSensor(Sensor.TYPE_PROXIMITY); try{ if(proximity!=null){ proximityWakelock=((PowerManager) getSystemService(Context.POWER_SERVICE)).newWakeLock(PROXIMITY_SCREEN_OFF_WAKE_LOCK, "telegram-voip-prx"); sm.registerListener(this, proximity, SensorManager.SENSOR_DELAY_NORMAL); } }catch(Exception x){ if (BuildVars.LOGS_ENABLED) { FileLog.e("Error initializing proximity sensor", x); } } }
Example 15
Source File: VoIPBaseService.java From Telegram with GNU General Public License v2.0 | 4 votes |
protected void configureDeviceForCall() { needPlayEndSound = true; AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE); if(!USE_CONNECTION_SERVICE){ am.setMode(AudioManager.MODE_IN_COMMUNICATION); am.requestAudioFocus(this, AudioManager.STREAM_VOICE_CALL, AudioManager.AUDIOFOCUS_GAIN); if(isBluetoothHeadsetConnected() && hasEarpiece()){ switch(audioRouteToSet){ case AUDIO_ROUTE_BLUETOOTH: if(!bluetoothScoActive){ needSwitchToBluetoothAfterScoActivates=true; try { am.startBluetoothSco(); } catch (Throwable ignore) { } }else{ am.setBluetoothScoOn(true); am.setSpeakerphoneOn(false); } break; case AUDIO_ROUTE_EARPIECE: am.setBluetoothScoOn(false); am.setSpeakerphoneOn(false); break; case AUDIO_ROUTE_SPEAKER: am.setBluetoothScoOn(false); am.setSpeakerphoneOn(true); break; } }else if(isBluetoothHeadsetConnected()){ am.setBluetoothScoOn(speakerphoneStateToSet); }else{ am.setSpeakerphoneOn(speakerphoneStateToSet); } }/*else{ if(isBluetoothHeadsetConnected() && hasEarpiece()){ switch(audioRouteToSet){ case AUDIO_ROUTE_BLUETOOTH: systemCallConnection.setAudioRoute(CallAudioState.ROUTE_BLUETOOTH); break; case AUDIO_ROUTE_EARPIECE: systemCallConnection.setAudioRoute(CallAudioState.ROUTE_WIRED_OR_EARPIECE); break; case AUDIO_ROUTE_SPEAKER: systemCallConnection.setAudioRoute(CallAudioState.ROUTE_SPEAKER); break; } }else{ if(hasEarpiece()) systemCallConnection.setAudioRoute(!speakerphoneStateToSet ? CallAudioState.ROUTE_WIRED_OR_EARPIECE : CallAudioState.ROUTE_SPEAKER); else systemCallConnection.setAudioRoute(!speakerphoneStateToSet ? CallAudioState.ROUTE_WIRED_OR_EARPIECE : CallAudioState.ROUTE_BLUETOOTH); } }*/ updateOutputGainControlState(); audioConfigured=true; SensorManager sm = (SensorManager) getSystemService(SENSOR_SERVICE); Sensor proximity = sm.getDefaultSensor(Sensor.TYPE_PROXIMITY); try{ if(proximity!=null){ proximityWakelock=((PowerManager) getSystemService(Context.POWER_SERVICE)).newWakeLock(PROXIMITY_SCREEN_OFF_WAKE_LOCK, "telegram-voip-prx"); sm.registerListener(this, proximity, SensorManager.SENSOR_DELAY_NORMAL); } }catch(Exception x){ if (BuildVars.LOGS_ENABLED) { FileLog.e("Error initializing proximity sensor", x); } } }
Example 16
Source File: IjkVideoView.java From DanDanPlayForAndroid with MIT License | 4 votes |
@TargetApi(Build.VERSION_CODES.M) private void openVideo() { if (mUri == null || mSurfaceHolder == null) { // not ready for playback just yet, will try again later return; } // we shouldn't clear the target state, because somebody might have // called start() previously release(false); // 声音控制 AudioManager am = (AudioManager) mAppContext.getSystemService(Context.AUDIO_SERVICE); am.requestAudioFocus(null, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); try { mMediaPlayer = createPlayer(); // REMOVED: mAudioSession mMediaPlayer.setOnPreparedListener(mPreparedListener); mMediaPlayer.setOnVideoSizeChangedListener(mSizeChangedListener); mMediaPlayer.setOnCompletionListener(mCompletionListener); mMediaPlayer.setOnErrorListener(mErrorListener); mMediaPlayer.setOnInfoListener(mInfoListener); mMediaPlayer.setOnBufferingUpdateListener(mBufferingUpdateListener); mMediaPlayer.setOnSeekCompleteListener(mSeekCompleteListener); mMediaPlayer.setOnTimedTextListener(mTimedTextListener); mCurrentBufferPercentage = 0; String scheme = mUri.getScheme(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && mIsUsingMediaDataSource && (TextUtils.isEmpty(scheme) || "file".equalsIgnoreCase(scheme))) { IMediaDataSource dataSource = new FileMediaDataSource(new File(mUri.toString())); mMediaPlayer.setDataSource(dataSource); } else { mMediaPlayer.setDataSource(mAppContext, mUri, mHeaders); } bindSurfaceHolder(mMediaPlayer, mSurfaceHolder); mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mMediaPlayer.setScreenOnWhilePlaying(true); mPrepareStartTime = System.currentTimeMillis(); mMediaPlayer.prepareAsync(); // REMOVED: mPendingSubtitleTracks // we don't set the target state here either, but preserve the // target state that was there before. mCurrentState = MediaPlayerParams.STATE_PREPARING; attachMediaController(); } catch (IOException | IllegalArgumentException ex) { Log.w(TAG, "Unable to open content: " + mUri, ex); mCurrentState = MediaPlayerParams.STATE_ERROR; mTargetState = MediaPlayerParams.STATE_ERROR; mErrorListener.onError(mMediaPlayer, MediaPlayer.MEDIA_ERROR_UNKNOWN, 0); } finally { // REMOVED: mPendingSubtitleTracks.clear(); _notifyMediaStatus(); } }
Example 17
Source File: VideoPlayerView.java From TigerVideo with Apache License 2.0 | 4 votes |
/** * 请求获取AudioFocus */ private void requestAudioFocus() { AudioManager audioManager = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE); audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT); }
Example 18
Source File: PlayerService.java From IdealMedia with Apache License 2.0 | 4 votes |
@Override public void onCreate() { super.onCreate(); // initialize default output device if (!BASS.BASS_Init(-1, 44100, 0)) { return; } // look for plugins plugins = ""; String path = getApplicationInfo().nativeLibraryDir; String[] list = new File(path).list(); for (String s: list) { int plug = BASS.BASS_PluginLoad(path+"/"+s, 0); if (plug != 0) { // plugin loaded... plugins += s + "\n"; // add it to the list } } if (plugins.equals("")) plugins = "no plugins - visit the BASS webpage to get some\n"; if(playerInterface != null) { playerInterface.onPluginsLoaded(plugins); } BASS.BASS_SetConfig(BASS.BASS_CONFIG_BUFFER, 1000); Log.w("BASS.BASS_CONFIG_BUFFER", "" + BASS.BASS_GetConfig(BASS.BASS_CONFIG_BUFFER)); // Pending Intend Intent intent = new Intent(this, NavigationActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); pendIntent = PendingIntent.getActivity(this, 0, intent, 0); //tracklist loadTracks(); loadEqualizerValues(); tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); tm.listen(telephone, PhoneStateListener.LISTEN_CALL_STATE); myBroadcastReceiver = new ServiceBroadcastReceiver(); IntentFilter intentFilter = new IntentFilter(Intent.ACTION_HEADSET_PLUG); intentFilter.addAction("android.media.VOLUME_CHANGED_ACTION"); intentFilter.addAction(Intent.ACTION_POWER_DISCONNECTED); registerReceiver(myBroadcastReceiver, intentFilter); ComponentName rcvMedia = new ComponentName(getPackageName(), MediaControlReceiver.class.getName()); mAudioManager = (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE); mAudioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); mAudioManager.registerMediaButtonEventReceiver(rcvMedia); // Use the remote control APIs (if available) to set the playback state if (Build.VERSION.SDK_INT >= 14 && remoteControlClient == null) { registerRemoteControl(rcvMedia); } }
Example 19
Source File: VoIPBaseService.java From TelePlus-Android with GNU General Public License v2.0 | 4 votes |
protected void configureDeviceForCall() { needPlayEndSound = true; AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE); if(!USE_CONNECTION_SERVICE){ am.setMode(AudioManager.MODE_IN_COMMUNICATION); am.requestAudioFocus(this, AudioManager.STREAM_VOICE_CALL, AudioManager.AUDIOFOCUS_GAIN); if(isBluetoothHeadsetConnected() && hasEarpiece()){ switch(audioRouteToSet){ case AUDIO_ROUTE_BLUETOOTH: if(!bluetoothScoActive){ needSwitchToBluetoothAfterScoActivates=true; try { am.startBluetoothSco(); } catch (Throwable ignore) { } }else{ am.setBluetoothScoOn(true); am.setSpeakerphoneOn(false); } break; case AUDIO_ROUTE_EARPIECE: am.setBluetoothScoOn(false); am.setSpeakerphoneOn(false); break; case AUDIO_ROUTE_SPEAKER: am.setBluetoothScoOn(false); am.setSpeakerphoneOn(true); break; } }else if(isBluetoothHeadsetConnected()){ am.setBluetoothScoOn(speakerphoneStateToSet); }else{ am.setSpeakerphoneOn(speakerphoneStateToSet); } }/*else{ if(isBluetoothHeadsetConnected() && hasEarpiece()){ switch(audioRouteToSet){ case AUDIO_ROUTE_BLUETOOTH: systemCallConnection.setAudioRoute(CallAudioState.ROUTE_BLUETOOTH); break; case AUDIO_ROUTE_EARPIECE: systemCallConnection.setAudioRoute(CallAudioState.ROUTE_WIRED_OR_EARPIECE); break; case AUDIO_ROUTE_SPEAKER: systemCallConnection.setAudioRoute(CallAudioState.ROUTE_SPEAKER); break; } }else{ if(hasEarpiece()) systemCallConnection.setAudioRoute(!speakerphoneStateToSet ? CallAudioState.ROUTE_WIRED_OR_EARPIECE : CallAudioState.ROUTE_SPEAKER); else systemCallConnection.setAudioRoute(!speakerphoneStateToSet ? CallAudioState.ROUTE_WIRED_OR_EARPIECE : CallAudioState.ROUTE_BLUETOOTH); } }*/ updateOutputGainControlState(); audioConfigured=true; SensorManager sm = (SensorManager) getSystemService(SENSOR_SERVICE); Sensor proximity = sm.getDefaultSensor(Sensor.TYPE_PROXIMITY); try{ if(proximity!=null){ proximityWakelock=((PowerManager) getSystemService(Context.POWER_SERVICE)).newWakeLock(PROXIMITY_SCREEN_OFF_WAKE_LOCK, "telegram-voip-prx"); sm.registerListener(this, proximity, SensorManager.SENSOR_DELAY_NORMAL); } }catch(Exception x){ if (BuildVars.LOGS_ENABLED) { FileLog.e("Error initializing proximity sensor", x); } } }
Example 20
Source File: MediaManager.java From DMusic with Apache License 2.0 | 4 votes |
public IMediaPlayer prepare(Context context, final Uri uri, final Map<String, String> heads, boolean looping) { if (context == null || uri == null) { return null; } currentState = STATE_PREPARING; currentBufferPercentage = 0; seekWhenPrepared = 0; // We shouldn't clear the target state, because somebody might have // called start() previously release(context, false); AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); // AudioManager.AUDIOFOCUS_GAIN / AudioManager.AUDIOFOCUS_GAIN_TRANSIENT am.requestAudioFocus(null, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); try { mediaPlayer = Factory.createPlayer(context, settings.getPlayer()); if (mediaPlayer == null) { return null; } mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); String scheme = uri.getScheme(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && settings.getUsingMediaDataSource() && (TextUtils.isEmpty(scheme) || scheme.equalsIgnoreCase("file"))) { IMediaDataSource dataSource = new FileMediaDataSource(new File(uri.toString())); mediaPlayer.setDataSource(dataSource); } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { mediaPlayer.setDataSource(context, uri, heads); } else { mediaPlayer.setDataSource(uri.toString()); } mediaPlayer.setLooping(looping); mediaPlayer.setOnPreparedListener(this); mediaPlayer.setOnCompletionListener(this); mediaPlayer.setOnBufferingUpdateListener(this); mediaPlayer.setScreenOnWhilePlaying(true); mediaPlayer.setOnSeekCompleteListener(this); mediaPlayer.setOnErrorListener(this); mediaPlayer.setOnInfoListener(this); mediaPlayer.setOnVideoSizeChangedListener(this); mediaPlayer.prepareAsync(); return mediaPlayer; } catch (Exception e) { ULog.w("Unable to open content: " + uri + e); onError(mediaPlayer, MediaPlayer.MEDIA_ERROR_UNKNOWN, 0); e.printStackTrace(); return null; } }