F0r3v3r-A-N00b
F0r3v3r-A-N00b

Reputation: 3003

Android: VideoView sometimes stop playing videos after some time

I created an app that plays video continuously from a local (LAN) website. First it loads a text file that contains a list of the videos to be played. Then the app plays each video one after another. It works fine but sometimes it just stops. at random videos. When it is stopped and I open another activity and go back to the previous activity (video) the video plays again. Sometimes I'm able to play through all the videos in the play list and go back to the first one. In short, the app is able to play all the videos (no encoder problem). Here's my code.

    videoView.setOnCompletionListener(new OnCompletionListener() {          
    @Override
    public void onCompletion(MediaPlayer mp) {              
        videourl = pmgr.getNextFile();
        video = Uri.parse(videourl);
        videoView.setVideoURI(video);
        videoView.requestFocus();
        videoView.start();              
    }
});

videoView.setOnPreparedListener(new OnPreparedListener() {          
    @Override
    public void onPrepared(MediaPlayer mp) {                
        mp.setVolume(actualVolume, actualVolume);
        mp.setVideoScalingMode(MediaPlayer.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING);
    }
});


@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
videoView.seekTo(position);
}






@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
position = videoView.getCurrentPosition();
}

Upvotes: 1

Views: 2629

Answers (1)

F0r3v3r-A-N00b
F0r3v3r-A-N00b

Reputation: 3003

Here's what worked for me.

videoView.setOnCompletionListener(new OnCompletionListener() {          
        @Override
        public void onCompletion(MediaPlayer mp) {              
            videoView.stopPlayback();
            videourl = pmgr.getNextFile();
            video = Uri.parse(videourl);
            videoView.setVideoURI(video);
            videoView.requestFocus();
            videoView.start();              
        }
    });

I added a call to stopPlayback() of the VideoView class. Since then all my videos (from a local website) runs continuously.

PlaylistManager class

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Scanner;

import android.os.AsyncTask;

public class PlaylistManager extends AsyncTask<String, Integer, String> {

private PlaylistLoadedListener pl;
private String urlPath = "";
private String tempList;
private ArrayList<String> playlist;
private int currentIndex = -1;
private boolean playlistLoaded = false;

HttpURLConnection urlConnection;

public boolean getPlaylistLoaded() {
    return playlistLoaded;
}
public ArrayList<String> getPlaylist() {
    return playlist;
}   






public PlaylistManager(String urlPath,PlaylistLoadedListener pl) {
    this.urlPath = urlPath;
    this.pl = pl;
}   





@Override
protected String doInBackground(String... params) {
    try {           
        URL url = new URL(this.urlPath + params[0]);
        urlConnection = (HttpURLConnection) url.openConnection();

        urlConnection.setRequestMethod("GET");
        urlConnection.connect();

        InputStream in = urlConnection.getInputStream();
        final ByteArrayOutputStream bo = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int length = in.read(buffer);       
        bo.write(buffer, 0, length);
        tempList = bo.toString();           
    } catch (MalformedURLException e) {         
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return tempList;
}






@Override
protected void onPostExecute(String result) {
    try {
        playlist = new ArrayList<String>();
        Scanner scanner = new Scanner(result);
        while (scanner.hasNextLine()) {
          String line = scanner.nextLine();
          if (!line.contains("--")) {
              playlist.add(line);             
          }       
        }
        scanner.close();
        if (playlist.size() != 0) {
            playlistLoaded = true;
            pl.OnPlaylistLoaded();
        }   
    }
    catch (NullPointerException e) {
    }       
    finally {
        urlConnection.disconnect(); 
    }       
}






public String getNextFile() {
    String ret = "";
    if (playlistLoaded) {
        if (playlist.size() != 0) {
            currentIndex++;
            if (currentIndex == playlist.size()) {
                currentIndex = 0;
            }
            ret = urlPath + playlist.get(currentIndex);
        }                       
    }
    return ret;
}

}

PlaylistLoadListener interface

public interface PlaylistLoadedListener {
    void OnPlaylistLoaded();
}

Upvotes: 1

Related Questions