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);
    }
}

Answers


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();
}

Need Your Help

How to make the radial part of elements with border-radius applied to them, unselectable in chrome and safari?

css

In this circle, I noticed the pointer only appears when the cursor is inside the circle for IE9 and firefox. However in safari or chrome the cursor is a pointer if you are anywhere inside the div

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.