Example usage for android.media AudioManager AUDIOFOCUS_GAIN

List of usage examples for android.media AudioManager AUDIOFOCUS_GAIN

Introduction

In this page you can find the example usage for android.media AudioManager AUDIOFOCUS_GAIN.

Prototype

int AUDIOFOCUS_GAIN

To view the source code for android.media AudioManager AUDIOFOCUS_GAIN.

Click Source Link

Document

Used to indicate a gain of audio focus, or a request of audio focus, of unknown duration.

Usage

From source file:com.google.android.car.kitchensink.audio.AudioTestFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
    Log.i(TAG, "onCreateView");
    init();/*from   w w w.j a v  a  2s  .co m*/
    View view = inflater.inflate(R.layout.audio, container, false);
    mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
    mAudioFocusHandler = new FocusHandler((RadioGroup) view.findViewById(R.id.button_focus_request_selection),
            (Button) view.findViewById(R.id.button_audio_focus_request),
            (TextView) view.findViewById(R.id.text_audio_focus_state));
    mMediaPlay = (Button) view.findViewById(R.id.button_media_play_start);
    mMediaPlay.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            boolean requestFocus = true;
            boolean repeat = true;
            mMusicPlayer.start(requestFocus, repeat, AudioManager.AUDIOFOCUS_GAIN);
        }
    });
    mMediaPlayOnce = (Button) view.findViewById(R.id.button_media_play_once);
    mMediaPlayOnce.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            mMusicPlayerShort.start(true, false, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
            // play only for 1 sec and stop
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    mMusicPlayerShort.stop();
                }
            }, 1000);
        }
    });
    mMediaStop = (Button) view.findViewById(R.id.button_media_play_stop);
    mMediaStop.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            mMusicPlayer.stop();
        }
    });
    mWavPlay = (Button) view.findViewById(R.id.button_wav_play_start);
    mWavPlay.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            mWavPlayer.start(true, true, AudioManager.AUDIOFOCUS_GAIN);
        }
    });
    mWavStop = (Button) view.findViewById(R.id.button_wav_play_stop);
    mWavStop.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            mWavPlayer.stop();
        }
    });
    mNavPlayOnce = (Button) view.findViewById(R.id.button_nav_play_once);
    mNavPlayOnce.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mAppFocusManager == null) {
                return;
            }
            if (DBG) {
                Log.i(TAG, "Nav start");
            }
            if (!mNavGuidancePlayer.isPlaying()) {
                try {
                    mAppFocusManager.requestAppFocus(CarAppFocusManager.APP_FOCUS_TYPE_NAVIGATION,
                            mOwnershipCallbacks);
                } catch (CarNotConnectedException e) {
                    Log.e(TAG, "Failed to set active focus", e);
                }
                mNavGuidancePlayer.start(true, false, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK,
                        new PlayStateListener() {
                            @Override
                            public void onCompletion() {
                                mAppFocusManager.abandonAppFocus(mOwnershipCallbacks,
                                        CarAppFocusManager.APP_FOCUS_TYPE_NAVIGATION);
                            }
                        });
            }
        }
    });
    mVrPlayOnce = (Button) view.findViewById(R.id.button_vr_play_once);
    mVrPlayOnce.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mAppFocusManager == null) {
                return;
            }
            if (DBG) {
                Log.i(TAG, "VR start");
            }
            try {
                mAppFocusManager.requestAppFocus(CarAppFocusManager.APP_FOCUS_TYPE_VOICE_COMMAND,
                        mOwnershipCallbacks);
            } catch (CarNotConnectedException e) {
                Log.e(TAG, "Failed to set active focus", e);
            }
            if (!mVrPlayer.isPlaying()) {
                mVrPlayer.start(true, false, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT, new PlayStateListener() {
                    @Override
                    public void onCompletion() {
                        mAppFocusManager.abandonAppFocus(mOwnershipCallbacks,
                                CarAppFocusManager.APP_FOCUS_TYPE_VOICE_COMMAND);
                    }
                });
            }
        }
    });
    mSystemPlayOnce = (Button) view.findViewById(R.id.button_system_play_once);
    mSystemPlayOnce.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (DBG) {
                Log.i(TAG, "System start");
            }
            if (!mSystemPlayer.isPlaying()) {
                // system sound played without focus
                mSystemPlayer.start(false, false, 0);
            }
        }
    });
    mNavStart = (Button) view.findViewById(R.id.button_nav_start);
    mNavStart.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            handleNavStart();
        }
    });
    mNavEnd = (Button) view.findViewById(R.id.button_nav_end);
    mNavEnd.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            handleNavEnd();
        }
    });
    mVrStart = (Button) view.findViewById(R.id.button_vr_start);
    mVrStart.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            handleVrStart();
        }
    });
    mVrEnd = (Button) view.findViewById(R.id.button_vr_end);
    mVrEnd.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            handleVrEnd();
        }
    });
    mRadioStart = (Button) view.findViewById(R.id.button_radio_start);
    mRadioStart.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            handleRadioStart();
        }
    });
    mRadioEnd = (Button) view.findViewById(R.id.button_radio_end);
    mRadioEnd.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            handleRadioEnd();
        }
    });
    mSpeakerPhoneOn = (Button) view.findViewById(R.id.button_speaker_phone_on);
    mSpeakerPhoneOn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            mAudioManager.setSpeakerphoneOn(true);
        }
    });
    mSpeakerPhoneOff = (Button) view.findViewById(R.id.button_speaker_phone_off);
    mSpeakerPhoneOff.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            mAudioManager.setSpeakerphoneOn(false);
        }
    });
    mMicrophoneOn = (Button) view.findViewById(R.id.button_microphone_on);
    mMicrophoneOn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            mAudioManager.setMicrophoneMute(false); // Turn the microphone on.
        }
    });
    mMicrophoneOff = (Button) view.findViewById(R.id.button_microphone_off);
    mMicrophoneOff.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            mAudioManager.setMicrophoneMute(true); // Mute the microphone.
        }
    });

    mRejectFocus = (ToggleButton) view.findViewById(R.id.button_reject_audio_focus);
    mRejectFocus.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (mCarEmulator == null) {
                return;
            }
            if (!mEnableMocking.isChecked()) {
                return;
            }
            if (isChecked) {
                mCarEmulator.setAudioFocusControl(true);
            } else {
                mCarEmulator.setAudioFocusControl(false);
            }
        }
    });
    mRejectFocus.setActivated(false);
    mEnableMocking = (ToggleButton) view.findViewById(R.id.button_mock_audio);
    mEnableMocking.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (mCarEmulator == null) {
                //TODO(pavelm): need to do a full switch between emulated and normal mode
                // all Car*Manager references should be invalidated.
                Toast.makeText(AudioTestFragment.this.getContext(), "Not supported yet :(", Toast.LENGTH_SHORT)
                        .show();
                return;
            }
            if (isChecked) {
                mRejectFocus.setActivated(true);
                mCarEmulator.start();
            } else {
                mRejectFocus.setActivated(false);
                mCarEmulator.stop();
                mCarEmulator = null;
            }
        }
    });
    mMuteMedia = (ToggleButton) view.findViewById(R.id.button_mute_media);
    mMuteMedia.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            try {
                if (isChecked) {
                    mCarAudioManager.setMediaMute(true);
                } else {
                    mCarAudioManager.setMediaMute(false);
                }
            } catch (CarNotConnectedException e) {
                //ignore
            }
        }
    });
    return view;
}

