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:co.codecrunch.musicplayerlite.manager.MusicPlayerService.java

@SuppressLint("NewApi")
private void createNotification(SongDetail mSongDetail) {
    try {/*from   www . jav a2 s.c  o m*/
        String songName = mSongDetail.getTitle();
        String authorName = mSongDetail.getArtist();
        String albumName = mSongDetail.getDisplay_name();
        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(ApplicationMusicPlayer.applicationContext, MusicPlayerBaseActivity.class);
        intent.setAction("openplayer");
        intent.setFlags(32768);
        PendingIntent contentIntent = PendingIntent.getActivity(ApplicationMusicPlayer.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(ApplicationMusicPlayer.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(ApplicationMusicPlayer.applicationContext) != null) {
                metadataEditor.putBitmap(RemoteControlClient.MetadataEditor.BITMAP_KEY_ARTWORK,
                        audioInfo.getCover(ApplicationMusicPlayer.applicationContext));
            }
            metadataEditor.apply();
            audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:in.risysnetworks.shplayer.mediacontroller.MusicPlayerService.java

@SuppressLint("NewApi")
private void createNotification(SongDetail mSongDetail) {
    try {/*from  www . ja  v a2 s .  c  om*/
        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(ApplicationSHPlayer.applicationContext, SHPlayerMainActivity.class);
        intent.setAction("openplayer");
        intent.setFlags(32768);
        PendingIntent contentIntent = PendingIntent.getActivity(ApplicationSHPlayer.applicationContext, 0,
                intent, 0);

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

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

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

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

        if (albumArt != null) {

            if (supportBigNotifications) {
                notification.bigContentView.setImageViewBitmap(R.id.player_album_art, albumArt);
            } else {
                notification.contentView.setImageViewBitmap(R.id.player_album_art, albumArt);
            }
        } else {

            if (supportBigNotifications) {
                notification.bigContentView.setImageViewResource(R.id.player_album_art,
                        R.drawable.bg_default_album_art);
            } else {
                notification.contentView.setImageViewResource(R.id.player_album_art,
                        R.drawable.bg_default_album_art);
            }
        }

        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);
        } else {
            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 (MediaController.getInstance().isAudioPaused()) {

            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.GONE);
                notification.contentView.setViewVisibility(R.id.player_play, View.VISIBLE);
            }
        } else {

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

        if (supportBigNotifications) {
            notification.bigContentView.setTextViewText(R.id.player_song_name, songName);
            notification.bigContentView.setTextViewText(R.id.player_author_name, authorName);
        } else {
            notification.contentView.setTextViewText(R.id.player_song_name, songName);
            notification.contentView.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(ApplicationSHPlayer.applicationContext) != null) {
                metadataEditor.putBitmap(RemoteControlClient.MetadataEditor.BITMAP_KEY_ARTWORK,
                        audioInfo.getCover(ApplicationSHPlayer.applicationContext));
            }
            metadataEditor.apply();
            audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
        }
    } catch (Exception e) {
        e.printStackTrace();

        System.out.println(" error --> " + e.toString());
    }
}

From source file:com.xnxs.mediaplayer.widget.media.VRVideoView.java

