在TextureView中裁剪并显示视频片段吗?

时间:2018-08-20 08:38:45

标签: android exoplayer textureview

我正在使用ExoPlayer来显示这样的流视频:

<?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"
    android:background="@drawable/mixer_display"
    tools:context=".MainActivity">

    <TextureView
        android:id="@+id/texture_view"
        android:layout_width="298dp"
        android:layout_height="205dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="37dp"
        android:layout_marginTop="31dp"
        android:focusable="true"
        app:resize_mode="fill"/>

    <!--<com.google.android.exoplayer2.ui.PlayerView-->
        <!--android:id="@+id/player_view"-->
        <!--android:layout_width="298dp"-->
        <!--android:layout_height="205dp"-->
        <!--android:layout_alignParentStart="true"-->
        <!--android:layout_alignParentTop="true"-->
        <!--android:layout_marginStart="37dp"-->
        <!--android:layout_marginTop="31dp"-->
        <!--android:focusable="true" />-->

    <TextureView
        android:id="@+id/angle_view1"
        android:layout_width="66dp"
        android:layout_height="48dp"
        android:layout_alignParentStart="true"
        android:layout_alignTop="@+id/angle_view4"
        android:layout_marginStart="24dp"
        android:focusable="true" />

    <TextureView
        android:id="@+id/angle_view2"
        android:layout_width="66dp"
        android:layout_height="48dp"
        android:layout_alignParentStart="true"
        android:layout_alignTop="@+id/angle_view1"
        android:layout_marginStart="110dp"
        android:focusable="true" />

    <TextureView
        android:id="@+id/angle_view3"
        android:layout_width="66dp"
        android:layout_height="48dp"
        android:layout_alignParentStart="true"
        android:layout_alignTop="@+id/angle_view1"
        android:layout_marginStart="196dp"
        android:focusable="true" />

    <TextureView
        android:id="@+id/angle_view4"
        android:layout_width="66dp"
        android:layout_height="48dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:layout_marginBottom="13dp"
        android:layout_marginEnd="244dp"
        android:focusable="true" />

</RelativeLayout>

MainActivity:

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    private TextureView simpleExoPlayerView;
    private SimpleExoPlayer player;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

// 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
            player = ExoPlayerFactory.newSimpleInstance(this, trackSelector, loadControl);
            simpleExoPlayerView = new TextureView(this);
            simpleExoPlayerView = (TextureView) findViewById(R.id.texture_view);

//Set media controller

// Bind the player to the view.
            player.setVideoTextureView(simpleExoPlayerView);
            Uri mp4VideoUri = Uri.parse("http://mediaserver01.heartscreation.co.jp/Dash2/John_Test/John_Test.mpd");
//            simpleExoPlayerView.setResizeMode(AspectRatioFrameLayout.RESIZE_MODE_FILL);
            player.setVideoScalingMode(C.VIDEO_SCALING_MODE_SCALE_TO_FIT);

//VIDEO FROM SD CARD: (2 steps. set up file and path, then change videoSource to get the file)
//        String urimp4 = "path/FileName.mp4"; //upload file to device and add path/name.mp4
//        Uri mp4VideoUri = Uri.parse(Environment.getExternalStorageDirectory().getAbsolutePath()+urimp4);

//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);
            DashChunkSource.Factory dashChunkSourceFactory =
                    new DefaultDashChunkSource.Factory(dataSourceFactory);
//Produces Extractor instances for parsing the media data.
            ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();

            MediaSource videoSource = new DashMediaSource(mp4VideoUri, dataSourceFactory, dashChunkSourceFactory, null, null);
            final LoopingMediaSource loopingSource = new LoopingMediaSource(videoSource);

// Prepare the player with the source.
            player.prepare(loopingSource);
            player.addListener(new Player.EventListener() {
//            @Override
//            public void onTimelineChanged(Timeline timeline, Object manifest) {
//                Log.v(TAG, "Listener-onTimelineChanged...");
//            }

                @Override
                public void onTimelineChanged(Timeline timeline, @Nullable Object manifest, int reason) {

                }

                @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 onShuffleModeEnabledChanged(boolean shuffleModeEnabled) {

                }

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

                @Override
                public void onPositionDiscontinuity(int reason) {

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

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

                @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
//        }
    }//End of onCreate

但是当我运行应用程序时,视频出现了偏差,它在视图中并不正确。如何解决?

还有一个问题,我的视频流由四个小屏幕组成。现在,我想对其进行裁剪并在angle_view中ID为activity_main.xml的一个TextureView中显示每个季度。 任何意见和建议将不胜感激。谢谢。

0 个答案:

没有答案