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();
}
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);
}
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();
}
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);
}
}
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);
}
}
Aggregations