@TargetApi(Build.VERSION_CODES.M)
private void openVideo() {
    if ((mUri == null && mIMediaDataSource == null) || mSurfaceHolder == null) {
        // not ready for playback just yet, will try again later
        return;/*www .ja  v a  2 s  . c o  m*/
    }
    // we shouldn't clear the target state, because somebody might have
    // called start() previously
    release(false);

    AudioManager am = getAudioManager();
    am.requestAudioFocus(null, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);

    try {
        mMediaPlayer = createPlayer(Settings.PV_PLAYER__IjkMediaPlayer);
        // TODO: create SubtitleController in MediaPlayer, but we need
        // a context for the subtitle renderers
        final Context context = getContext();
        // REMOVED: SubtitleController

        // REMOVED: mAudioSession
        mMediaPlayer.setOnPreparedListener(mPreparedListener);
        mMediaPlayer.setOnVideoSizeChangedListener(mSizeChangedListener);
        mMediaPlayer.setOnCompletionListener(mCompletionListener);
        mMediaPlayer.setOnErrorListener(mErrorListener);
        mMediaPlayer.setOnInfoListener(mInfoListener);
        mMediaPlayer.setOnBufferingUpdateListener(mBufferingUpdateListener);
        mCurrentBufferPercentage = 0;

        if (mUri != null) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
                mMediaPlayer.setDataSource(mAppContext, mUri, mHeaders);
            } else {
                mMediaPlayer.setDataSource(mUri.toString());
            }
        } else {
            mMediaPlayer.setDataSource(mIMediaDataSource);
        }
        bindSurfaceHolder(mMediaPlayer, mSurfaceHolder);
        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mMediaPlayer.setScreenOnWhilePlaying(true);
        mMediaPlayer.prepareAsync();
        // REMOVED: mPendingSubtitleTracks

        // we don't set the target state here either, but preserve the
        // target state that was there before.
        mCurrentState = STATE_PREPARING;
        attachMediaController();
    } catch (IOException ex) {
        Log.w(TAG, "Unable to open content: " + mUri, ex);
        mCurrentState = STATE_ERROR;
        mTargetState = STATE_ERROR;
        mErrorListener.onError(mMediaPlayer, MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
    } catch (IllegalArgumentException ex) {
        Log.w(TAG, "Unable to open content: " + mUri, ex);
        mCurrentState = STATE_ERROR;
        mTargetState = STATE_ERROR;
        mErrorListener.onError(mMediaPlayer, MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
    } finally {
        // REMOVED: mPendingSubtitleTracks.clear();
    }
}

From source file:org.amahi.anywhere.service.AudioService.java

@Override
public void onAudioFocusChange(int audioFocus) {
    switch (audioFocus) {
    case AudioManager.AUDIOFOCUS_GAIN:
        handleAudioFocusGain();/* w  ww .  j  a  v a  2s.  co m*/
        break;

    case AudioManager.AUDIOFOCUS_LOSS:
    case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
        handleAudioFocusLoss();
        break;

    case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
        handleAudioFocusDuck();
        break;

    default:
        break;
    }
}

From source file:com.allthatseries.RNAudioPlayer.Playback.java

/**
 * Called by AudioManager on audio focus changes.
 * Implementation of {@link AudioManager.OnAudioFocusChangeListener}
 *//*from  w w w .j ava  2 s. c o m*/
@Override
public void onAudioFocusChange(int focusChange) {
    if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
        // We have gained focus:
        mAudioFocus = AUDIO_FOCUSED;

    } else if (focusChange == AudioManager.AUDIOFOCUS_LOSS
            || focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT
            || focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) {
        // We have lost focus. If we can duck (low playback volume), we can keep playing.
        // Otherwise, we need to pause the playback.
        boolean canDuck = focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK;
        mAudioFocus = canDuck ? AUDIO_NO_FOCUS_CAN_DUCK : AUDIO_NO_FOCUS_NO_DUCK;

        // If we are playing, we need to reset media player by calling configMediaPlayerState
        // with mAudioFocus properly set.
        if (mState == PlaybackStateCompat.STATE_PLAYING && !canDuck) {
            // If we don't have audio focus and can't duck, we save the information that
            // we were playing, so that we can resume playback once we get the focus back.
            mPlayOnFocusGain = true;
        }
    } else {
        Log.e(TAG, "onAudioFocusChange: Ignoring unsupported focusChange: " + focusChange);
    }
    configMediaPlayerState();
}

From source file:com.goftagram.telegram.messenger.MusicPlayerService.java

@SuppressLint("NewApi")
private void createNotification(MessageObject messageObject) {
    String songName = messageObject.getMusicTitle();
    String authorName = messageObject.getMusicAuthor();
    AudioInfo audioInfo = MediaController.getInstance().getAudioInfo();

    RemoteViews simpleContentView = new RemoteViews(getApplicationContext().getPackageName(),
            R.layout.player_small_notification);
    RemoteViews expandedView = null;//from w w w .j  av a 2 s.com
    if (supportBigNotifications) {
        expandedView = new RemoteViews(getApplicationContext().getPackageName(),
                R.layout.player_big_notification);
    }

    Intent intent = new Intent(ApplicationLoader.applicationContext, LaunchActivity.class);
    intent.setAction("com.tmessages.openplayer");
    intent.setFlags(32768);
    PendingIntent contentIntent = PendingIntent.getActivity(ApplicationLoader.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() : 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.nocover_small);
        if (supportBigNotifications) {
            notification.bigContentView.setImageViewResource(R.id.player_album_art, R.drawable.nocover_big);
        }
    }
    if (MediaController.getInstance().isDownloadingCurrentMessage()) {
        notification.contentView.setViewVisibility(R.id.player_pause, View.GONE);
        notification.contentView.setViewVisibility(R.id.player_play, View.GONE);
        notification.contentView.setViewVisibility(R.id.player_next, View.GONE);
        notification.contentView.setViewVisibility(R.id.player_previous, View.GONE);
        notification.contentView.setViewVisibility(R.id.player_progress_bar, View.VISIBLE);
        if (supportBigNotifications) {
            notification.bigContentView.setViewVisibility(R.id.player_pause, View.GONE);
            notification.bigContentView.setViewVisibility(R.id.player_play, View.GONE);
            notification.bigContentView.setViewVisibility(R.id.player_next, View.GONE);
            notification.bigContentView.setViewVisibility(R.id.player_previous, View.GONE);
            notification.bigContentView.setViewVisibility(R.id.player_progress_bar, View.VISIBLE);
        }
    } else {
        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() != null) {
            metadataEditor.putBitmap(RemoteControlClient.MetadataEditor.BITMAP_KEY_ARTWORK,
                    audioInfo.getCover());
        }
        metadataEditor.apply();
        audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
    }
}

