当前位置: 首页>>代码示例>>Java>>正文


Java AudioFocusRequest类代码示例

本文整理汇总了Java中android.media.AudioFocusRequest的典型用法代码示例。如果您正苦于以下问题:Java AudioFocusRequest类的具体用法?Java AudioFocusRequest怎么用?Java AudioFocusRequest使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


AudioFocusRequest类属于android.media包,在下文中一共展示了AudioFocusRequest类的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: requestAudioFocus

import android.media.AudioFocusRequest; //导入依赖的package包/类
public boolean requestAudioFocus(OnAudioFocusChangeListener focusChangeListener,
                                 int streamType, int audioFocusGain) {
    int r;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        r = mAudioManager.requestAudioFocus(
                new AudioFocusRequest.Builder(audioFocusGain)
                        .setAudioAttributes(
                                new AudioAttributes.Builder()
                                        .setLegacyStreamType(streamType)
                                        .build())
                        .setOnAudioFocusChangeListener(focusChangeListener)
                        .build());
    } else {
        //noinspection deprecation
        r = mAudioManager.requestAudioFocus(focusChangeListener, streamType, audioFocusGain);
    }

    return r == AudioManager.AUDIOFOCUS_REQUEST_GRANTED;
}
 
开发者ID:marverenic,项目名称:Jockey,代码行数:20,代码来源:AudioManagerCompat.java

示例2: onCreate

import android.media.AudioFocusRequest; //导入依赖的package包/类
@Override
public void onCreate() {
    super.onCreate();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_DEFAULT_CHANNEL_ID, getString(R.string.notification_channel_name), NotificationManagerCompat.IMPORTANCE_DEFAULT);
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.createNotificationChannel(notificationChannel);

        AudioAttributes audioAttributes = new AudioAttributes.Builder()
                .setUsage(AudioAttributes.USAGE_MEDIA)
                .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
                .build();
        audioFocusRequest = new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN)
                .setOnAudioFocusChangeListener(audioFocusChangeListener)
                .setAcceptsDelayedFocusGain(false)
                .setWillPauseWhenDucked(true)
                .setAudioAttributes(audioAttributes)
                .build();
    }

    audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

    mediaSession = new MediaSessionCompat(this, "PlayerService");
    mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
    mediaSession.setCallback(mediaSessionCallback);

    Context appContext = getApplicationContext();

    Intent activityIntent = new Intent(appContext, MainActivity.class);
    mediaSession.setSessionActivity(PendingIntent.getActivity(appContext, 0, activityIntent, 0));

    Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null, appContext, MediaButtonReceiver.class);
    mediaSession.setMediaButtonReceiver(PendingIntent.getBroadcast(appContext, 0, mediaButtonIntent, 0));

    exoPlayer = ExoPlayerFactory.newSimpleInstance(new DefaultRenderersFactory(this), new DefaultTrackSelector(), new DefaultLoadControl());
    exoPlayer.addListener(exoPlayerListener);
    DataSource.Factory httpDataSourceFactory = new OkHttpDataSourceFactory(new OkHttpClient(), Util.getUserAgent(this, getString(R.string.app_name)), null);
    Cache cache = new SimpleCache(new File(this.getCacheDir().getAbsolutePath() + "/exoplayer"), new LeastRecentlyUsedCacheEvictor(1024 * 1024 * 100)); // 100 Mb max
    this.dataSourceFactory = new CacheDataSourceFactory(cache, httpDataSourceFactory, CacheDataSource.FLAG_BLOCK_ON_CACHE | CacheDataSource.FLAG_IGNORE_CACHE_ON_ERROR);
    this.extractorsFactory = new DefaultExtractorsFactory();
}
 
开发者ID:SergeyVinyar,项目名称:AndroidAudioExample,代码行数:43,代码来源:PlayerService.java

示例3: requestAudioFocus

import android.media.AudioFocusRequest; //导入依赖的package包/类
private void requestAudioFocus() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        AudioAttributes playbackAttributes = new AudioAttributes.Builder()
                .setUsage(AudioAttributes.USAGE_VOICE_COMMUNICATION)
                .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
                .build();
        AudioFocusRequest focusRequest =
                new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN_TRANSIENT)
                        .setAudioAttributes(playbackAttributes)
                        .setAcceptsDelayedFocusGain(true)
                        .setOnAudioFocusChangeListener(
                                new AudioManager.OnAudioFocusChangeListener() {
                                    @Override
                                    public void onAudioFocusChange(int i) { }
                                })
                        .build();
        audioManager.requestAudioFocus(focusRequest);
    } else {
        audioManager.requestAudioFocus(null, AudioManager.STREAM_VOICE_CALL,
                AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
    }
}
 
开发者ID:twilio,项目名称:video-quickstart-android,代码行数:23,代码来源:VideoActivity.java

