Search in sources :

Example 11 with DefaultExtractorsFactory

use of androidx.media3.extractor.DefaultExtractorsFactory in project android-UniversalMusicPlayer by googlesamples.

the class LocalPlayback method play.

@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) {
        // release everything except the player
        releaseResources(false);
        MediaMetadataCompat track = mMusicProvider.getMusic(MediaIDHelper.extractMusicIDFromMediaID(item.getDescription().getMediaId()));
        String source = track.getString(MusicProviderSource.CUSTOM_METADATA_TRACK_SOURCE);
        if (source != null) {
            // Escape spaces for URLs
            source = source.replaceAll(" ", "%20");
        }
        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();
}
Also used : DefaultExtractorsFactory(com.google.android.exoplayer2.extractor.DefaultExtractorsFactory) MediaMetadataCompat(android.support.v4.media.MediaMetadataCompat) AudioAttributes(com.google.android.exoplayer2.audio.AudioAttributes) ExtractorMediaSource(com.google.android.exoplayer2.source.ExtractorMediaSource) DefaultLoadControl(com.google.android.exoplayer2.DefaultLoadControl) DataSource(com.google.android.exoplayer2.upstream.DataSource) ExtractorMediaSource(com.google.android.exoplayer2.source.ExtractorMediaSource) MediaSource(com.google.android.exoplayer2.source.MediaSource) ExtractorsFactory(com.google.android.exoplayer2.extractor.ExtractorsFactory) DefaultExtractorsFactory(com.google.android.exoplayer2.extractor.DefaultExtractorsFactory) DefaultDataSourceFactory(com.google.android.exoplayer2.upstream.DefaultDataSourceFactory) DefaultTrackSelector(com.google.android.exoplayer2.trackselection.DefaultTrackSelector)

Example 12 with DefaultExtractorsFactory

use of androidx.media3.extractor.DefaultExtractorsFactory in project react-native-track-player by react-native-kit.

the class ExoPlayback method load.

@Override
public void load(Track track, Promise callback) {
    loadCallback = callback;
    Uri url = track.url;
    String userAgent = Util.getUserAgent(context, "react-native-track-player");
    DefaultHttpDataSourceFactory httpDataSourceFactory = new DefaultHttpDataSourceFactory(userAgent, null, DefaultHttpDataSource.DEFAULT_CONNECT_TIMEOUT_MILLIS, DefaultHttpDataSource.DEFAULT_READ_TIMEOUT_MILLIS, true);
    DataSource.Factory factory = new DefaultDataSourceFactory(context, null, httpDataSourceFactory);
    MediaSource source;
    if (cacheMaxSize > 0 && !track.urlLocal) {
        File cacheDir = new File(context.getCacheDir(), "TrackPlayer");
        Cache cache = new SimpleCache(cacheDir, new LeastRecentlyUsedCacheEvictor(cacheMaxSize));
        factory = new CacheDataSourceFactory(cache, factory, 0, cacheMaxSize);
    }
    if (track.type == TrackType.DASH) {
        source = new DashMediaSource(url, factory, new DefaultDashChunkSource.Factory(factory), null, null);
    } else if (track.type == TrackType.HLS) {
        source = new HlsMediaSource(url, factory, null, null);
    } else if (track.type == TrackType.SMOOTH_STREAMING) {
        source = new SsMediaSource(url, factory, new DefaultSsChunkSource.Factory(factory), null, null);
    } else {
        source = new ExtractorMediaSource(url, factory, new DefaultExtractorsFactory(), null, null);
    }
    player.prepare(source);
}
Also used : DefaultExtractorsFactory(com.google.android.exoplayer2.extractor.DefaultExtractorsFactory) LeastRecentlyUsedCacheEvictor(com.google.android.exoplayer2.upstream.cache.LeastRecentlyUsedCacheEvictor) DashMediaSource(com.google.android.exoplayer2.source.dash.DashMediaSource) CacheDataSourceFactory(com.google.android.exoplayer2.upstream.cache.CacheDataSourceFactory) DefaultHttpDataSourceFactory(com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory) DefaultDataSourceFactory(com.google.android.exoplayer2.upstream.DefaultDataSourceFactory) DefaultExtractorsFactory(com.google.android.exoplayer2.extractor.DefaultExtractorsFactory) ExtractorMediaSource(com.google.android.exoplayer2.source.ExtractorMediaSource) SsMediaSource(com.google.android.exoplayer2.source.smoothstreaming.SsMediaSource) Uri(android.net.Uri) DataSource(com.google.android.exoplayer2.upstream.DataSource) DefaultHttpDataSource(com.google.android.exoplayer2.upstream.DefaultHttpDataSource) HlsMediaSource(com.google.android.exoplayer2.source.hls.HlsMediaSource) HlsMediaSource(com.google.android.exoplayer2.source.hls.HlsMediaSource) DashMediaSource(com.google.android.exoplayer2.source.dash.DashMediaSource) ExtractorMediaSource(com.google.android.exoplayer2.source.ExtractorMediaSource) MediaSource(com.google.android.exoplayer2.source.MediaSource) SsMediaSource(com.google.android.exoplayer2.source.smoothstreaming.SsMediaSource) SimpleCache(com.google.android.exoplayer2.upstream.cache.SimpleCache) CacheDataSourceFactory(com.google.android.exoplayer2.upstream.cache.CacheDataSourceFactory) DefaultHttpDataSourceFactory(com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory) DefaultDataSourceFactory(com.google.android.exoplayer2.upstream.DefaultDataSourceFactory) File(java.io.File) Cache(com.google.android.exoplayer2.upstream.cache.Cache) SimpleCache(com.google.android.exoplayer2.upstream.cache.SimpleCache)