From source file:com.example.android.uamp.playback.LocalPlayback.java

/**
 * Try to get the system audio focus./*from w  w w. j  a v  a 2 s.c  o m*/
 */
private void tryToGetAudioFocus() {
    LogHelper.d(TAG, "tryToGetAudioFocus");
    int result = mAudioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
    if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
        mAudioFocus = AUDIO_FOCUSED;
    } else {
        mAudioFocus = AUDIO_NO_FOCUS_NO_DUCK;
    }
}

From source file:com.mylovemhz.simplay.MusicService.java

@Override
public void onAudioFocusChange(int focusChange) {
    switch (focusChange) {
    case AudioManager.AUDIOFOCUS_LOSS:
        stop();//from   w  w w  .  j av  a 2s . c om
        break;
    case AudioManager.AUDIOFOCUS_REQUEST_FAILED:
        stopSelf();
        break;
    case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
        pause();
        break;
    case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
        lowerVolume();
        break;
    case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT:
    case AudioManager.AUDIOFOCUS_GAIN:
        restoreVolume();
        break;
    }
}

From source file:com.classiqo.nativeandroid_32bitz.playback.LocalPlayback.java

@Override
public void onAudioFocusChange(int focusChange) {
    LogHelper.d(TAG, "onAudioFocusChange. focusChange = ", focusChange);

    if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
        mAudioFocus = AUDIO_FOCUSED;/*from   ww  w.ja va  2s  . c  o  m*/
    } else if (focusChange == AudioManager.AUDIOFOCUS_LOSS
            || focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT
            || focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) {
        boolean canDuck = focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK;
        mAudioFocus = canDuck ? AUDIO_NO_FOCUS_CAN_DUCK : AUDIO_NO_FOCUS_NO_DUCK;

        if (mState == PlaybackState.STATE_PLAYING && !canDuck) {
            mPlayOnFocusGain = true;
        }
    } else {
        LogHelper.e(TAG, "onAudioFocusChange: Ignoring unsupported focusChange: ", focusChange);
    }
    configMediaPlayerState();
}

