Android 流媒体播放器开发实现
2010-05-26 15:38:00 来源:WEB开发网mPlayer.start();
return;
}
current = path;
mPlayer = null;
new PlayThread(current).start();
} catch (Exception e) {
}
}
private void setListener() {
if (mPlayer != null) {
mPlayer.setOnErrorListener(this);
mPlayer.setOnBufferingUpdateListener(this);
mPlayer.setOnCompletionListener(this);
}
}
/**
* 下载数据,分段下载
* todo:联网方式和分段
* @param mediaUrl
* @param start
* @param end
*/
private void playFromNet(String mediaUrl, int start, int end) {
URLConnection cn = null;
FileOutputStream out = null;
InputStream is = null;
try {
cn = new URL(mediaUrl).openConnection();
cn.connect();
is = cn.getInputStream();
int mediaLength = cn.getContentLength();
if (is == null) {
return;
}
deleteTempFile(true);
DLTempFile = File.createTempFile(TEMP_DOWNLOAD_FILE_NAME,
FILE_POSTFIX);
out = new FileOutputStream(DLTempFile);
byte buf[] = new byte[PER_READ];
int readLength = 0;
while (readLength != -1 && !stop) {
if (pause) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
continue;
}
readLength = is.read(buf);
if (readLength > 0) {
try {
out.write(buf, 0, readLength);
totalKbRead += readLength;
} catch (Exception e) {
Log.e(TAG, e.toString());
}
}
dealWithBufferData();
}
if (totalKbRead == mediaLength) {
downloadOver = true;
更多精彩
赞助商链接