Example 13 with DefaultExtractorsFactory

use of androidx.media3.extractor.DefaultExtractorsFactory in project NiceMusic by lizixian18.

the class ExoPlayback method play.

@Override
public void play(SongInfo info) {
    mPlayOnFocusGain = true;
    mFocusAndLockManager.tryToGetAudioFocus();
    registerAudioNoisyReceiver();
    String mediaId = info.getSongId();
    boolean mediaHasChanged = !TextUtils.equals(mediaId, mCurrentMediaId);
    if (mediaHasChanged) {
        mCurrentMediaId = mediaId;
        mCurrentMediaSongInfo = info;
    }
    if (mediaHasChanged || mExoPlayer == null) {
        // release everything except the player
        releaseResources(false);
        String source = info.getSongUrl();
        if (source != null) {
            // Escape spaces for URLs
            source = source.replaceAll(" ", "%20");
        }
        if (mExoPlayer == null) {
            mExoPlayer = ExoPlayerFactory.newSimpleInstance(mContext, new DefaultTrackSelector(), new DefaultLoadControl());
            mExoPlayer.addListener(mEventListener);
        }
        final AudioAttributes audioAttributes = new AudioAttributes.Builder().setContentType(CONTENT_TYPE_MUSIC).setUsage(USAGE_MEDIA).build();
        mExoPlayer.setAudioAttributes(audioAttributes);
        DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(mContext, Util.getUserAgent(mContext, "musiclibrary"), null);
        ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();
        // MediaSource mediaSource = new ExtractorMediaSource(Uri.parse(source), dataSourceFactory, extractorsFactory, null, null);
        MediaSource mediaSource = buildMediaSource(Uri.parse(source), null, null, null);
        mExoPlayer.prepare(mediaSource);
        mFocusAndLockManager.acquireWifiLock();
    }
    configurePlayerState();
}
Also used : DefaultExtractorsFactory(com.google.android.exoplayer2.extractor.DefaultExtractorsFactory) HlsMediaSource(com.google.android.exoplayer2.source.hls.HlsMediaSource) DashMediaSource(com.google.android.exoplayer2.source.dash.DashMediaSource) ExtractorMediaSource(com.google.android.exoplayer2.source.ExtractorMediaSource) MediaSource(com.google.android.exoplayer2.source.MediaSource) SsMediaSource(com.google.android.exoplayer2.source.smoothstreaming.SsMediaSource) ExtractorsFactory(com.google.android.exoplayer2.extractor.ExtractorsFactory) DefaultExtractorsFactory(com.google.android.exoplayer2.extractor.DefaultExtractorsFactory) DefaultDataSourceFactory(com.google.android.exoplayer2.upstream.DefaultDataSourceFactory) DefaultTrackSelector(com.google.android.exoplayer2.trackselection.DefaultTrackSelector) AudioAttributes(com.google.android.exoplayer2.audio.AudioAttributes) DefaultLoadControl(com.google.android.exoplayer2.DefaultLoadControl) HttpDataSource(com.google.android.exoplayer2.upstream.HttpDataSource) DataSource(com.google.android.exoplayer2.upstream.DataSource)

