本文整理汇总了Java中com.google.android.exoplayer2.upstream.DataSource类的典型用法代码示例。如果您正苦于以下问题:Java DataSource类的具体用法?Java DataSource怎么用?Java DataSource使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
DataSource类属于com.google.android.exoplayer2.upstream包,在下文中一共展示了DataSource类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: onCreate
import com.google.android.exoplayer2.upstream.DataSource; //导入依赖的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();
}
示例2: EncryptedFileDataSourceFactory
import com.google.android.exoplayer2.upstream.DataSource; //导入依赖的package包/类
/**
* Instantiates a new Encrypted file data source factory.
*
* @param context the context
* @param cipher the cipher
* @param secretKeySpec the secret key spec
* @param ivParameterSpec the iv parameter spec
* @param listener the listener
*/
public EncryptedFileDataSourceFactory(Context context, Cipher cipher, SecretKeySpec secretKeySpec, IvParameterSpec ivParameterSpec, TransferListener<? super DataSource> listener) {
mCipher = cipher;
mSecretKeySpec = secretKeySpec;
mIvParameterSpec = ivParameterSpec;
mTransferListener = listener;
String userAgent = Util.getUserAgent(context, context.getPackageName());
this.context = context.getApplicationContext();
this.baseDataSourceFactory = new DefaultDataSourceFactory(context, userAgent);
}
示例3: doInBackground
import com.google.android.exoplayer2.upstream.DataSource; //导入依赖的package包/类
@Override
protected List<SampleGroup> doInBackground(String... uris) {
List<SampleGroup> result = new ArrayList<>();
Context context = getApplicationContext();
String userAgent = Util.getUserAgent(context, "ExoPlayerDemo");
DataSource dataSource = new DefaultDataSource(context, null, userAgent, false);
for (String uri : uris) {
DataSpec dataSpec = new DataSpec(Uri.parse(uri));
InputStream inputStream = new DataSourceInputStream(dataSource, dataSpec);
try {
readSampleGroups(new JsonReader(new InputStreamReader(inputStream, "UTF-8")), result);
} catch (Exception e) {
Log.e(TAG, "Error loading sample list: " + uri, e);
sawError = true;
} finally {
Util.closeQuietly(dataSource);
}
}
return result;
}
示例4: CacheDataSource
import com.google.android.exoplayer2.upstream.DataSource; //导入依赖的package包/类
/**
* Constructs an instance with arbitrary {@link DataSource} and {@link DataSink} instances for
* reading and writing the cache. One use of this constructor is to allow data to be transformed
* before it is written to disk.
*
* @param cache The cache.
* @param upstream A {@link DataSource} for reading data not in the cache.
* @param cacheReadDataSource A {@link DataSource} for reading data from the cache.
* @param cacheWriteDataSink A {@link DataSink} for writing data to the cache. If null, cache is
* accessed read-only.
* @param flags A combination of {@link #FLAG_BLOCK_ON_CACHE}, {@link #FLAG_IGNORE_CACHE_ON_ERROR}
* and {@link #FLAG_IGNORE_CACHE_FOR_UNSET_LENGTH_REQUESTS}, or 0.
* @param eventListener An optional {@link EventListener} to receive events.
*/
public CacheDataSource(Cache cache, DataSource upstream, DataSource cacheReadDataSource,
DataSink cacheWriteDataSink, @Flags int flags, @Nullable EventListener eventListener) {
this.cache = cache;
this.cacheReadDataSource = cacheReadDataSource;
this.blockOnCache = (flags & FLAG_BLOCK_ON_CACHE) != 0;
this.ignoreCacheOnError = (flags & FLAG_IGNORE_CACHE_ON_ERROR) != 0;
this.ignoreCacheForUnsetLengthRequests =
(flags & FLAG_IGNORE_CACHE_FOR_UNSET_LENGTH_REQUESTS) != 0;
this.upstreamDataSource = upstream;
if (cacheWriteDataSink != null) {
this.cacheWriteDataSource = new TeeDataSource(upstream, cacheWriteDataSink);
} else {
this.cacheWriteDataSource = null;
}
this.eventListener = eventListener;
}
示例5: buildMediaSource
import com.google.android.exoplayer2.upstream.DataSource; //导入依赖的package包/类
private MediaSource buildMediaSource(Uri uri, String overrideExtension) {
DataSource.Factory mediaDataSourceFactory = buildDataSourceFactory(true);
int type = TextUtils.isEmpty(overrideExtension) ? Util.inferContentType(uri)
: Util.inferContentType("." + overrideExtension);
switch (type) {
case C.TYPE_SS:
return new SsMediaSource(uri, buildDataSourceFactory(false),
new DefaultSsChunkSource.Factory(mediaDataSourceFactory), mainHandler, null);
case C.TYPE_DASH:
return new DashMediaSource(uri, buildDataSourceFactory(false),
new DefaultDashChunkSource.Factory(mediaDataSourceFactory), mainHandler, null);
case C.TYPE_HLS:
return new HlsMediaSource(uri, mediaDataSourceFactory, mainHandler, null);
case C.TYPE_OTHER:
return new ExtractorMediaSource(uri, mediaDataSourceFactory, new DefaultExtractorsFactory(),
mainHandler, null);
default: {
throw new IllegalStateException("Unsupported type: " + type);
}
}
}
示例6: getDataSourceFactory
import com.google.android.exoplayer2.upstream.DataSource; //导入依赖的package包/类
@Override
public DataSource.Factory getDataSourceFactory() {
//采用默认
return new DefaultCacheDataSourceFactory(context,100000000,"1234567887654321".getBytes(),eventListener);
//自定义配置
/* LeastRecentlyUsedCacheEvictor evictor = new LeastRecentlyUsedCacheEvictor(100000000);
SimpleCache simpleCache = new SimpleCache
//设置你缓存目录
(new File(context.getExternalCacheDir(), "media"),
//缓存驱逐器
evictor,
// 缓存文件加密,那么在使用AES / CBC的文件系统中缓存密钥将被加密 密钥必须是16字节长
"1234567887654321".getBytes());
//使用缓存数据源工厂类
return new DefaultCacheDataSourceFactory(simpleCache,
//设置下载数据加载工厂类
new JDefaultDataSourceFactory(context),
//设置缓存标记
0
//最大缓存文件大小,不填写 默认2m
4 * 1024 * 1024
//设置缓存监听事件
);*/
//或者 如果需要监听事件
//使用缓存数据源工厂类
/* return new CacheDataSourceFactory(simpleCache,
//设置下载数据加载工厂类
new JDefaultDataSourceFactory(context),
//缓存读取数据源工厂
new FileDataSourceFactory(),
//缓存数据接收器的工厂
new CacheDataSinkFactory(simpleCache, CacheDataSource.DEFAULT_MAX_CACHE_FILE_SIZE),
//设置缓存标记
0,
//设置缓存监听事件
eventListener);*/
}
示例7: createDataSource
import com.google.android.exoplayer2.upstream.DataSource; //导入依赖的package包/类
@Override
public DataSource createDataSource() {
if (null != mCipher) {
return new MyDefaultDataSource(context, mCipher, mSecretKeySpec, mIvParameterSpec, mTransferListener, baseDataSourceFactory.createDataSource());
} else if (key != null) {
return new Encrypted1FileDataSource(key, new DefaultBandwidthMeter());
} else {
return new DefaultDataSource(context, new DefaultBandwidthMeter(), baseDataSourceFactory.createDataSource());
}
}
示例8: closeQuietly
import com.google.android.exoplayer2.upstream.DataSource; //导入依赖的package包/类
/**
* Closes a {@link DataSource}, suppressing any {@link IOException} that may occur.
*
* @param dataSource The {@link DataSource} to close.
*/
public static void closeQuietly(DataSource dataSource) {
try {
if (dataSource != null) {
dataSource.close();
}
} catch (IOException e) {
// Ignore.
}
}
示例9: getDataSource
import com.google.android.exoplayer2.upstream.DataSource; //导入依赖的package包/类
/***
* 初始化数据源工厂
* @return DataSource.Factory data source
*/
public DataSource.Factory getDataSource() {
if (listener != null) {
return listener.getDataSourceFactory();
} else {
return new JDefaultDataSourceFactory(context);
}
}
示例10: onCreate
import com.google.android.exoplayer2.upstream.DataSource; //导入依赖的package包/类
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (!isPlaying) {
Handler mainHandler = new Handler();
BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
TrackSelection.Factory videoTrackSelectionFactory =
new AdaptiveVideoTrackSelection.Factory(bandwidthMeter);
TrackSelector trackSelector =
new DefaultTrackSelector(videoTrackSelectionFactory);
LoadControl loadControl = new DefaultLoadControl();
SimpleExoPlayer player =
ExoPlayerFactory.newSimpleInstance(MainActivity.this, trackSelector, loadControl);
SimpleExoPlayerView playerView = (SimpleExoPlayerView) findViewById(R.id.videoView);
playerView.setPlayer(player);
DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(MainActivity.this,
Util.getUserAgent(MainActivity.this, "yourApplicationName"));
MediaSource mediaSource = new ExtractorMediaSource(Uri.parse("https://r7---sn-3c27ln7k.googlevideo.com/videoplayback?id=6fb497d0971b8cdf&itag=22&source=picasa&begin=0&requiressl=yes&mm=30&mn=sn-3c27ln7k&ms=nxu&mv=m&nh=IgphcjAzLmticDAxKgkxMjcuMC4wLjE&pl=22&sc=yes&mime=video/mp4&lmt=1486083166327499&mt=1486135406&ip=134.249.158.189&ipbits=8&expire=1486164239&sparams=ip,ipbits,expire,id,itag,source,requiressl,mm,mn,ms,mv,nh,pl,sc,mime,lmt&signature=3BB06D8D4294F8C49B3CE910B3D6849954302BB1.02ABE00700DFCEF715E72D0EFB73B67841E659F8&key=ck2&ratebypass=yes&title=%5BAnime365%5D%20Kuzu%20no%20Honkai%20-%2004%20(t1174045)"), dataSourceFactory,
new DefaultExtractorsFactory(), null, null);
player.prepare(mediaSource);
player.setPlayWhenReady(true);
}
}
示例11: buildSource
import com.google.android.exoplayer2.upstream.DataSource; //导入依赖的package包/类
@Override
protected MediaSource buildSource(HostActivity host, String userAgent,
TransferListener<? super DataSource> mediaTransferListener) {
DataSource.Factory manifestDataSourceFactory = new DefaultDataSourceFactory(host, userAgent);
DataSource.Factory mediaDataSourceFactory = new DefaultDataSourceFactory(host, userAgent,
mediaTransferListener);
Uri manifestUri = Uri.parse(parameters.manifestUrl);
DefaultDashChunkSource.Factory chunkSourceFactory = new DefaultDashChunkSource.Factory(
mediaDataSourceFactory);
return new DashMediaSource(manifestUri, manifestDataSourceFactory, chunkSourceFactory,
MIN_LOADABLE_RETRY_COUNT, 0 /* livePresentationDelayMs */, null, null);
}
示例12: readAndDiscard
import com.google.android.exoplayer2.upstream.DataSource; //导入依赖的package包/类
/**
* Reads and discards all data specified by the {@code dataSpec}.
*
* @param dataSpec Defines the data to be read.
* @param dataSource The {@link DataSource} to read the data from.
* @param buffer The buffer to be used while downloading.
* @param priorityTaskManager If not null it's used to check whether it is allowed to proceed with
* caching.
* @param priority The priority of this task.
* @return Number of read bytes, or 0 if no data is available because the end of the opened range
* has been reached.
*/
private static long readAndDiscard(DataSpec dataSpec, DataSource dataSource, byte[] buffer,
PriorityTaskManager priorityTaskManager, int priority)
throws IOException, InterruptedException {
while (true) {
if (priorityTaskManager != null) {
// Wait for any other thread with higher priority to finish its job.
priorityTaskManager.proceed(priority);
}
try {
dataSource.open(dataSpec);
long totalRead = 0;
while (true) {
if (Thread.interrupted()) {
throw new InterruptedException();
}
int read = dataSource.read(buffer, 0, buffer.length);
if (read == C.RESULT_END_OF_INPUT) {
return totalRead;
}
totalRead += read;
}
} catch (PriorityTaskManager.PriorityTooLowException exception) {
// catch and try again
} finally {
Util.closeQuietly(dataSource);
}
}
}
示例13: startPlayer
import com.google.android.exoplayer2.upstream.DataSource; //导入依赖的package包/类
/**
* 播放启动视频
*/
private void startPlayer() {
// 0. set player view
playerView = (SimpleExoPlayerView) findViewById(R.id.playerView);
playerView.setUseController(false);
playerView.getKeepScreenOn();
playerView.setResizeMode(RESIZE_MODE_FILL);
// 1. Create a default TrackSelector
TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory(new DefaultBandwidthMeter());
trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);
// 2. Create a default LoadControl
loadControl = new DefaultLoadControl();
// 3. Create the mPlayer
mPlayer = ExoPlayerFactory.newSimpleInstance(this, trackSelector, loadControl);
mPlayer.addListener(this);
// 4. set player
playerView.setPlayer(mPlayer);
mPlayer.setPlayWhenReady(true);
// 5. prepare to play
File file = new File(Constants.FILE_VIDEO_FLODER, "jcode.mp4");
if (file.isFile() && file.exists()) {
mUri = Uri.fromFile(file);
} else {
Toast.makeText(this,"文件未找到",Toast.LENGTH_SHORT).show();
return;
}
ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();
DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(this, "UserAgent");
videoSource = new ExtractorMediaSource(mUri, dataSourceFactory, extractorsFactory, null, null);
// 6. ready to play
mPlayer.prepare(videoSource);
}
示例14: buildMediaSource
import com.google.android.exoplayer2.upstream.DataSource; //导入依赖的package包/类
private MediaSource buildMediaSource(Uri uri) {
DataSource.Factory manifestDataSourceFactory = new DefaultHttpDataSourceFactory("ua");
DashChunkSource.Factory dashChunkSourceFactory = new DefaultDashChunkSource.Factory(
new DefaultHttpDataSourceFactory("ua", BANDWIDTH_METER));
return new DashMediaSource.Factory(dashChunkSourceFactory, manifestDataSourceFactory)
.createMediaSource(uri);
}
示例15: buildMediaSource
import com.google.android.exoplayer2.upstream.DataSource; //导入依赖的package包/类
private MediaSource buildMediaSource(Uri uri) {
DashChunkSource.Factory dashChunkSourceFactory = new DefaultDashChunkSource.Factory(
new DefaultHttpDataSourceFactory("ua", BANDWIDTH_METER));
DataSource.Factory manifestDataSourceFactory = new DefaultHttpDataSourceFactory("ua");
return new DashMediaSource.Factory(dashChunkSourceFactory, manifestDataSourceFactory).
createMediaSource(uri);
}