Android媒体播放器 - “无法创建媒体播放器”

时间:2018-05-14 13:49:14

标签: android-studio android-mediaplayer android-videoview

我正在尝试使用2个setForResults Image 1创建一个具有主要活动性的程序,首先它在图库中搜索视频,然后返回主要活动,按顺序在VideoView中播放,Image 2。进入可以放置字幕的其他活动后,再次回到主要活动状态。但此时出现警告无法播放此视频,例如Image 3

在放下字幕并转到包含其他VideoView的上一个活动后,会出现一个新警告Image 4

这是我主要活动的代码。我试图将全局变量用于测试。

我想在图像3和图像4上修复视频。因为我正在测试,此时我唯一的问题是使用VideoView和MediaPlayer,第一次之后无法再打开视频。

public class TelaHome extends Activity {

    VideoView video;
    int num = 0;
    ImageView videocheck;
    ImageView legcheck;
    Button butvideo;
    Button butleg;
    Button butstart;
    TextView textovideo;
    String nomedovideo;
    String urivideo;
    Uri Uridovideo;
    int cont = 0;
    int stop = 0;
    Boolean simvid = false;
    Boolean simleg = false;
    Intent videointent;
    Intent letraintent;
    Bundle pacotemain;
    Bundle letra;

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

        butstart = (Button) findViewById(R.id.butstart);
        butvideo = (Button) findViewById(R.id.butvideo);
        butleg = (Button) findViewById(R.id.butleg);
        videocheck = (ImageView) findViewById(R.id.imgvideocheck);
        legcheck = (ImageView) findViewById(R.id.imglegcheck);
        textovideo = (TextView) findViewById(R.id.textovideo);
        textovideo.setMovementMethod(new ScrollingMovementMethod());
        video = (VideoView) findViewById(R.id.videoView);


        butstart.setVisibility(View.INVISIBLE);
        videocheck.setImageResource(R.mipmap.ic_not_checked);
        textovideo.setText("");

        MediaController mc = new MediaController(this);
        mc.setAnchorView(video);
        video.setMediaController(mc);

        butvideo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                launchMainActivity();
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == 1) {
            if(resultCode == Activity.RESULT_OK){
                videointent = data;
                pacotemain = videointent.getExtras();

                if (pacotemain != null) {
                    nomedovideo = pacotemain.getString("arquivo");
                    urivideo = pacotemain.getString("curPF");
                    if (urivideo != null) {
                        Uridovideo = Uri.parse(urivideo);
                        video.setVideoURI(Uridovideo);
                        video.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                            @Override
                            public void onPrepared(MediaPlayer arg0) {
                                video.start();
                            }
                        });
                    }
                    String formato = "";
                    char[] lala;
                    if (nomedovideo != null) {
                        lala = (nomedovideo.toCharArray());
                        int x = nomedovideo.length();
                        for (int i = x - 3; i < x; i++) {
                            formato = (formato + lala[i]);
                        }
                        textovideo.setText(nomedovideo);
                    }
                }
                final Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (video.isPlaying()) {
                            cont++;
                            videocheck.setImageResource(R.mipmap.ic_checked);
                            simvid = true;
                        }
                    }
                }, 1000);
            }
        }

        if (requestCode == 7) {
            if(resultCode == Activity.RESULT_OK){
                letraintent = data;
                letra = letraintent.getExtras();
                if (letra != null){
                    legcheck.setImageResource(R.mipmap.ic_checked);
                    simleg = true;
                }
            }
        }
        final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                abrirplay(simvid,simleg, pacotemain, letra);
            }
        }, 1500);


    }//onActivityResult

我试了好几次,例如: 我在protected void OnCreate中重复此代码,在“requestCode == 1”和“requestCode == 7”中的“if(resultCode == Activity.CANCELED)”中重复

if (pacotemain != null) {
                    nomedovideo = pacotemain.getString("arquivo");
                    urivideo = pacotemain.getString("curPF");
                    if (urivideo != null) {
                        Uridovideo = Uri.parse(urivideo);
                        video.setVideoURI(Uridovideo);
                        video.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                            @Override
                            public void onPrepared(MediaPlayer arg0) {
                                video.start();
                            }
                        });

0 个答案:

没有答案