Example 14 with DefaultExtractorsFactory

use of androidx.media3.extractor.DefaultExtractorsFactory in project United4 by nilesr.

the class United method playSong.

/**
 * Plays the given song, by finding its R.raw id in the songs map, stored in properties
 * If notify is set set to true, will notify the webpage. For more information, see NotifierService's documentation
 *
 * @param song   the full name of the song to play
 * @param reload whether to notify the web page
 */
public static void playSong(String song, final boolean reload) {
    Log.i(TAG, "playSong called on " + song);
    // Find the filename in the map
    String songs = P.get("songs");
    JsonReader reader = new JsonReader(new StringReader(songs));
    String id = null;
    try {
        reader.beginObject();
        while (reader.hasNext()) {
            String name = reader.nextName();
            String read = reader.nextString();
            // noinspection EqualsReplaceableByObjectsCall
            if (name.equals(song)) {
                id = read;
                Log.i(TAG, "Song id is " + id);
                break;
            }
        }
        reader.close();
    } catch (Exception e) {
        e.printStackTrace();
        return;
    }
    // If we couldn't find it, fucking die
    if (id == null) {
        Log.e(TAG, "Song not found");
        throw new IllegalArgumentException("song not found");
    }
    Log.i(TAG, "Loading sound");
    // Stop any song in progress
    stop();
    // Set some properties in case the javascript forgot to set them
    P.set("is_playing", "true");
    P.set("current_song", song);
    if (player != null) {
        player.stop();
    }
    LoadControl loadControl = new DefaultLoadControl();
    player = ExoPlayerFactory.newSimpleInstance(getContext(), trackSelector, loadControl);
    player.addListener(new SongDoneListener());
    DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(getContext(), Util.getUserAgent(getContext(), "la/u/ncher"), bandwidthMeter);
    ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();
    MediaSource mediaSource = new ExtractorMediaSource(Uri.parse("https://niles.xyz/valhalla_music/" + id + ".mp3"), dataSourceFactory, extractorsFactory, null, null);
    player.setPlayWhenReady(true);
    player.prepare(mediaSource);
    if (reload) {
        NotifierService.notify(NotifierService.NotificationType.RELOAD_MUSIC);
    }
}
Also used : DefaultExtractorsFactory(com.google.android.exoplayer2.extractor.DefaultExtractorsFactory) DefaultLoadControl(com.google.android.exoplayer2.DefaultLoadControl) LoadControl(com.google.android.exoplayer2.LoadControl) ExtractorMediaSource(com.google.android.exoplayer2.source.ExtractorMediaSource) ExoPlaybackException(com.google.android.exoplayer2.ExoPlaybackException) DefaultLoadControl(com.google.android.exoplayer2.DefaultLoadControl) DataSource(com.google.android.exoplayer2.upstream.DataSource) ExtractorMediaSource(com.google.android.exoplayer2.source.ExtractorMediaSource) MediaSource(com.google.android.exoplayer2.source.MediaSource) ExtractorsFactory(com.google.android.exoplayer2.extractor.ExtractorsFactory) DefaultExtractorsFactory(com.google.android.exoplayer2.extractor.DefaultExtractorsFactory) DefaultDataSourceFactory(com.google.android.exoplayer2.upstream.DefaultDataSourceFactory) StringReader(java.io.StringReader) JsonReader(android.util.JsonReader)

