我正在尝试播放包含实时流的URL链接,但是我已经使用用户代理创建了它们,因此您只能从我的应用程序观看流,我正在将exoplayer与playerview一起使用,但无法管理与自定义用户代理的链接。如果有人之前做过此事,请给我一些建议,如何做,以及如何在exoplayer上与用户代理播放实时流链接。 这是我的代码:
public class ActivityStreamPlayer extends AppCompatActivity {
private static final String TAG = "ActivityStreamPlayer";
String url;
String str_category, str_id, str_image, str_name, str_url, str_description, str_channel_type, str_video_id;
public String channel_name;
private PlayerView playerView;
private SimpleExoPlayer player;
private static final DefaultBandwidthMeter BANDWIDTH_METER = new DefaultBandwidthMeter();
private DataSource.Factory mediaDataSourceFactory;
private Handler mainHandler;
private List<Channel> items = new ArrayList<>();
private ProgressBar progressBar;
TextView textView;
TextView channeli;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_streaming);
url = getIntent().getStringExtra("url");
Intent intent = getIntent();
if (null != intent) {
str_id = intent.getStringExtra(Constant.KEY_CHANNEL_ID);
str_name = intent.getStringExtra(Constant.KEY_CHANNEL_NAME);
str_image = intent.getStringExtra(Constant.KEY_CHANNEL_IMAGE);
str_url = intent.getStringExtra(Constant.KEY_CHANNEL_URL);
str_description = intent.getStringExtra(Constant.KEY_CHANNEL_DESCRIPTION);
str_channel_type = intent.getStringExtra(Constant.KEY_CHANNEL_TYPE);
str_video_id = intent.getStringExtra(Constant.KEY_VIDEO_ID);
}
textView = findViewById(R.id.infostream);
channeli = findViewById(R.id.kanalistream);
channeli.setText(str_name);
mediaDataSourceFactory = buildDataSourceFactory(true);
mainHandler = new Handler();
BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
RenderersFactory renderersFactory = new DefaultRenderersFactory(this);
TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory(bandwidthMeter);
TrackSelector trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);
LoadControl loadControl = new DefaultLoadControl();
player = ExoPlayerFactory.newSimpleInstance(renderersFactory, trackSelector, loadControl);
playerView = findViewById(R.id.exoPlayerView);
playerView.setPlayer(player);
playerView.setUseController(true);
playerView.requestFocus();
Uri uri = Uri.parse(url);
MediaSource mediaSource = buildMediaSource(uri, null);
player.prepare(mediaSource);
player.setPlayWhenReady(true);
player.addListener(new Player.EventListener() {
@Override
public void onTimelineChanged(Timeline timeline, @Nullable Object manifest, int reason) {
Log.d(TAG, "onTimelineChanged: ");
}
@Override
public void onTracksChanged(TrackGroupArray trackGroups, TrackSelectionArray trackSelections) {
Log.d(TAG, "onTracksChanged: " + trackGroups.length);
}
@Override
public void onLoadingChanged(boolean isLoading) {
Log.d(TAG, "onLoadingChanged: " + isLoading);
}
@Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
Log.d(TAG, "onPlayerStateChanged: " + playWhenReady);
if (playbackState == PlaybackStateCompat.STATE_PLAYING) {
textView.setVisibility(View.GONE);
channeli.setVisibility(View.GONE);
}
}
@Override
public void onRepeatModeChanged(int repeatMode) {
}
@Override
public void onShuffleModeEnabledChanged(boolean shuffleModeEnabled) {
}
@Override
public void onPlayerError(ExoPlaybackException error) {
Log.e(TAG, "onPlayerError: ", error);
player.stop();
errorDialog();
}
@Override
public void onPositionDiscontinuity(int reason) {
Log.d(TAG, "onPositionDiscontinuity: true");
}
@Override
public void onPlaybackParametersChanged(PlaybackParameters playbackParameters) {
}
@Override
public void onSeekProcessed() {
}
});
Log.d("INFO", "ActivityVideoPlayer");
}
private MediaSource buildMediaSource(Uri uri, String overrideExtension) {
int type = TextUtils.isEmpty(overrideExtension) ? Util.inferContentType(uri)
: Util.inferContentType("." + overrideExtension);
switch (type) {
case C.TYPE_SS:
return new SsMediaSource.Factory(new DefaultSsChunkSource.Factory(mediaDataSourceFactory), buildDataSourceFactory(false)).createMediaSource(uri);
case C.TYPE_DASH:
return new DashMediaSource.Factory(new DefaultDashChunkSource.Factory(mediaDataSourceFactory), buildDataSourceFactory(false)).createMediaSource(uri);
case C.TYPE_HLS:
return new HlsMediaSource.Factory(mediaDataSourceFactory).createMediaSource(uri);
case C.TYPE_OTHER:
return new ExtractorMediaSource.Factory(mediaDataSourceFactory).createMediaSource(uri);
default: {
throw new IllegalStateException("Unsupported type: " + type);
}
}
}
private DataSource.Factory buildDataSourceFactory(boolean useBandwidthMeter) {
return buildDataSourceFactory(useBandwidthMeter ? BANDWIDTH_METER : null);
}
public DataSource.Factory buildDataSourceFactory(DefaultBandwidthMeter bandwidthMeter) {
return new DefaultDataSourceFactory(this, bandwidthMeter,
buildHttpDataSourceFactory(bandwidthMeter));
}
public HttpDataSource.Factory buildHttpDataSourceFactory(DefaultBandwidthMeter bandwidthMeter) {
return new DefaultHttpDataSourceFactory(Util.getUserAgent(this, "ExoPlayerDemo"), bandwidthMeter);
}
@Override
public void onBackPressed() {
super.onBackPressed();
player.stop();
}
public void errorDialog() {
new AlertDialog.Builder(this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle(getResources().getString(R.string.msg_oops))
.setCancelable(false)
.setMessage(getResources().getString(R.string.msg_failed))
.setPositiveButton(getResources().getString(R.string.option_retry), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
retryLoad();
}
})
.setNegativeButton(getResources().getString(R.string.option_no), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
finish();
}
})
.show();
}
public void retryLoad() {
Uri uri = Uri.parse(url);
MediaSource mediaSource = buildMediaSource(uri, null);
player.prepare(mediaSource);
player.setPlayWhenReady(true);
}
}