简体   繁体   中英

VideoView in Android is not playing video correctly

I am having a strange issue playing a VideoView. I have done my best to simplify the code as much as possible. Below are 4 classes: A, MirrorActivity, Replay, and MyTask. Assume the following occurs in this order:

  1. A is created.
  2. MirrorActivity () is created.
  3. Inside MirrorActivity ()'s onCreate(), that instance calls A 's setMirrorActivity () to allow A to have a reference to it.
  4. A 's doThis () method is called, which executes mirrorActivity. playVideo ().
  5. playVideo() is executed.
  6. Replay's executeVideo() is called.
  7. MyTask is executed.

For some strange reason, when the above is executed, the video does not play. However, when the myButton ImageButton is pressed inside MirrorActivity , it plays the video on command. Both of these seem to be doing the same thing by calling MirrorActivity's playVideo (). Do you know why the above does not execute?

A

public class A{
private static final A instance = new A();
private MirrorActivity mirrorActivity;

public static A getInstance()   {
    return instance;
}

public void setMirrorActivity(MirrorActivity mirrorActivity) {
    this.mirrorActivity = mirrorActivity;
}

public void doThis(String url){
    mirrorActivity.playVideo(String url);
}

}

MirrorActivity

public class MirrorActivity extends Activity {
public static String VIDEO_URL = "example.mp4";
public VideoView mVideoView;

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

    // Set Mirror Activity
    A.getInstance().setMirrorActivity(this);

    mVideoView = (VideoView) findViewById(R.id.mirrorVideoView);
    MyTask vTask = new MyTask(mVideoView);

    ImageButton myButton = (ImageButton) findViewById(R.id.myButton);
    myButton.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) 
        {
            MirrorActivity.this.playVideo(MirrorActivity.VIDEO_URL);
        }
    });
}

public void playVideo(String videoURL)
{
    MyTask mt = new MyTask(mVideoView);
    Replay.executeVideo(MirrorActivity.VIDEO_URL, 
              this, 
              mVideoView, 
              mt);
}

}

Replay

public class Replay{
public static void executeVideo(String uri, Activity activity, VideoView vid, MyTask mt) 
{
    vid.setMediaController(new MediaController(activity););
    vid.setVideoURI(Uri.parse(uri));
    mt.execute();
}

}

MyTask

public class MyTask extends AsyncTask<Void, Integer, Void> {
    private VideoView video;
    private int duration = 0;           // in milliseconds

    public MyTask(VideoView vid)    {
        video = vid;
    }

    @Override
    protected Void doInBackground(Void... params)   {
        video.start();
        video.requestFocus();

        video.setOnPreparedListener(new OnPreparedListener(){
            @Override
            public void onPrepared(MediaPlayer mp) {
                duration = video.getDuration();
            }
        });

        return null;
    }

    @Override
    protected void onProgressUpdate(Integer... values)  {
        super.onProgressUpdate(values);
    }
}

I think UI element cannot access in doInBackground . And also start() after every declaration.

Try this method too.

private MediaController ctlr;

@Override
public void onCreate(Bundle bundle) {
    super.onCreate(bundle);
    setContentView(R.layout.activity_overlay_gradient);
    getWindow().setFormat(PixelFormat.TRANSLUCENT);
// Set Mirror Activity
   A.getInstance().setMirrorActivity(this);

   mVideoView = (VideoView) findViewById(R.id.mirrorVideoView);

       Uri uri=Uri.parse(videourl or video path);
        mVideoView .setVideoURI(uri);
        mVideoView .setVideoPath(videourl);

        ctlr=new MediaController(this);
        ctlr.setAnchorView(video);
        ctlr.setMediaPlayer(video);
        mVideoView .setMediaController(ctlr);
        mVideoView .requestFocus();
        mVideoView .start();
}

Omg. I believe I found the solution. I'm sorry to those who were investigating, but here is what I did to get it to work both ways. Pressing imageButton and calling A.doThis() will be able to play the video now.

I modified MirrorActivity 's playVideo() function to use the following:

public void playVideo(final String videoURL)
{
    Runnable runnable = new Runnable(){

        @Override
        public void run()
        {
             MyTask mt = new MyTask(mVideoView);
             Replay.executeVideo(videoURL, 
                  MirrorActivity.this, 
                  mVideoView, 
                  mt);
        }
    });

    Handler mainHandler = new Handler(Looper.getMainLooper());
    mainHandler.post(runnable);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM