使用SavedInstanceState暂停,恢复和销毁MediaPlayers

问题描述

一段时间以来,我一直在为此而苦苦挣扎,这可能是我从未看到或得到的非常简单的东西。我的主要活动有两种不同的观点,正在播放两种不同的视频。我了解我需要保存它们所在的位置,然后暂停它们,以便在应用移至另一个活动时暂停,然后在用户返回到该活动时,视频恢复并继续循环播放。比我希望在用户关闭应用程序后发布两个视频。因此,显然我理解了这些概念,并且理解我需要使用onPause(),onResume()和onDestroy()。但是我正在努力用我拥有的代码来实现它。这是我的主要活动:

    package com.example.mediacontrollerexperiments;

import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;

public class MainActivity extends AppCompatActivity {
    ConstraintLayout theView;
    private VideoView view1;
    private VideoView view2;

    String TAG = "VideoPlayer";
    private Object String;

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


        configureVideoView();



        theView.setonTouchListener(new OnSwipetouchListener(MainActivity.this) {
            //Note that everything is opposite.  So SwipeBottom is really from the top and top is really from the bottom and right and left are also reversed.


            @Override
            public void onSwipeBottom() {
                Intent map = new Intent(MainActivity.this,TestActivity.class);
                startActivity(map);
                Toast.makeText(getApplicationContext(),getResources().getString(R.string.toastBottom),Toast.LENGTH_SHORT).show();
            }

        });
        ;


    }



    private void configureVideoView() {

        MediaController mediaController;
        theView=(ConstraintLayout) findViewById(R.id.theView);

        view1=(VideoView) findViewById(R.id.view1);

        view1.setVideoPath("https://emMetapp.s3-us-west-1.amazonaws.com/bar.mp4");
        mediaController = new MediaController(this);
        mediaController.setAnchorView(view1);
        final int[] media_length = new int[1];
        view1.setMediaController(mediaController);
        view1.setonPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                mp.setLooping(true);
                mp.setVolume(0,0);
                Log.i(TAG,"Duration = " + view1.getDuration());
            }


        });
        view1.start();


        view2=(VideoView) findViewById(R.id.view2);


        view2.setVideoPath("https://emMetapp.s3-us-west-1.amazonaws.com/catering.mp4");
        mediaController = new MediaController(this);
        final int[] media_length2 = new int[1];
        mediaController.setAnchorView(view2);
        view2.setMediaController(mediaController);
        view2.setonPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                mp.setLooping(true);
                mp.setVolume(0,"Duration = " + view2.getDuration());
            }

        });
        view2.start();

    }


}

让我知道您是否还需要其他东西。

请一定要有一个主题可以解释这一点。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)