Example 15 with DefaultExtractorsFactory

use of androidx.media3.extractor.DefaultExtractorsFactory in project react-native-track-player by react-native-kit.

the class Track method toMediaSource.

public MediaSource toMediaSource(Context ctx, LocalPlayback playback) {
    // Updates the user agent if not set
    if (userAgent == null || userAgent.isEmpty())
        userAgent = Util.getUserAgent(ctx, "react-native-track-player");
    DataSource.Factory ds;
    if (resourceId != 0) {
        try {
            RawResourceDataSource raw = new RawResourceDataSource(ctx);
            raw.open(new DataSpec(uri));
            ds = new DataSource.Factory() {

                @Override
                public DataSource createDataSource() {
                    return raw;
                }
            };
        } catch (IOException ex) {
            // Should never happen
            throw new RuntimeException(ex);
        }
    } else if (Utils.isLocal(uri)) {
        // Creates a local source factory
        ds = new DefaultDataSourceFactory(ctx, userAgent);
    } else {
        // Creates a default http source factory, enabling cross protocol redirects
        DefaultHttpDataSourceFactory factory = new DefaultHttpDataSourceFactory(userAgent, null, DefaultHttpDataSource.DEFAULT_CONNECT_TIMEOUT_MILLIS, DefaultHttpDataSource.DEFAULT_READ_TIMEOUT_MILLIS, true);
        if (headers != null) {
            factory.getDefaultRequestProperties().set(headers);
        }
        ds = playback.enableCaching(factory);
    }
    switch(type) {
        case DASH:
            return createDashSource(ds);
        case HLS:
            return createHlsSource(ds);
        case SMOOTH_STREAMING:
            return createSsSource(ds);
        default:
            return new ProgressiveMediaSource.Factory(ds, new DefaultExtractorsFactory().setConstantBitrateSeekingEnabled(true)).createMediaSource(uri);
    }
}
Also used : DefaultExtractorsFactory(com.google.android.exoplayer2.extractor.DefaultExtractorsFactory) ProgressiveMediaSource(com.google.android.exoplayer2.source.ProgressiveMediaSource) IOException(java.io.IOException)

Aggregations

DefaultExtractorsFactory (com.google.android.exoplayer2.extractor.DefaultExtractorsFactory)21 MediaSource (com.google.android.exoplayer2.source.MediaSource)13 ExtractorMediaSource (com.google.android.exoplayer2.source.ExtractorMediaSource)12 DefaultDataSourceFactory (com.google.android.exoplayer2.upstream.DefaultDataSourceFactory)12 ExtractorsFactory (com.google.android.exoplayer2.extractor.ExtractorsFactory)9 DefaultRenderersFactory (com.google.android.exoplayer2.DefaultRenderersFactory)8 DefaultTrackSelector (com.google.android.exoplayer2.trackselection.DefaultTrackSelector)8 DataSource (com.google.android.exoplayer2.upstream.DataSource)8 DefaultLoadControl (com.google.android.exoplayer2.DefaultLoadControl)7 Uri (android.net.Uri)6 ExoPlayerFactory (com.google.android.exoplayer2.ExoPlayerFactory)6 HlsMediaSource (com.google.android.exoplayer2.source.hls.HlsMediaSource)5 AudioAttributes (com.google.android.exoplayer2.audio.AudioAttributes)4 DashMediaSource (com.google.android.exoplayer2.source.dash.DashMediaSource)4 SsMediaSource (com.google.android.exoplayer2.source.smoothstreaming.SsMediaSource)4 HashMap (java.util.HashMap)4 SuppressLint (android.annotation.SuppressLint)3 ProgressiveMediaSource (com.google.android.exoplayer2.source.ProgressiveMediaSource)3 DefaultHttpDataSource (com.google.android.exoplayer2.upstream.DefaultHttpDataSource)3 DefaultHttpDataSourceFactory (com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory)3