Android:VideoView有时会在一段时间后停止播放视频

时间:2014-11-28 02:04:28

标签: android

我创建了一个从本地(LAN)网站连续播放视频的应用。首先,它加载一个文本文件,其中包含要播放的视频列表。然后,应用程序一个接一个地播放每个视频。它工作正常,但有时它只是停止。在随机视频。当它停止并且我打开另一个活动并返回上一个活动(视频)时,视频将再次播放。有时我可以播放播放列表中的所有视频并返回第一个视频。简而言之,该应用程序能够播放所有视频(没有编码器问题)。这是我的代码。

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

1 个答案:

答案 0 :(得分:1)

这对我有用。

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

我添加了对VideoView类的stopPlayback()的调用。从那时起,我的所有视频(来自本地网站)都在不断运行。

PlaylistManager类

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接口

public interface PlaylistLoadedListener {
    void OnPlaylistLoaded();
}