android如何在一个视频中播放不同的视频

时间:2014-03-25 09:26:38

标签: android android-videoview

我有这段代码,我只会使用一个视频播放不同的视频...

但由于我对Android编程很新,我不知道如何正确使用setoncompletionlistener ..你能不能帮我解决问题..

这是我的代码..

我很感激帮助:))

package com.thesis.americansignlanguage;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.VideoView;

public class Videocompareclass extends Activity implements MediaPlayer.OnCompletionListener {
    String get;
VideoView view;
int wordIndex = 0;
String[] getSplit;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.gesturecompare);
    Bundle gotWord = getIntent().getExtras();
    get = gotWord.getString("key");
    TextView Word = (TextView)findViewById(R.id.textView1);
    view = (VideoView)findViewById(R.id.vd_view);
    Word.setText(get);
    view.setMediaController(new MediaController(this));

    getSplit = get.split(" ");

    String path = "/storage/sdcard1/video/" + getSplit[wordIndex] + ".mp4";
    wordIndex++;
    view.setVideoPath(path);
    view.requestFocus();
    view.start();

    view.setOnPreparedListener(new OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            //  progressBar.setVisibility(View.GONE);
            //  loadingText.setVisibility(View.GONE);
        }
    });

    view.setOnErrorListener(new OnErrorListener() {
        @Override
        public boolean onError(MediaPlayer mp, int what, int extra) {
            Log.d("error code", "whate: " + what +" extra"+extra);
            //progressBar.setVisibility(View.GONE);
            //loadingText.setVisibility(View.GONE);
            return false;
        }
    });

    view.setOnCompletionListener(completeListener);
}

OnCompletionListener completeListener = new OnCompletionListener() {

    @Override
    public void onCompletion(MediaPlayer mp) {
        Log.e("VideoPlayer", "OnComplete");
        if(get.length() >= wordIndex) {
        String path = "/storage/sdcard1/video/" + getSplit[wordIndex] + ".mp4";
        wordIndex++;
        view.setVideoPath(path);
        view.requestFocus();
        view.setOnCompletionListener(completeListener);
        view.start();
        }

        else {
             mp.release();
             finish();
        }
    }
};
}

这里的logcat正在说.. 它有像ArrayIndexOutofBounds这样的错误.. 我不知道如何解决它..

03-26 20:46:19.047: E/AndroidRuntime(28343): FATAL EXCEPTION: main
03-26 20:46:19.047: E/AndroidRuntime(28343): java.lang.ArrayIndexOutOfBoundsException: length=2; index=2
03-26 20:46:19.047: E/AndroidRuntime(28343):    at com.thesis.americansignlanguage.Videocompareclass$1.onCompletion(Videocompareclass.java:68)
03-26 20:46:19.047: E/AndroidRuntime(28343):    at android.widget.VideoView$3.onCompletion(VideoView.java:381)
03-26 20:46:19.047: E/AndroidRuntime(28343):    at android.media.MediaPlayer$EventHandler.handleMessage(MediaPlayer.java:2075)
03-26 20:46:19.047: E/AndroidRuntime(28343):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-26 20:46:19.047: E/AndroidRuntime(28343):    at android.os.Looper.loop(Looper.java:137)
03-26 20:46:19.047: E/AndroidRuntime(28343):    at android.app.ActivityThread.main(ActivityThread.java:4800)
03-26 20:46:19.047: E/AndroidRuntime(28343):    at java.lang.reflect.Method.invokeNative(Native Method)
03-26 20:46:19.047: E/AndroidRuntime(28343):    at java.lang.reflect.Method.invoke(Method.java:511)
03-26 20:46:19.047: E/AndroidRuntime(28343):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:798)
03-26 20:46:19.047: E/AndroidRuntime(28343):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:565)
03-26 20:46:19.047: E/AndroidRuntime(28343):    at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

我们可以这样设置: -

videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
   @Override
   public void onCompletion(MediaPlayer pMp) {
    Log.e("VideoPlayer", "OnComplete");
     // your logic here 
   }
});

答案 1 :(得分:0)

//您一次只能播放一个视频,如果您想在完成第一个视频时播放下一个视频,只需使用此方法即可。 //在on create中应该声明vdieo视图

   public count = 0;
private void playVideo() {
    Log.d("check", "URL: "+url);
            int count= 0;                   
        String path = "/storage/extSdCard/video/" + getSplit[count] + ".mp4";
        count++;
    videoView.setVideoPath(uri);
    videoView.requestFocus();
    videoView.start();  

    videoView.setOnPreparedListener(new OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
        //  progressBar.setVisibility(View.GONE);
        //  loadingText.setVisibility(View.GONE);
        }
    });

    videoView.setOnErrorListener(new OnErrorListener() {
        @Override
        public boolean onError(MediaPlayer mp, int what, int extra) {
            Log.d("error code","whate: "+ what +" extra"+extra);
            //progressBar.setVisibility(View.GONE);
            //loadingText.setVisibility(View.GONE);
            return false;
        }
    });
     videoView.setOnCompletionListener(completeListner);
   }
          OnCompletionListener  completeListner = new OnCompletionListener() {

        @Override
    public void onCompletion(MediaPlayer mp) {
    if(videos.length() >= count) {  
               String path = "/storage/extSdCard/video/" + videos[count] + ".mp4";
               count++;
       videoView.setVideoPath(uri);
       videoView.requestFocus();
               videoView.setOnCompletionListener(completeListner);
       videoView.start();
                }
              else{
                     finish();
                   }                
        }
    }