示例4: setAudioFocus

import android.media.AudioFocusRequest; //导入依赖的package包/类
private void setAudioFocus(boolean setFocus) {
    if (audioManager != null) {
        if (setFocus) {
            savedAudioMode = audioManager.getMode();
            // Request audio focus before making any device switch.
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                AudioAttributes playbackAttributes = new AudioAttributes.Builder()
                        .setUsage(AudioAttributes.USAGE_VOICE_COMMUNICATION)
                        .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
                        .build();
                AudioFocusRequest focusRequest = new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN_TRANSIENT)
                        .setAudioAttributes(playbackAttributes)
                        .setAcceptsDelayedFocusGain(true)
                        .setOnAudioFocusChangeListener(new AudioManager.OnAudioFocusChangeListener() {
                            @Override
                            public void onAudioFocusChange(int i) {
                            }
                        })
                        .build();
                audioManager.requestAudioFocus(focusRequest);
            } else {
                audioManager.requestAudioFocus(null, AudioManager.STREAM_VOICE_CALL,
                        AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
            }
            /*
             * Start by setting MODE_IN_COMMUNICATION as default audio mode. It is
             * required to be in this mode when playout and/or recording starts for
             * best possible VoIP performance. Some devices have difficulties with speaker mode
             * if this is not set.
             */
            audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
        } else {
            audioManager.setMode(savedAudioMode);
            audioManager.abandonAudioFocus(null);
        }
    }
}
 
开发者ID:twilio,项目名称:voice-quickstart-android,代码行数:38,代码来源:VoiceActivity.java

示例5: abandonAudioFocus

import android.media.AudioFocusRequest; //导入依赖的package包/类
public void abandonAudioFocus(OnAudioFocusChangeListener focusChangeListener) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        mAudioManager.abandonAudioFocusRequest(
                new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN)
                        .setOnAudioFocusChangeListener(focusChangeListener)
                        .build());
    } else {
        //noinspection deprecation
        mAudioManager.abandonAudioFocus(focusChangeListener);
    }
}
 
开发者ID:marverenic,项目名称:Jockey,代码行数:12,代码来源:AudioManagerCompat.java

示例6: setAudioFocus

import android.media.AudioFocusRequest; //导入依赖的package包/类
private void setAudioFocus() {
    if (audioManager == null) {
        return;
    }
    originalAudioMode = audioManager.getMode();
    // Request audio focus before making any device switch
    if (Build.VERSION.SDK_INT >= 26) {
        AudioAttributes playbackAttributes = new AudioAttributes.Builder()
            .setUsage(AudioAttributes.USAGE_VOICE_COMMUNICATION)
            .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
            .build();
        focusRequest = new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE)
            .setAudioAttributes(playbackAttributes)
            .setAcceptsDelayedFocusGain(true)
            .setOnAudioFocusChangeListener(new AudioManager.OnAudioFocusChangeListener() {
                @Override
                public void onAudioFocusChange(int i) { }
            })
            .build();
        audioManager.requestAudioFocus(focusRequest);
    } else {
        audioManager.requestAudioFocus(
            null,
            AudioManager.STREAM_VOICE_CALL,
            AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE
        );
    }
    /*
     * Start by setting MODE_IN_COMMUNICATION as default audio mode. It is
     * required to be in this mode when playout and/or recording starts for
     * best possible VoIP performance. Some devices have difficulties with speaker mode
     * if this is not set.
     */
    audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
}
 
开发者ID:hoxfon,项目名称:react-native-twilio-programmable-voice,代码行数:36,代码来源:TwilioVoiceModule.java

示例7:

import android.media.AudioFocusRequest; //导入依赖的package包/类
@RequiresApi(Build.VERSION_CODES.O)
/* package */ AudioFocusRequest getAudioFocusRequest() {
    return new AudioFocusRequest.Builder(mFocusGain)
            .setAudioAttributes(getAudioAttributes())
            .setAcceptsDelayedFocusGain(mAcceptsDelayedFocusGain)
            .setWillPauseWhenDucked(mPauseOnDuck)
            .setOnAudioFocusChangeListener(mOnAudioFocusChangeListener, mFocusChangeHandler)
            .build();
}
 
开发者ID:y20k,项目名称:transistor,代码行数:10,代码来源:AudioFocusRequestCompat.java

示例8: initAudioFocusRequest

import android.media.AudioFocusRequest; //导入依赖的package包/类
private void initAudioFocusRequest() {
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    instructionFocusRequest = new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK)
      .build();
  }
}
 
开发者ID:mapbox,项目名称:mapbox-navigation-android,代码行数:7,代码来源:NavigationInstructionPlayer.java


注:本文中的android.media.AudioFocusRequest类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。