当我们旋转包含Exoplayer的Relativelayout时,视频不会播放

时间:2017-10-31 04:53:44

标签: android android-layout video exoplayer

旋转包含Exoplayer的Relativelayout

时,

视频无法播放

我有一个包含Exoplayer的活动。如果我们将视频放在相对布局中,它可以正常工作,但是当我们旋转相对布局时,它不会播放视频,如何做到这一点

这是活动的代码

public class ExoplayerAct extends Activity implements VideoRendererEventListener {


    private static final String TAG = "MainActivity";
    private SimpleExoPlayerView simpleExoPlayerView;
    private SimpleExoPlayer player;
    private TextView resolutionTextView;
    String j;
    RelativeLayout mainlayoutexo;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.exoplayer);
        resolutionTextView = new TextView(this);
        //resolutionTextView = (TextView) findViewById(R.id.resolution_textView);

        Intent iin = getIntent();
        Bundle b = iin.getExtras();
        if (b != null) {
            j = (String) b.get("fileVideoPath");
        }

        mainlayoutexo   = (RelativeLayout) findViewById(R.id.mainlayoutexo);
        mainlayoutexo.setRotation(90);

// 1. Create a default TrackSelector
        BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
        TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory(bandwidthMeter);
        TrackSelector trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);

// 2. Create a default LoadControl
        LoadControl loadControl = new DefaultLoadControl();

// 3. Create the player.newSimpleInstance(RenderersFactory, TrackSelector, LoadControl).
        player = ExoPlayerFactory.newSimpleInstance(this, trackSelector, loadControl);
        simpleExoPlayerView = new SimpleExoPlayerView(this);
        simpleExoPlayerView = (SimpleExoPlayerView) findViewById(R.id.player_view);

//Set media controller
        simpleExoPlayerView.setUseController(true);

        simpleExoPlayerView.requestFocus();


// Bind the player to the view.
        simpleExoPlayerView.setPlayer(player);
        Uri mp4VideoUri = Uri.parse(j);





//Measures bandwidth during playback. Can be null if not required.
        DefaultBandwidthMeter bandwidthMeterA = new DefaultBandwidthMeter();
//Produces DataSource instances through which media data is loaded.
        DefaultDataSourceFactory dataSourceFactory = new DefaultDataSourceFactory(this
                , Util.getUserAgent(this, "exoplayer2example"), bandwidthMeterA);
//Produces Extractor instances for parsing the media data.
        ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();


// II. ADJUST HERE:

//This is the MediaSource representing the media to be played:
//FOR SD CARD SOURCE:
        MediaSource videoSource = new ExtractorMediaSource(mp4VideoUri, dataSourceFactory, extractorsFactory, null, null);

//FOR LIVESTREAM LINK:
//        MediaSource videoSource = new HlsMediaSource(mp4VideoUri, dataSourceFactory, 1, null, null);
        final LoopingMediaSource loopingSource = new LoopingMediaSource(videoSource);

// Prepare the player with the source.
        player.prepare(loopingSource);

        player.addListener(new ExoPlayer.EventListener() {
            @Override
            public void onTimelineChanged(Timeline timeline, Object manifest) {
                Log.v(TAG, "Listener-onTimelineChanged...");
            }

            @Override
            public void onTracksChanged(TrackGroupArray trackGroups, TrackSelectionArray trackSelections) {
                Log.v(TAG, "Listener-onTracksChanged...");
            }

            @Override
            public void onLoadingChanged(boolean isLoading) {
                Log.v(TAG, "Listener-onLoadingChanged...isLoading:"+isLoading);
            }

            @Override
            public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
                Log.v(TAG, "Listener-onPlayerStateChanged..." + playbackState);
            }

            @Override
            public void onRepeatModeChanged(int repeatMode) {
                Log.v(TAG, "Listener-onRepeatModeChanged...");
            }

            @Override
            public void onPlayerError(ExoPlaybackException error) {
                Log.v(TAG, "Listener-onPlayerError...");
                player.stop();
                player.prepare(loopingSource);
                player.setPlayWhenReady(true);
            }

            @Override
            public void onPositionDiscontinuity() {
                Log.v(TAG, "Listener-onPositionDiscontinuity...");
            }

            @Override
            public void onPlaybackParametersChanged(PlaybackParameters playbackParameters) {
                Log.v(TAG, "Listener-onPlaybackParametersChanged...");
            }
        });
        PlaybackParameters playbackParameters = new PlaybackParameters(2.0f, 1.0f);
        player.setPlaybackParameters(playbackParameters);

        player.setPlayWhenReady(true); //run file/link when ready to play.
        player.setVideoDebugListener(this); //for listening to resolution change and  outputing the resolution
    }//End of onCreate

    @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) {
        Log.v(TAG, "onVideoSizeChanged ["  + " width: " + width + " height: " + height + "]");
        resolutionTextView.setText("RES:(WxH):"+width+"X"+height +"\n           "+height+"p");
    }

    @Override
    public void onRenderedFirstFrame(Surface surface) {

    }

    @Override
    public void onVideoDisabled(DecoderCounters counters) {

    }




//-------------------------------------------------------ANDROID LIFECYCLE---------------------------------------------------------------------------------------------

    @Override
    protected void onStop() {
        super.onStop();
        Log.v(TAG, "onStop()...");
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.v(TAG, "onStart()...");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.v(TAG, "onResume()...");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.v(TAG, "onPause()...");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.v(TAG, "onDestroy()...");
        player.release();
    }
}

,布局是

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"


    >

    <RelativeLayout
        android:id="@+id/mainlayoutexo"
        android:layout_width="match_parent"
        android:layout_height="match_parent">


        <com.google.android.exoplayer2.ui.SimpleExoPlayerView
            android:id="@+id/player_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_above="@+id/PlaybackControlViewa"

            android:layout_marginTop="10dp"
            android:focusable="true"
            app:resize_mode="fit"
            app:use_controller="false" />

        <com.google.android.exoplayer2.ui.PlaybackControlView
            android:id="@+id/PlaybackControlViewa"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:background="@color/bluebackground">

            <ImageButton
                android:id="@id/exo_play"
                style="@style/ExoMediaButton.Play" />

            <ImageButton
                android:id="@id/exo_pause"
                style="@style/ExoMediaButton.Pause" />

            <com.google.android.exoplayer2.ui.DefaultTimeBar
                android:id="@id/exo_progress"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#F20000" />


        </com.google.android.exoplayer2.ui.PlaybackControlView>

    </RelativeLayout>
</RelativeLayout>

我只想旋转包含exoplayer的相对布局,如何做到这一点并用它进行视频播放

1 个答案:

答案 0 :(得分:0)

我正在运行相同的代码,虽然旋转dosnt有任何处理,但是当我在纵向和横向之间切换时,视频仍会运行。它暂停一秒,然后继续新的方向。

我在日志中收到警告,但这是关于预期的。

你可以发布你得到的日志吗?

代码来自: https://github.com/ayalus/ExoPlayer-2-Example/tree/master/ExoPlayer2Example