From source file:com.google.android.car.kitchensink.radio.RadioTestFragment.java

private void addHandlers() {
    mOpenRadio.setOnClickListener(new View.OnClickListener() {
        @Override//from   w ww.j  a  v  a 2  s .  c o  m
        public void onClick(View v) {
            handleRadioStart();
            updateStates();
        }
    });
    mCloseRadio.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            handleRadioEnd();
            mToggleMuteRadio.setChecked(true);
            updateStates();
        }
    });
    mToggleMuteRadio.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (DBG) {
                Log.i(TAG, "Toggle mute radio");
            }
            mRadioTuner.setMute(!mRadioTuner.getMute());
            updateStates();
        }
    });
    mGetRadioFocus.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (DBG) {
                Log.i(TAG, "Get radio focus");
            }
            try {
                mCarAudioManager.requestAudioFocus(mRadioFocusListener, mRadioAudioAttrib,
                        AudioManager.AUDIOFOCUS_GAIN, 0);
            } catch (CarNotConnectedException e) {
                //ignore for now
            }
            mHasRadioFocus = true;
            updateStates();
        }
    });
    mReleaseRadioFocus.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (DBG) {
                Log.i(TAG, "Release radio focus");
            }
            mCarAudioManager.abandonAudioFocus(mRadioFocusListener, mRadioAudioAttrib);
            mHasRadioFocus = false;
            updateStates();
        }
    });
    mGetFocus.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (DBG) {
                Log.i(TAG, "Get secondary focus");
            }
            mAudioManager.requestAudioFocus(mSecondaryFocusListener, AudioManager.STREAM_MUSIC,
                    AudioManager.AUDIOFOCUS_GAIN);
            mHasSecondaryFocus = true;
            updateStates();
        }
    });
    mReleaseFocus.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (DBG) {
                Log.i(TAG, "Release secondary focus");
            }
            mAudioManager.abandonAudioFocus(mSecondaryFocusListener);
            mHasSecondaryFocus = false;
            updateStates();
        }
    });
    mRadioNext.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (DBG) {
                Log.i(TAG, "Next radio station");
            }
            if (mRadioTuner != null) {
                mRadioTuner.scan(RadioTuner.DIRECTION_UP, true);
            }
            updateStates();
        }
    });
    mRadioPrev.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (DBG) {
                Log.i(TAG, "Previous radio station");
            }
            if (mRadioTuner != null) {
                mRadioTuner.scan(RadioTuner.DIRECTION_DOWN, true);
            }
            updateStates();
        }
    });
    mRadioScanCancel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (DBG) {
                Log.i(TAG, "Cancel radio scan");
            }
            if (mRadioTuner != null) {
                mRadioTuner.cancel();
            }
            updateStates();
        }
    });
    mRadioTuneToStation.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (DBG) {
                Log.i(TAG, "Tuning to station");
            }
            String station = mStationFrequency.getText().toString().trim();
            if (mRadioTuner != null && !(station.equals(""))) {
                mRadioTuner.tune(Integer.parseInt(station), 0);
            }
            resetMessages();
            updateMessages();
            updateStates();
        }
    });
    mRadioStepUp.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (DBG) {
                Log.i(TAG, "Step up");
            }
            if (mRadioTuner != null) {
                mRadioTuner.step(RadioTuner.DIRECTION_UP, false);
            }
            resetMessages();
            updateMessages();
            updateStates();
        }
    });
    mRadioStepDown.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (DBG) {
                Log.i(TAG, "Step down");
            }
            if (mRadioTuner != null) {
                mRadioTuner.step(RadioTuner.DIRECTION_DOWN, false);
            }
            resetMessages();
            updateMessages();
            updateStates();
        }
    });
    mRadioGetProgramInfo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (DBG) {
                Log.i(TAG, "getProgramInformation");
            }
            if (mRadioTuner != null) {
                RadioManager.ProgramInfo[] programInfos = new RadioManager.ProgramInfo[1];
                mRadioTuner.getProgramInformation(programInfos);
                addLog(Log.INFO, "mRadioTuner.getProgramInformation() =>" + programInfos[0]);
            }
        }
    });
    mRadioBand.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (DBG) {
                Log.i(TAG, "Changing radio band");
            }
            if (mRadioTuner != null) {
                mRadioTuner.setConfiguration(mRadioBand.isChecked() ? mFmConfig : mAmConfig);
            }
            resetMessages();
            updateMessages();
            updateStates();
        }
    });
}

