将屏幕从横向旋转到prot时保存网址

时间:2018-09-01 09:37:00

标签: java android

我正在尝试在播放器上播放视频,并且该视频是我从api获取的网址 所以我在recycleview1中有一个频道,当我单击一个频道时,该频道开始在新活动中播放 在该活动中,播放器占据了屏幕的一半,其余部分用于recycleview2,其中包含一些通道,可在通道之间轻松快速地导航

因此,当我从recycleview1打开频道并开始播放时,我将屏幕旋转至横向,使其正常工作,然后恢复屏幕以再次移植仍在运行的频道

但是问题是当我单击来自recycleview2的频道时,该频道导致更新已经播放的网址并启动新的网址 因此,每次我从recycleview2点击频道时,视频视图都会更新为点击的新频道网址

我遇到问题,当我从recycleview2播放频道后旋转屏幕时,videoview开始播放我从recyclewview1单击的频道,然后再从recycleview2单击新频道,因为它返回到onCreate方法,该方法将根据传递的信息来构建url。点击

所以我以前保存的是url的实例状态,所以当我旋转屏幕以显示风景时,vidoeview仍然播放了最后播放的频道

但是问题是当我重新旋转屏幕以再次进行波特率设置时,此处的视频视图返回到开始活动时播放的第一个频道

我也尝试保存实例状态,但是它不起作用

这是代码:

public class TVChannel extends AppCompatActivity implements
        TVAdapter.TVAdapterOnClickHandler {

    private VideoView videoView;
    private int position = 0;
    private String video;
    private MediaController mediaController;

    String mChannelTitle;
    String mChannelApp;
    String mChannelStreamname;
    String mChannelCat;

    TVAdapter mAdapter;
    RecyclerView mSportsList;

    private TextView mCategory;
    private TextView mErrorMessageDisplay;
    private ProgressBar mLoadingIndicator;

    Context mContext ;

    public TVItem channelObject;

    Uri vidUri;
    String myBoolean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
            setContentView(R.layout.activity_tv_channel);

            ButterKnife.bind(this);

            mSportsList = (RecyclerView) findViewById(R.id.rv_channel);
            mErrorMessageDisplay = (TextView) findViewById(R.id.tv_error_message_display);

            mContext = this;

            channelObject = getIntent().getParcelableExtra("TVChannel");

            if (channelObject != null) {
                mChannelTitle = String.valueOf(channelObject.getTitle());
                mChannelApp = String.valueOf(channelObject.getApp());
                mChannelStreamname = String.valueOf(channelObject.getStreamName());
                mChannelCat = String.valueOf(channelObject.getCat());
            }

            mCategory = (TextView) findViewById(R.id.category);
            videoView = (VideoView) findViewById(R.id.videoView);

                String host = "http://ip/";
                String app = mChannelApp;
                String streamname = mChannelStreamname;
                String playlist = "playlist.m3u8";
                vidUri = Uri.parse(host).buildUpon()
                        .appendPath(app)
                        .appendPath(streamname)
                        .appendPath(playlist)
                        .build();

                videoView.setVideoURI(vidUri);

            final PlayerControlView playerControlView = (PlayerControlView) findViewById(R.id.player_control_view);

            playerControlView.setAlwaysShow(true);

            PlayerControlView.ViewHolder viewHolder = playerControlView.getViewHolder();
            viewHolder.pausePlayButton.setPauseDrawable(ContextCompat.getDrawable(this, R.drawable.pause));
            viewHolder.pausePlayButton.setPlayDrawable(ContextCompat.getDrawable(this, R.drawable.play));
            viewHolder.controlsBackground.setBackgroundColor(ContextCompat.getColor(this, R.color.colorPrimaryDark));
            viewHolder.currentTimeText.setTextColor(ContextCompat.getColor(this, R.color.colorAccent));
//        viewHolder.totalTimeText.setTextSize(18);
            playerControlView.setNextListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(TVChannel.this, "onClick Next", Toast.LENGTH_SHORT).show();
                }
            });
            playerControlView.setPrevListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(TVChannel.this, "onClick Prev", Toast.LENGTH_SHORT).show();
                }
            });


            mediaController = playerControlView.getMediaControllerWrapper();

            videoView.setMediaController(mediaController);
            videoView.seekTo(position);
            videoView.start();
            videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    playerControlView.show();
                }
            });


            GridLayoutManager LayoutManagerSports = new GridLayoutManager(this, 3);
            mSportsList.setLayoutManager(LayoutManagerSports);
            mSportsList.setHasFixedSize(true);
            mAdapter = new TVAdapter(this, mContext);
            mSportsList.setAdapter(mAdapter);

            mLoadingIndicator = (ProgressBar) findViewById(R.id.pb_loading_indicator);

            String sortchannels = mChannelCat.toLowerCase() + ".php";

            loadTVData(sortchannels);

        } else {

            setContentView(R.layout.activity_tv_channel2);

            ButterKnife.bind(this);

            mSportsList = (RecyclerView) findViewById(R.id.rv_channel);
            mErrorMessageDisplay = (TextView) findViewById(R.id.tv_error_message_display);

            mContext = this;

            channelObject = getIntent().getParcelableExtra("TVChannel");

            if (channelObject != null) {
                mChannelTitle = String.valueOf(channelObject.getTitle());
                mChannelApp = String.valueOf(channelObject.getApp());
                mChannelStreamname = String.valueOf(channelObject.getStreamName());
                mChannelCat = String.valueOf(channelObject.getCat());
            }

            mCategory = (TextView) findViewById(R.id.category);
            videoView = (VideoView) findViewById(R.id.videoView);

            if (savedInstanceState != null){

                myBoolean = savedInstanceState.getString("video");
                Toast.makeText(this, "This is my Toast message!",
                        Toast.LENGTH_LONG).show();

                videoView.setVideoURI(Uri.parse(myBoolean));
            }

            else {

                String host = "http://ip/";
                String app = mChannelApp;
                String streamname = mChannelStreamname;
                String playlist = "playlist.m3u8";
                vidUri = Uri.parse(host).buildUpon()
                        .appendPath(app)
                        .appendPath(streamname)
                        .appendPath(playlist)
                        .build();

                videoView.setVideoURI(vidUri);

            }

            final PlayerControlView playerControlView = (PlayerControlView) findViewById(R.id.player_control_view);

            playerControlView.setAlwaysShow(false);

            PlayerControlView.ViewHolder viewHolder = playerControlView.getViewHolder();
            viewHolder.pausePlayButton.setPauseDrawable(ContextCompat.getDrawable(this, R.drawable.pause));
            viewHolder.pausePlayButton.setPlayDrawable(ContextCompat.getDrawable(this, R.drawable.play));
            viewHolder.controlsBackground.setBackgroundColor(ContextCompat.getColor(this, R.color.controlBackground));
            viewHolder.currentTimeText.setTextColor(ContextCompat.getColor(this, R.color.colorAccent));
//        viewHolder.totalTimeText.setTextSize(18);
            playerControlView.setNextListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(TVChannel.this, "onClick Next", Toast.LENGTH_SHORT).show();
                }
            });
            playerControlView.setPrevListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(TVChannel.this, "onClick Prev", Toast.LENGTH_SHORT).show();
                }
            });

            mediaController = playerControlView.getMediaControllerWrapperFullScreen();

            videoView.setMediaController(mediaController);
            videoView.seekTo(position);
            videoView.start();
            videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    playerControlView.show();
                }
            });


            GridLayoutManager LayoutManagerSports = new GridLayoutManager(this, 3);
            mSportsList.setLayoutManager(LayoutManagerSports);
            mSportsList.setHasFixedSize(true);
            mAdapter = new TVAdapter(this, mContext);
            mSportsList.setAdapter(mAdapter);

            mLoadingIndicator = (ProgressBar) findViewById(R.id.pb_loading_indicator);

            String sortchannels = mChannelCat.toLowerCase() + ".php";

            loadTVData(sortchannels);

        }

    }

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
        super.onSaveInstanceState(savedInstanceState);

        // Store current position.
        savedInstanceState.putString("video", String.valueOf(vidUri));
        videoView.start();
    }

    // After rotating the phone. This method is called.
    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);

        // Get saved position.
        video = savedInstanceState.getString("video");
        videoView.start();
    }


    private void loadTVData(String sortChannels) {
        showTVDataView();
        new FetchTVTask().execute(sortChannels);
    }

    @Override
    public void onClick(TVItem channel, View view) {

        if (channel != null) {
            mChannelTitle = String.valueOf(channel.getTitle());
            mChannelApp = String.valueOf(channel.getApp());
            mChannelStreamname = String.valueOf(channel.getStreamName());
        }

        String host = "http://ip/";
        String app = mChannelApp;
        String streamname = mChannelStreamname;
        String playlist = "playlist.m3u8";
        vidUri = Uri.parse(host).buildUpon()
                .appendPath(app)
                .appendPath(streamname)
                .appendPath(playlist)
                .build();

        videoView.setVideoURI(vidUri);

        final PlayerControlView playerControlView = (PlayerControlView) findViewById(R.id.player_control_view);
        playerControlView.setAlwaysShow(true);

        PlayerControlView.ViewHolder viewHolder = playerControlView.getViewHolder();
        viewHolder.pausePlayButton.setPauseDrawable(ContextCompat.getDrawable(this, R.drawable.pause));
        viewHolder.pausePlayButton.setPlayDrawable(ContextCompat.getDrawable(this, R.drawable.play));
        viewHolder.controlsBackground.setBackgroundColor(ContextCompat.getColor(this, R.color.colorPrimaryDark));
        viewHolder.currentTimeText.setTextColor(ContextCompat.getColor(this, R.color.colorAccent));
//        viewHolder.totalTimeText.setTextSize(18);
        playerControlView.setNextListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(TVChannel.this, "onClick Next", Toast.LENGTH_SHORT).show();
            }
        });
        playerControlView.setPrevListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(TVChannel.this, "onClick Prev", Toast.LENGTH_SHORT).show();
            }
        });

        videoView.setMediaController(playerControlView.getMediaControllerWrapper());
