@Override protected void onPostExecute(Integer connectionType) { if (connectionType == CONNECTION_TYPE_ERROR) { Toast.makeText(PlayerService.this, getString(R.string.toastalert_unable_to_connect), Toast.LENGTH_LONG).show(); stopPlayback(false); } else if (mStation.getPlaybackState() != PLAYBACK_STATE_STOPPED) { // prepare player preparePLayer(connectionType); // add listener mPlayer.addListener(PlayerService.this); // set content type mPlayer.setAudioAttributes(new AudioAttributes.Builder() .setUsage(C.USAGE_MEDIA) .setContentType(C.CONTENT_TYPE_MUSIC) .build() ); } // release init lock mPlayerInitLock = false; }
public SimpleExoPlayer2(RenderersFactory renderersFactory, TrackSelector trackSelector, LoadControl loadControl) { componentListener = new ComponentListener(); videoListeners = new CopyOnWriteArraySet<>(); textOutputs = new CopyOnWriteArraySet<>(); metadataOutputs = new CopyOnWriteArraySet<>(); Looper eventLooper = Looper.myLooper() != null ? Looper.myLooper() : Looper.getMainLooper(); Handler eventHandler = new Handler(eventLooper); renderers = renderersFactory.createRenderers(eventHandler, componentListener, componentListener, componentListener, componentListener); // Obtain counts of video and audio renderers. int videoRendererCount = 0; int audioRendererCount = 0; for (Renderer renderer : renderers) { switch (renderer.getTrackType()) { case C.TRACK_TYPE_VIDEO: videoRendererCount++; break; case C.TRACK_TYPE_AUDIO: audioRendererCount++; break; } } this.videoRendererCount = videoRendererCount; this.audioRendererCount = audioRendererCount; // Set initial values. audioVolume = 1; audioSessionId = C.AUDIO_SESSION_ID_UNSET; audioAttributes = AudioAttributes.DEFAULT; videoScalingMode = C.VIDEO_SCALING_MODE_DEFAULT; // Build the player and associated objects. player = createExoPlayerImpl(renderers, trackSelector, loadControl); }
@Deprecated public void setAudioStreamType(@C.StreamType int streamType) { @C.AudioUsage int usage = Util.getAudioUsageForStreamType(streamType); @C.AudioContentType int contentType = Util.getAudioContentTypeForStreamType(streamType); AudioAttributes audioAttributes = new AudioAttributes.Builder().setUsage(usage).setContentType(contentType).build(); setAudioAttributes(audioAttributes); }
public void setAudioAttributes(AudioAttributes audioAttributes) { this.audioAttributes = audioAttributes; ExoPlayerMessage[] messages = new ExoPlayerMessage[audioRendererCount]; int count = 0; for (Renderer renderer : renderers) { if (renderer.getTrackType() == C.TRACK_TYPE_AUDIO) { messages[count++] = new ExoPlayerMessage(renderer, C.MSG_SET_AUDIO_ATTRIBUTES, audioAttributes); } } player.sendMessages(messages); }
public void setAudioStreamType(int streamType) { @C.AudioUsage int usage = Util.getAudioUsageForStreamType(streamType); @C.AudioContentType int contentType = Util.getAudioContentTypeForStreamType(streamType); AudioAttributes audioAttributes = new AudioAttributes.Builder() .setUsage(usage) .setContentType(contentType) .build(); sendMessage(C.TRACK_TYPE_AUDIO, C.MSG_SET_AUDIO_ATTRIBUTES, audioAttributes); }
protected SimpleExoPlayer(RenderersFactory renderersFactory, TrackSelector trackSelector, LoadControl loadControl) { componentListener = new ComponentListener(); videoListeners = new CopyOnWriteArraySet<>(); textOutputs = new CopyOnWriteArraySet<>(); metadataOutputs = new CopyOnWriteArraySet<>(); videoDebugListeners = new CopyOnWriteArraySet<>(); audioDebugListeners = new CopyOnWriteArraySet<>(); Looper eventLooper = Looper.myLooper() != null ? Looper.myLooper() : Looper.getMainLooper(); Handler eventHandler = new Handler(eventLooper); renderers = renderersFactory.createRenderers(eventHandler, componentListener, componentListener, componentListener, componentListener); // Obtain counts of video and audio renderers. int videoRendererCount = 0; int audioRendererCount = 0; for (Renderer renderer : renderers) { switch (renderer.getTrackType()) { case C.TRACK_TYPE_VIDEO: videoRendererCount++; break; case C.TRACK_TYPE_AUDIO: audioRendererCount++; break; } } this.videoRendererCount = videoRendererCount; this.audioRendererCount = audioRendererCount; // Set initial values. audioVolume = 1; audioSessionId = C.AUDIO_SESSION_ID_UNSET; audioAttributes = AudioAttributes.DEFAULT; videoScalingMode = C.VIDEO_SCALING_MODE_DEFAULT; // Build the player and associated objects. player = createExoPlayerImpl(renderers, trackSelector, loadControl); }
/** * Returns the attributes for audio playback. */ public AudioAttributes getAudioAttributes() { return audioAttributes; }
@Override public void play(QueueItem item) { mPlayOnFocusGain = true; tryToGetAudioFocus(); registerAudioNoisyReceiver(); String mediaId = item.getDescription().getMediaId(); boolean mediaHasChanged = !TextUtils.equals(mediaId, mCurrentMediaId); if (mediaHasChanged) { mCurrentMediaId = mediaId; } if (mediaHasChanged || mExoPlayer == null) { releaseResources(false); // release everything except the player MediaMetadataCompat track = mMusicProvider.getMusic( MediaIDHelper.extractMusicIDFromMediaID( item.getDescription().getMediaId())); String source = track.getString(MusicProviderSource.CUSTOM_METADATA_TRACK_SOURCE); if (source != null) { source = source.replaceAll(" ", "%20"); // Escape spaces for URLs } if (mExoPlayer == null) { mExoPlayer = ExoPlayerFactory.newSimpleInstance( mContext, new DefaultTrackSelector(), new DefaultLoadControl()); mExoPlayer.addListener(mEventListener); } // Android "O" makes much greater use of AudioAttributes, especially // with regards to AudioFocus. All of UAMP's tracks are music, but // if your content includes spoken word such as audiobooks or podcasts // then the content type should be set to CONTENT_TYPE_SPEECH for those // tracks. final AudioAttributes audioAttributes = new AudioAttributes.Builder() .setContentType(CONTENT_TYPE_MUSIC) .setUsage(USAGE_MEDIA) .build(); mExoPlayer.setAudioAttributes(audioAttributes); // Produces DataSource instances through which media data is loaded. DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory( mContext, Util.getUserAgent(mContext, "uamp"), null); // Produces Extractor instances for parsing the media data. ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory(); // The MediaSource represents the media to be played. MediaSource mediaSource = new ExtractorMediaSource( Uri.parse(source), dataSourceFactory, extractorsFactory, null, null); // Prepares media to play (happens on background thread) and triggers // {@code onPlayerStateChanged} callback when the stream is ready to play. mExoPlayer.prepare(mediaSource); // If we are streaming from the internet, we want to hold a // Wifi lock, which prevents the Wifi radio from going to // sleep while the song is playing. mWifiLock.acquire(); } configurePlayerState(); }
/** * Sets the stream type for audio playback, used by the underlying audio track. * <p> * Setting the stream type during playback may introduce a short gap in audio output as the audio * track is recreated. A new audio session id will also be generated. * <p> * Calling this method overwrites any attributes set previously by calling * {@link #setAudioAttributes(AudioAttributes)}. * * @deprecated Use {@link #setAudioAttributes(AudioAttributes)}. * @param streamType The stream type for audio playback. */ @Deprecated public void setAudioStreamType(@C.StreamType int streamType) { @C.AudioUsage int usage = Util.getAudioUsageForStreamType(streamType); @C.AudioContentType int contentType = Util.getAudioContentTypeForStreamType(streamType); AudioAttributes audioAttributes = new AudioAttributes.Builder().setUsage(usage).setContentType(contentType).build(); setAudioAttributes(audioAttributes); }
/** * Sets the attributes for audio playback, used by the underlying audio track. If not set, the * default audio attributes will be used. They are suitable for general media playback. * <p> * Setting the audio attributes during playback may introduce a short gap in audio output as the * audio track is recreated. A new audio session id will also be generated. * <p> * If tunneling is enabled by the track selector, the specified audio attributes will be ignored, * but they will take effect if audio is later played without tunneling. * <p> * If the device is running a build before platform API version 21, audio attributes cannot be set * directly on the underlying audio track. In this case, the usage will be mapped onto an * equivalent stream type using {@link Util#getStreamTypeForAudioUsage(int)}. * * @param audioAttributes The attributes to use for audio playback. */ public void setAudioAttributes(AudioAttributes audioAttributes) { this.audioAttributes = audioAttributes; ExoPlayerMessage[] messages = new ExoPlayerMessage[audioRendererCount]; int count = 0; for (Renderer renderer : renderers) { if (renderer.getTrackType() == C.TRACK_TYPE_AUDIO) { messages[count++] = new ExoPlayerMessage(renderer, C.MSG_SET_AUDIO_ATTRIBUTES, audioAttributes); } } player.sendMessages(messages); }