0

I am a web developer tasked with making what should be a fairly simple app I think. I haven't had much experience with Android development so forgive what may be stupid mistakes.

I basically need to be able to take an array of video paths (that are synced on to the external SD) and play through the videos one by one. I don't need any controls or anything, just fullscreen video.

As I don't have much experience with Android, or Java for that matter, I thought I'd try Easy Video Player, which I guess is just a wrapper for the MediaPlayer class. I can get a single video to play no problem, but I'm having trouble playing the next video automatically. Here's my code so far (Just using online demo videos at the moment rather than off the SD card. Also, the layout file is pretty much the same as the example in the link above, just with a few different attribute values):

public class VideoActivity extends AppCompatActivity implements EasyVideoCallback {

    private static final String[] TEST_VIDEOS = {"http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4", "http://www.html5videoplayer.net/videos/toystory.mp4"};

    private EasyVideoPlayer player;
    private int currentVideo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video);
        View decorView = getWindow().getDecorView();
        int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
        decorView.setSystemUiVisibility(uiOptions);

        player = (EasyVideoPlayer) findViewById(R.id.video_player);

        player.setCallback(this);

        currentVideo = 0;

        player.setSource(Uri.parse(TEST_VIDEOS[currentVideo]));
    }

    @Override
    public void onPaused(EasyVideoPlayer player) {
        super.onPause();

        player.pause();
    }

    @Override
    public void onCompletion(EasyVideoPlayer player) {
        if (currentVideo != TEST_VIDEOS.length - 1) {
            currentVideo++;
            player.reset();
            player.setSource(Uri.parse(TEST_VIDEOS[currentVideo]));
            player.start();
        }
    }
}

This is the monitor output that appears when onCompletion() is called:

D/EasyVideoPlayer: onCompletion()
D/EasyVideoPlayer: Loading web URI: http://www.html5videoplayer.net/videos/toystory.mp4
E/MediaPlayer: start called in state 4
E/MediaPlayer: error (-38, 0)
E/MediaPlayer: Error (-38,0)

I could be nearly there or I could be way off, I just have no idea. I tried calling just player.reset() in onCompletion() but it didn't seem to do anything at all. I would assume the player would just go to an initial state with no video loaded.

Any help is appreciated and I apologise for the ignorance,

Thanks.

Update:

I moved player.start(); into an onPrepared() listener and the errors pasted above no longer appear. Also, after a few seconds, the sound of the next video plays but the player still shows the last frame of the first video with a play button.

Perceptic
  • 443
  • 1
  • 7
  • 19

1 Answers1

0

Please change your onCompletion method to this:

@Override
    public void onCompletion(EasyVideoPlayer player) {
        if (currentVideo != TEST_VIDEOS.length - 1) {
            currentVideo++;
            this.player = player;
            this.player.reset();
            this.player.setSource(Uri.parse(TEST_VIDEOS[currentVideo]));
            this.player.start();
            this.player.setCallback(this); 
        }
    }
Tushar Monirul
  • 4,944
  • 9
  • 39
  • 49
  • Thanks for the reply. Getting `Cannot resolve method 'setOnCompletionListener(...)'`. Without that line, it's exactly the same as before unfortunately. – Perceptic Oct 09 '16 at 19:07
  • Ok, I did not notice that you are using third party library. Replace that line by `this.player.setCallback(this);` – Tushar Monirul Oct 09 '16 at 19:21
  • did you replace `this.player.setOnCompletionListener(this);` to `this.player.setCallback(this);`? this should work, because you have used the `setCallback(this);` in `onCreate` method. – Tushar Monirul Oct 09 '16 at 19:31
  • @Perceptic ok, I have edited my answer, can you please try it again? – Tushar Monirul Oct 09 '16 at 19:34
  • Yeah, I replaced it and it's still the same error. Looking [here](http://stackoverflow.com/questions/15516469/android-media-player-start-called-in-state-4-error-38-0), it could be that the `start()` method is being called too early? – Perceptic Oct 09 '16 at 19:39