//
        videoView.seekTo(position);
        videoView.start();
        videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                playerControlView.show();
            }
        });

    }

    private void showTVDataView() {
        mErrorMessageDisplay.setVisibility(View.INVISIBLE);
        mSportsList.setVisibility(View.VISIBLE);
    }

    private void showErrorMessage() {
        mSportsList.setVisibility(View.INVISIBLE);
        mErrorMessageDisplay.setVisibility(View.VISIBLE);
    }

    public class FetchTVTask extends AsyncTask<String, Void, ArrayList<TVItem>> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            mLoadingIndicator.setVisibility(View.VISIBLE);
        }

        @Override
        protected ArrayList<TVItem> doInBackground(String... params) {
            if (params.length == 0) {
                return null;
            }
            String sortChannels = params[0];
            URL channelRequestUrl = NetworkTV.buildUrl(sortChannels);

            try {
                String jsonTVResponse = NetworkTV.getResponseFromHttpUrl(channelRequestUrl);

                ArrayList<TVItem> simpleJsonTVData = JsonTV.getSimpleTVStringsFromJson(TVChannel.this, jsonTVResponse);

                return simpleJsonTVData;

            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }

        @Override
        protected void onPostExecute(ArrayList<TVItem> TVData) {
            mLoadingIndicator.setVisibility(View.INVISIBLE);
            mCategory.setText(TVData.get(0).getCat());
            if (TVData != null) {
                showTVDataView();
                mAdapter.setTVData(TVData);
            } else {
                showErrorMessage();
            }
        }
    }


}

您可以看到我使用的是

if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
}

else{
}

设置纵向和横向内容视图

这是我用来在横向模式下保存url的代码:

if (savedInstanceState != null){

                myBoolean = savedInstanceState.getString("video");
                Toast.makeText(this, "This is my Toast message!",
                        Toast.LENGTH_LONG).show();

                videoView.setVideoURI(Uri.parse(myBoolean));
            }

            else {

                String host = "http://ip/";
                String app = mChannelApp;
                String streamname = mChannelStreamname;
                String playlist = "playlist.m3u8";
                vidUri = Uri.parse(host).buildUpon()
                        .appendPath(app)
                        .appendPath(streamname)
                        .appendPath(playlist)
                        .build();

                videoView.setVideoURI(vidUri);

            }

,如果saveInstanceState为null,则根据我以纵向模式传递的点击信息构建这样的网址:

channelObject = getIntent().getParcelableExtra("TVChannel");

                if (channelObject != null) {
                    mChannelTitle = String.valueOf(channelObject.getTitle());
                    mChannelApp = String.valueOf(channelObject.getApp());
                    mChannelStreamname = String.valueOf(channelObject.getStreamName());
                    mChannelCat = String.valueOf(channelObject.getCat());
                }

再次将屏幕从横向模式旋转到纵向模式时,如何保存URL通道?

0 个答案:

没有答案