我是初学者的android。谁能帮我简要介绍一下这个问题。我动态地获得两个链接
如果我在第一个(url)上收到错误,我想播放第二个媒体(备用)源。 我正在寻找一个星期,一个很好的答案将不胜感激。如果你无法解决,请提升它以进入更多的眼睛..在此先感谢。
这是我的完整代码
ImageButton playPause,lockScreen,unLock;
SeekBar seekBar;
TextView start,end;
boolean isPlaying = false;
boolean isLock = false;
RelativeLayout controller;
FrameLayout root;
boolean isControllerShowing = true;
private Handler mHandler;
SimpleExoPlayerView simpleExoPlayerView;
private SimpleExoPlayer player;
SessionManagement management;
String url, alternate;
Uri mp4VideoUri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_live_tv);
checkConnection();
Sensey.getInstance().init(this);
Sensey.getInstance().startOrientationDetection(15,this);
management = new SessionManagement(this);
HashMap<String,String> hashMap = management.getUserDetails();
url = hashMap.get(SessionManagement.LIVE_URL);
alternate = hashMap.get(SessionManagement.ALTERNATE_URL);
playPause = findViewById(R.id.btnPlayPause);
lockScreen = findViewById(R.id.btnLock);
unLock = findViewById(R.id.btnOpenLock);
seekBar = findViewById(R.id.seeker);
end = findViewById(R.id.duration);
start = findViewById(R.id.position);
controller = findViewById(R.id.layout_controller);
root = findViewById(R.id.activity_live);
try {
BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory(bandwidthMeter);
TrackSelector trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);
LoadControl loadControl = new DefaultLoadControl();
player = ExoPlayerFactory.newSimpleInstance(this, trackSelector, loadControl);
simpleExoPlayerView = findViewById(R.id.video_view);
simpleExoPlayerView.setUseController(false);
simpleExoPlayerView.requestFocus();
simpleExoPlayerView.setPlayer(player);
mp4VideoUri = Uri.parse(url); //Radnom 540p indian channel
mHandler = new Handler();
DefaultBandwidthMeter bandwidthMeterA = new DefaultBandwidthMeter();
DefaultDataSourceFactory dataSourceFactory = new
DefaultDataSourceFactory(this, Util.getUserAgent(this, "Madha TV"), bandwidthMeterA);
DefaultExtractorsFactory();
dataSourceFactory, extractorsFactory, null, null);
MediaSource videoSource = new HlsMediaSource(mp4VideoUri, dataSourceFactory, 1, null, null);
final LoopingMediaSource loopingSource = new LoopingMediaSource(videoSource);
player.prepare(loopingSource);
player.addListener(new ExoPlayer.EventListener() {
@Override
public void onTimelineChanged(Timeline timeline, Object manifest, int reason) {
}
@Override
public void onTracksChanged(TrackGroupArray trackGroups, TrackSelectionArray trackSelections) {
}
@Override
public void onLoadingChanged(boolean isLoading) {
}
@Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
}
@Override
public void onRepeatModeChanged(int repeatMode) {
}
@Override
public void onShuffleModeEnabledChanged(boolean shuffleModeEnabled) {
}
@Override
public void onPlayerError(ExoPlaybackException error) {
switch (error.type) {
case ExoPlaybackException.TYPE_SOURCE:
Log.e("Error", "TYPE_SOURCE: " + error.getSourceException().getMessage());
Intent intent1 = new Intent(LiveTVActivity.this, MainActivity.class);
startActivity(intent1);
finish();
Toast.makeText(LiveTVActivity.this, error.getSourceException().getMessage(), Toast.LENGTH_SHORT).show();
break;
case ExoPlaybackException.TYPE_RENDERER:
break;
case ExoPlaybackException.TYPE_UNEXPECTED:
Log.e("Error", "TYPE_UNEXPECTED: " + error.getUnexpectedException().getMessage());
break;
}
player.stop();
player.prepare(loopingSource);
player.setPlayWhenReady(true);
}
@Override
public void onPositionDiscontinuity(int reason) {
}
@Override
public void onPlaybackParametersChanged(PlaybackParameters playbackParameters) {
}
@Override
public void onSeekProcessed() {
}
});
player.setPlayWhenReady(true); //run file/link when ready to play.
player.setVideoDebugListener(this); //for listening to resolution change and outputing the resolution
if (player.getPlayWhenReady())
{
playPause.setImageResource(R.drawable.cvp_action_pause);
isPlaying = true;
}
else
{
playPause.setImageResource(R.drawable.cvp_action_play);
isPlaying = false;
}
root.setOnClickListener(this);
playPause.setOnClickListener(this);
lockScreen.setOnClickListener(this);
unLock.setOnClickListener(this);
}
catch (Exception e)
{
Log.e("Error",e.toString());
}
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);
}
updateMetadata();
} else {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
getWindow().clearFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
}
updateMetadata();
}
}
@Override
public void onTopSideUp() {
updateMetadata();
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
@Override
public void onBottomSideUp() {
}
@Override
public void onRightSideUp() {
updateMetadata();
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
@Override
public void onLeftSideUp() {
updateMetadata();
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
}
@SuppressWarnings("ConstantConditions")
private void updateMetadata() {
Point displaySize;
displaySize = Utils.getDisplaySize(this);
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(displaySize.x,displaySize.y);
lp.gravity = Gravity.CENTER;
simpleExoPlayerView.setLayoutParams(lp);
simpleExoPlayerView.invalidate();
}
private void checkConnection()
{
boolean isConnected = ConnectivityReceiver.isConnected();
showSnack(isConnected);
}
// Showing the status in Snackbar
private void showSnack(boolean isConnected) {
if (!isConnected) {
Config.noConnection(LiveTVActivity.this);
}
}
@Override
public void onClick(View view) {
if (view.getId() == R.id.btnPlayPause) {
setPlayPause();
}else if (view.getId() == R.id.btnLock) {
if (!isLock)
{
unLock.setVisibility(View.VISIBLE);
isLock = true;
controller.setVisibility(View.GONE);
root.setClickable(false);
}
}else if (view.getId() == R.id.btnOpenLock)
{
if (isLock)
{
root.setClickable(true);
isLock = false;
unLock.setVisibility(View.GONE);
controller.setVisibility(View.VISIBLE);
}
} else if (view.getId() == R.id.activity_live)
{
showController();
}
}
public void showController()
{
if (!isControllerShowing)
{
isControllerShowing = true;
controller.setVisibility(View.VISIBLE);
}
else
{
isControllerShowing = false;
controller.setVisibility(View.GONE);
}
}
private void setPlayPause() {
if (isPlaying)
{
player.setPlayWhenReady(false);
isPlaying = false;
playPause.setImageResource(R.drawable.cvp_action_play);
}
else
{
player.setPlayWhenReady(true);
isPlaying = true;
playPause.setImageResource(R.drawable.cvp_action_pause);
}
}
@Override
public void onNetworkConnectionChanged(boolean isConnected) {
showSnack(isConnected);
}
@Override
public void onVideoEnabled(DecoderCounters counters) {
}
@Override
public void onVideoDecoderInitialized(String decoderName, long initializedTimestampMs, long initializationDurationMs) {
}
@Override
public void onVideoInputFormatChanged(Format format) {
}
@Override
public void onDroppedFrames(int count, long elapsedMs) {
}
@Override
public void onVideoSizeChanged(int width, int height, int unappliedRotationDegrees, float pixelWidthHeightRatio) {
}
@Override
public void onRenderedFirstFrame(Surface surface) {
}
@Override
public void onVideoDisabled(DecoderCounters counters) {
}
答案 0 :(得分:0)
试试这段代码..
IEnumerable<KeyValuePair<int, int>> combined = d1.Concat(d2.Where(x => !d1.Keys.Contains(x.Key)));
Dictionary<int, int> result = combined.ToDictionary(x => x.Key, x => x.Value);
答案 1 :(得分:0)
我也遇到了同样的问题。你必须对你的代码做一些改变 1.实现外部播放器听取活动,以便普通列表将使用播放器的每个瞬间
然后你必须在方法中添加所有播放器初始化代码并在外部播放器的错误状态下调用它 的 initializePlayer(); 强>
private void initializePlayer(String video_url){
尝试{ videoUri = Uri.parse(VIDEOS_URL + contentMasterModel.getImage_or_video());
simpleExoPlayerView.requestFocus();
TrackSelection.Factory videoTrackSelectionFactory =
new AdaptiveTrackSelection.Factory(BANDWIDTH_METER);
trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);
player = ExoPlayerFactory.newSimpleInstance(this, trackSelector);
simpleExoPlayerView.setPlayer(player);
//player.setPlayWhenReady(shouldAutoPlay);
player.addListener(this);
DefaultExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();
MediaSource mediaSource = new ExtractorMediaSource(videoUri,
mediaDataSourceFactory, extractorsFactory, null, null);
player.prepare(mediaSource);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
// Call some material design APIs here
// player.setPlayWhenReady(true);
}
inErrorState = false;
} catch (Exception e) {
e.printStackTrace();
Log.e("Please try again", "download again");
}
}
OnError
private void errorinitializePlayer() {
videoUri = Uri.parse(VIDEOS_URL);
simpleExoPlayerView.requestFocus();
TrackSelection.Factory videoTrackSelectionFactory =
new AdaptiveTrackSelection.Factory(BANDWIDTH_METER);
trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);
player = ExoPlayerFactory.newSimpleInstance(this, trackSelector);
simpleExoPlayerView.setPlayer(player);
//player.setPlayWhenReady(shouldAutoPlay);
player.addListener(this);
DefaultExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();
MediaSource mediaSource = new ExtractorMediaSource(videoUri,
mediaDataSourceFactory, extractorsFactory, null, null);
player.prepare(mediaSource);
inErrorState = false;}
并在活动中实现EventListener,PlaybackControlView.VisibilityListener