From source file:org.videolan.vlc.AudioService.java

@TargetApi(8)
private void changeAudioFocus(boolean gain) {
    if (!Util.isFroyoOrLater()) // NOP if not supported
        return;// ww  w .j a  v  a 2s.  co m

    audioFocusListener = new OnAudioFocusChangeListener() {
        @Override
        public void onAudioFocusChange(int focusChange) {
            if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK
                    || focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT) {
                /*
                 * Lower the volume to 36% to "duck" when an alert or something
                 * needs to be played.
                 */
                LibVLC.getExistingInstance().setVolume(36);
            } else {
                LibVLC.getExistingInstance().setVolume(100);
            }
        }
    };

    AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
    if (gain)
        am.requestAudioFocus(audioFocusListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
    else
        am.abandonAudioFocus(audioFocusListener);

}

From source file:dk.dr.radio.afspilning.Afspiller.java

    /**
 * Responding to the loss of audio focus
 */// ww w  . j a  v a  2  s.  c  om
@SuppressLint("NewApi")
private OnAudioFocusChangeListener getOnAudioFocusChangeListener() {
  if (onAudioFocusChangeListener == null)
    onAudioFocusChangeListener = new OnAudioFocusChangeListener() {

      //private int lydstyreFrDuck = -1;

      @TargetApi(Build.VERSION_CODES.FROYO)
      public void onAudioFocusChange(int focusChange) {
        Log.d("onAudioFocusChange " + focusChange);
        AudioManager am = (AudioManager) App.instans.getSystemService(Context.AUDIO_SERVICE);

        switch (focusChange) {
          // Kommer ved f.eks. en SMS eller taleinstruktion i Google Maps
          case (AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK):
            Log.d("JPER duck");
            if (afspillerstatus != Status.STOPPET) {
              // Vi 'dukker' lyden mens den vigtigere lyd hres
              // St lydstyrken ned til en 1/3-del
              //lydstyreFrDuck = am.getStreamVolume(AudioManager.STREAM_MUSIC);
              //am.setStreamVolume(AudioManager.STREAM_MUSIC, (lydstyreFrDuck + 2) / 3, 0);
              mediaPlayer.setVolume(0.1f, 0.1f); // logaritmisk skala - 0.1 svarer til 1/3-del
            }
            break;

          // Dette sker ved f.eks. opkald
          case (AudioManager.AUDIOFOCUS_LOSS_TRANSIENT):
            Log.d("JPER pause");
            if (afspillerstatus != Status.STOPPET) {
              pauseAfspilning(); // stter afspilningPPause=false
              if (afspillerlyde) afspillerlyd.stop.start();
              afspilningPPause = true;
            }
            break;

          // Dette sker hvis en anden app med lyd startes, f.eks. et spil
          case (AudioManager.AUDIOFOCUS_LOSS):
            Log.d("JPER stop");
            stopAfspilning();
            am.abandonAudioFocus(this);
            break;

          // Dette sker nr opkaldet er slut og ved f.eks. opkald
          case (AudioManager.AUDIOFOCUS_GAIN):
            Log.d("JPER Gain");
            if (afspillerstatus == Status.STOPPET) {
              if (afspilningPPause) startAfspilningIntern();
            } else {
              // Genskab lydstyrke fr den blev dukket
              mediaPlayer.setVolume(1f, 1f);
              //if (lydstyreFrDuck > 0) {
              //  am.setStreamVolume(AudioManager.STREAM_MUSIC, lydstyreFrDuck, 0);
              //}
              // Genstart ikke afspilning, der spilles allerede!
              //startAfspilningIntern();
            }
        }
      }
    };
  return (OnAudioFocusChangeListener) onAudioFocusChangeListener;
}

From source file:com.customprogrammingsolutions.MediaStreamer.MediaStreamerService.java

@Override
public void onAudioFocusChange(int focusChange) {
    //very inefficient...try optimizing when you have time  
    switch (focusChange) {
    case AudioManager.AUDIOFOCUS_GAIN:
        stop();//from   www  . j a v a 2  s  .  c o  m
        play();
        break;

    case AudioManager.AUDIOFOCUS_LOSS:
        // Lost focus for an unbounded amount of time: stop playback and release media player
        stop();
        break;

    case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
        // Lost focus for a short time, but we have to stop
        stop();
        break;

    case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
        // Lost focus for a short time, but it's ok to keep playing
        // at an attenuated level
        stop();
        break;
    }

}