From source file:com.murati.oszk.audiobook.playback.LocalPlayback.java

private void tryToGetAudioFocus() {
    LogHelper.d(TAG, "tryToGetAudioFocus");
    int result = mAudioManager.requestAudioFocus(mOnAudioFocusChangeListener, AudioManager.STREAM_MUSIC,
            AudioManager.AUDIOFOCUS_GAIN);
    if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
        mCurrentAudioFocusState = AUDIO_FOCUSED;
    } else {/*from  w  w w. j  a v  a  2  s .  c o m*/
        mCurrentAudioFocusState = AUDIO_NO_FOCUS_NO_DUCK;
    }
}

From source file:net.simno.klingar.playback.LocalPlayback.java

private void tryToGetAudioFocus() {
    Timber.d("tryToGetAudioFocus");
    int result = audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
    if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
        audioFocus = AUDIO_FOCUSED;/*  w w  w.j  a  v a2 s. c  om*/
    } else {
        audioFocus = AUDIO_NO_FOCUS_NO_DUCK;
    }
}

From source file:com.dmplayer.manager.MusicPlayerService.java

@SuppressLint("NewApi")
private void createNotification(SongDetail mSongDetail) {
    try {//from  www  .  j  ava  2s.co  m
        String songName = mSongDetail.getTitle();
        String authorName = mSongDetail.getArtist();
        SongDetail audioInfo = MediaController.getInstance().getPlayingSongDetail();

        RemoteViews simpleContentView = new RemoteViews(getApplicationContext().getPackageName(),
                R.layout.player_small_notification);
        RemoteViews expandedView = null;
        if (supportBigNotifications) {
            expandedView = new RemoteViews(getApplicationContext().getPackageName(),
                    R.layout.player_big_notification);
        }

        Intent intent = new Intent(ApplicationDMPlayer.applicationContext, DMPlayerBaseActivity.class);
        intent.setAction("openplayer");
        intent.setFlags(32768);
        PendingIntent contentIntent = PendingIntent.getActivity(ApplicationDMPlayer.applicationContext, 0,
                intent, 0);

        Notification notification = new NotificationCompat.Builder(getApplicationContext())
                .setSmallIcon(R.drawable.player).setContentIntent(contentIntent).setContentTitle(songName)
                .build();

        notification.contentView = simpleContentView;
        if (supportBigNotifications) {
            notification.bigContentView = expandedView;
        }

        setListeners(simpleContentView);
        if (supportBigNotifications) {
            setListeners(expandedView);
        }

        Bitmap albumArt = audioInfo != null ? audioInfo.getSmallCover(ApplicationDMPlayer.applicationContext)
                : null;

        if (albumArt != null) {
            notification.contentView.setImageViewBitmap(R.id.player_album_art, albumArt);
            if (supportBigNotifications) {
                notification.bigContentView.setImageViewBitmap(R.id.player_album_art, albumArt);
            }
        } else {
            notification.contentView.setImageViewResource(R.id.player_album_art,
                    R.drawable.bg_default_album_art);
            if (supportBigNotifications) {
                notification.bigContentView.setImageViewResource(R.id.player_album_art,
                        R.drawable.bg_default_album_art);
            }
        }
        notification.contentView.setViewVisibility(R.id.player_progress_bar, View.GONE);
        notification.contentView.setViewVisibility(R.id.player_next, View.VISIBLE);
        notification.contentView.setViewVisibility(R.id.player_previous, View.VISIBLE);
        if (supportBigNotifications) {
            notification.bigContentView.setViewVisibility(R.id.player_next, View.VISIBLE);
            notification.bigContentView.setViewVisibility(R.id.player_previous, View.VISIBLE);
            notification.bigContentView.setViewVisibility(R.id.player_progress_bar, View.GONE);
        }

        if (MediaController.getInstance().isAudioPaused()) {
            notification.contentView.setViewVisibility(R.id.player_pause, View.GONE);
            notification.contentView.setViewVisibility(R.id.player_play, View.VISIBLE);
            if (supportBigNotifications) {
                notification.bigContentView.setViewVisibility(R.id.player_pause, View.GONE);
                notification.bigContentView.setViewVisibility(R.id.player_play, View.VISIBLE);
            }
        } else {
            notification.contentView.setViewVisibility(R.id.player_pause, View.VISIBLE);
            notification.contentView.setViewVisibility(R.id.player_play, View.GONE);
            if (supportBigNotifications) {
                notification.bigContentView.setViewVisibility(R.id.player_pause, View.VISIBLE);
                notification.bigContentView.setViewVisibility(R.id.player_play, View.GONE);
            }
        }

        notification.contentView.setTextViewText(R.id.player_song_name, songName);
        notification.contentView.setTextViewText(R.id.player_author_name, authorName);
        if (supportBigNotifications) {
            notification.bigContentView.setTextViewText(R.id.player_song_name, songName);
            notification.bigContentView.setTextViewText(R.id.player_author_name, authorName);
        }
        notification.flags |= Notification.FLAG_ONGOING_EVENT;
        startForeground(5, notification);

        if (remoteControlClient != null) {
            RemoteControlClient.MetadataEditor metadataEditor = remoteControlClient.editMetadata(true);
            metadataEditor.putString(MediaMetadataRetriever.METADATA_KEY_ARTIST, authorName);
            metadataEditor.putString(MediaMetadataRetriever.METADATA_KEY_TITLE, songName);
            if (audioInfo != null && audioInfo.getCover(ApplicationDMPlayer.applicationContext) != null) {
                metadataEditor.putBitmap(RemoteControlClient.MetadataEditor.BITMAP_KEY_ARTWORK,
                        audioInfo.getCover(ApplicationDMPlayer.applicationContext));
            }
            metadataEditor.apply();
            audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.mantraideas.androidaudio.manager.MusicPlayerService.java

@SuppressLint("NewApi")
private void createNotification(SongDetail mSongDetail) {
    try {//from  ww  w  .ja v  a2  s.co m
        String songName = mSongDetail.getTitle();
        String authorName = mSongDetail.getArtist();
        SongDetail audioInfo = MediaController.getInstance().getPlayingSongDetail();

        RemoteViews simpleContentView = new RemoteViews(getApplicationContext().getPackageName(),
                R.layout.player_small_notification);
        RemoteViews expandedView = null;
        if (supportBigNotifications) {
            expandedView = new RemoteViews(getApplicationContext().getPackageName(),
                    R.layout.player_big_notification);
        }

        Intent intent = new Intent(MyApplication.applicationContext, MainActivity.class);
        intent.setAction("openplayer");
        intent.setFlags(32768);
        PendingIntent contentIntent = PendingIntent.getActivity(MyApplication.applicationContext, 0, intent, 0);

        Notification notification = new NotificationCompat.Builder(getApplicationContext())
                .setSmallIcon(R.drawable.player).setContentIntent(contentIntent).setContentTitle(songName)
                .build();

        notification.contentView = simpleContentView;
        if (supportBigNotifications) {
            notification.bigContentView = expandedView;
        }

        setListeners(simpleContentView);
        if (supportBigNotifications) {
            setListeners(expandedView);
        }

        Bitmap albumArt = audioInfo != null ? audioInfo.getSmallCover(MyApplication.applicationContext) : null;

        if (albumArt != null) {
            notification.contentView.setImageViewBitmap(R.id.player_album_art, albumArt);
            if (supportBigNotifications) {
                notification.bigContentView.setImageViewBitmap(R.id.player_album_art, albumArt);
            }
        } else {
            notification.contentView.setImageViewResource(R.id.player_album_art,
                    R.drawable.bg_default_album_art);
            if (supportBigNotifications) {
                notification.bigContentView.setImageViewResource(R.id.player_album_art,
                        R.drawable.bg_default_album_art);
            }
        }
        notification.contentView.setViewVisibility(R.id.player_progress_bar, View.GONE);
        notification.contentView.setViewVisibility(R.id.player_next, View.VISIBLE);
        notification.contentView.setViewVisibility(R.id.player_previous, View.VISIBLE);
        if (supportBigNotifications) {
            notification.bigContentView.setViewVisibility(R.id.player_next, View.VISIBLE);
            notification.bigContentView.setViewVisibility(R.id.player_previous, View.VISIBLE);
            notification.bigContentView.setViewVisibility(R.id.player_progress_bar, View.GONE);
        }

        if (MediaController.getInstance().isAudioPaused()) {
            notification.contentView.setViewVisibility(R.id.player_pause, View.GONE);
            notification.contentView.setViewVisibility(R.id.player_play, View.VISIBLE);
            if (supportBigNotifications) {
                notification.bigContentView.setViewVisibility(R.id.player_pause, View.GONE);
                notification.bigContentView.setViewVisibility(R.id.player_play, View.VISIBLE);
            }
        } else {
            notification.contentView.setViewVisibility(R.id.player_pause, View.VISIBLE);
            notification.contentView.setViewVisibility(R.id.player_play, View.GONE);
            if (supportBigNotifications) {
                notification.bigContentView.setViewVisibility(R.id.player_pause, View.VISIBLE);
                notification.bigContentView.setViewVisibility(R.id.player_play, View.GONE);
            }
        }

        notification.contentView.setTextViewText(R.id.player_song_name, songName);
        notification.contentView.setTextViewText(R.id.player_author_name, authorName);
        if (supportBigNotifications) {
            notification.bigContentView.setTextViewText(R.id.player_song_name, songName);
            notification.bigContentView.setTextViewText(R.id.player_author_name, authorName);
        }
        notification.flags |= Notification.FLAG_ONGOING_EVENT;
        startForeground(5, notification);

        if (remoteControlClient != null) {
            RemoteControlClient.MetadataEditor metadataEditor = remoteControlClient.editMetadata(true);
            metadataEditor.putString(MediaMetadataRetriever.METADATA_KEY_ARTIST, authorName);
            metadataEditor.putString(MediaMetadataRetriever.METADATA_KEY_TITLE, songName);
            if (audioInfo != null && audioInfo.getCover(MyApplication.applicationContext) != null) {
                metadataEditor.putBitmap(RemoteControlClient.MetadataEditor.BITMAP_KEY_ARTWORK,
                        audioInfo.getCover(MyApplication.applicationContext));
            }
            metadataEditor.apply();
            audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:kz.qobyzbook.manager.MusicPlayerService.java

@SuppressLint("NewApi")
private void createNotification(SongDetail mSongDetail) {
    try {/*from  www.  j  a va 2 s .com*/
        String songName = mSongDetail.getTitle();
        String authorName = mSongDetail.getArtist();
        String songImage = mSongDetail.getImage_url();
        SongDetail audioInfo = MediaController.getInstance().getPlayingSongDetail();

        RemoteViews simpleContentView = new RemoteViews(getApplicationContext().getPackageName(),
                R.layout.player_small_notification);
        RemoteViews expandedView = null;
        if (supportBigNotifications) {
            expandedView = new RemoteViews(getApplicationContext().getPackageName(),
                    R.layout.player_big_notification);
        }

        Intent intent = new Intent(ApplicationDMPlayer.applicationContext, DMPlayerBaseActivity.class);
        intent.setAction("openplayer");
        intent.setFlags(32768);
        PendingIntent contentIntent = PendingIntent.getActivity(ApplicationDMPlayer.applicationContext, 0,
                intent, 0);

        Notification notification = new NotificationCompat.Builder(getApplicationContext())
                .setSmallIcon(R.drawable.player).setContentIntent(contentIntent).setContentTitle(songName)
                .build();

        notification.contentView = simpleContentView;
        if (supportBigNotifications) {
            notification.bigContentView = expandedView;
        }

        setListeners(simpleContentView);
        if (supportBigNotifications) {
            setListeners(expandedView);
        }

        Bitmap albumArt = audioInfo != null ? audioInfo.getSmallCover(ApplicationDMPlayer.applicationContext)
                : null;

        if (albumArt != null) {
            notification.contentView.setImageViewBitmap(R.id.player_album_art, albumArt);
            if (supportBigNotifications) {
                notification.bigContentView.setImageViewBitmap(R.id.player_album_art, albumArt);
            }
        } else {
            notification.contentView.setImageViewResource(R.id.player_album_art,
                    R.drawable.bg_default_album_art);
            if (supportBigNotifications) {
                notification.bigContentView.setImageViewResource(R.id.player_album_art,
                        R.drawable.bg_default_album_art);
            }
        }
        notification.contentView.setViewVisibility(R.id.player_progress_bar, View.GONE);
        notification.contentView.setViewVisibility(R.id.player_next, View.VISIBLE);
        notification.contentView.setViewVisibility(R.id.player_previous, View.VISIBLE);
        if (supportBigNotifications) {
            notification.bigContentView.setViewVisibility(R.id.player_next, View.VISIBLE);
            notification.bigContentView.setViewVisibility(R.id.player_previous, View.VISIBLE);
            notification.bigContentView.setViewVisibility(R.id.player_progress_bar, View.GONE);
        }

        if (MediaController.getInstance().isAudioPaused()) {
            notification.contentView.setViewVisibility(R.id.player_pause, View.GONE);
            notification.contentView.setViewVisibility(R.id.player_play, View.VISIBLE);
            if (supportBigNotifications) {
                notification.bigContentView.setViewVisibility(R.id.player_pause, View.GONE);
                notification.bigContentView.setViewVisibility(R.id.player_play, View.VISIBLE);
            }
        } else {
            notification.contentView.setViewVisibility(R.id.player_pause, View.VISIBLE);
            notification.contentView.setViewVisibility(R.id.player_play, View.GONE);
            if (supportBigNotifications) {
                notification.bigContentView.setViewVisibility(R.id.player_pause, View.VISIBLE);
                notification.bigContentView.setViewVisibility(R.id.player_play, View.GONE);
            }
        }

        notification.contentView.setTextViewText(R.id.player_song_name, songName);
        notification.contentView.setTextViewText(R.id.player_author_name, authorName);
        if (supportBigNotifications) {
            notification.bigContentView.setTextViewText(R.id.player_song_name, songName);
            notification.bigContentView.setTextViewText(R.id.player_author_name, authorName);
            //                notification.bigContentView.setTextViewText(R.id.player_albumname, albumName);
        }
        notification.flags |= Notification.FLAG_ONGOING_EVENT;
        startForeground(5, notification);

        if (remoteControlClient != null) {
            RemoteControlClient.MetadataEditor metadataEditor = remoteControlClient.editMetadata(true);
            metadataEditor.putString(MediaMetadataRetriever.METADATA_KEY_ARTIST, authorName);
            metadataEditor.putString(MediaMetadataRetriever.METADATA_KEY_TITLE, songName);
            if (audioInfo != null && audioInfo.getCover(ApplicationDMPlayer.applicationContext) != null) {
                metadataEditor.putBitmap(RemoteControlClient.MetadataEditor.BITMAP_KEY_ARTWORK,
                        audioInfo.getCover(ApplicationDMPlayer.applicationContext));
            }
            metadataEditor.apply();
            audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.y20k.transistor.PlayerService.java

private boolean requestFocus() {
    int result = mAudioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
    return result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED;
}