p.streef
p.streef

Reputation: 3815

Play video from URL in new activity when finished loading

Using the following code I can play a video on android in a new Activity. I would however like to preload the video (during a loading screen) and then show it when it is fully loaded. Is it possible to perhaps somehow trigger the activity to show later? In theory It would also be ok to not use a different activity.

public class VideoPlayer extends Activity implements OnCompletionListener,OnPreparedListener
{

    private VideoView mVV;

    @Override
    public void onCreate(Bundle b) {
        super.onCreate(b);

        setContentView(R.layout.videoplayer);

        String url = getIntent().getStringExtra("url");
        if(url == null)
            finish();

        mVV = (VideoView)findViewById(R.id.myvideoview);
        mVV.setOnCompletionListener(this);
        mVV.setOnPreparedListener(this);
        mVV.setVideoURI(Uri.parse(url));
        mVV.start();
    }


    public void stopPlaying() {
        mVV.stopPlayback();
        this.finish();
    }

    @Override
    public void onCompletion(MediaPlayer mp) {
        finish();
    }

    @Override
    public void onPrepared(MediaPlayer mp) {

    }
}

from my main activity:

private void playVideo(String url) {
        Intent videoPlaybackActivity = new Intent(this, VideoPlayer.class);
        videoPlaybackActivity.putExtra("url", url);
        startActivity(videoPlaybackActivity);
    }

I presume I can use the onPrepared function, but I'm not sure how to do the activity triggering to show the activity later.

Upvotes: 1

Views: 690

Answers (1)

Riddim
Riddim

Reputation: 153

http://developer.android.com/reference/android/widget/VideoView.htmlHere are the methods for the VideoView. So there is indeed a: setOnPreparedListener(MediaPlayer.OnPreparedListener l)

Probably the best way is to use fragments in the activity and show the fragment in the activity where the mVV = (VideoView)findViewById(R.id.myvideoview); is declared when the onprepared is called.

Or hide and show the view in the activity. (using fragments is probably nicer). Think that will work and that that is what you wanted?

Upvotes: 1

Related Questions