如何以字节块播放视频?
Posted
技术标签:
【中文标题】如何以字节块播放视频?【英文标题】:How can i play a video in chunks of bytes? 【发布时间】:2014-02-04 06:05:14 【问题描述】:我想在我保存在我的资产文件夹中的 android 中播放视频。 我已将其更改为字节数组,并且播放成功,使用以下代码
private String getDataSource() throws IOException
InputStream stream = getAssets().open("famous.3gp");
if (stream == null)
throw new RuntimeException("stream is null");
File temp = File.createTempFile("test", "mp4");
temp.deleteOnExit();
String tempPath = temp.getAbsolutePath();
int totalRead = 0;
int bytesToRead = 1 * 1024;
FileOutputStream out = new FileOutputStream(temp);
byte buf[] = new byte[128];
int numread = 0;
do
numread = stream.read(buf);
totalRead += numread;
if (numread <= 0)
break;
out.write(buf, 0, numread);
while (totalRead<bytesToRead);
try
stream.close();
catch (IOException ex)
Log.e("mini", "error: " + ex.getMessage(), ex);
return tempPath;
//
在oncreate中
videoView.setVideoPath(getDataSource());
但我的要求是,我想播放数组中的前 100 个字节,完成后,依次播放接下来的 100 个字节,然后再播放 100 个这样的字节。
【问题讨论】:
【参考方案1】:我找到了解决办法
public class VideoDemo extends Activity
private MediaController ctlr;
VideoView videoView = null;
Context context = null;
long totalRead = 0;
int bytesToRead = 50 * 1024;
private int mPlayerPosition;
private File mBufferFile;
@Override
public void onCreate(Bundle icicle)
super.onCreate(icicle);
getWindow().setFormat(PixelFormat.TRANSLUCENT);
setContentView(R.layout.main);
videoView = (VideoView) findViewById(R.id.videoview);
ctlr = new MediaController(this);
ctlr.setMediaPlayer(videoView);
videoView.setMediaController(ctlr);
videoView.requestFocus();
new GetYoutubeFile().start();
private class GetYoutubeFile extends Thread
private String mUrl;
private String mFile;
public GetYoutubeFile()
@Override
public void run()
super.run();
try
File bufferingDir = new File(
Environment.getExternalStorageDirectory()
+ "/YoutubeBuff");
InputStream stream = getAssets().open("famous.3gp");
if (stream == null)
throw new RuntimeException("stream is null");
File temp = File.createTempFile("test", "mp4");
System.out.println("hi");
temp.deleteOnExit();
String tempPath = temp.getAbsolutePath();
File bufferFile = File.createTempFile("test", "mp4");
BufferedOutputStream bufferOS = new BufferedOutputStream(
new FileOutputStream(bufferFile));
InputStream is = getAssets().open("famous.3gp");
BufferedInputStream bis = new BufferedInputStream(is, 2048);
byte[] buffer = new byte[16384];
int numRead;
boolean started = false;
while ((numRead = bis.read(buffer)) != -1)
bufferOS.write(buffer, 0, numRead);
bufferOS.flush();
totalRead += numRead;
if (totalRead > 120000 && !started)
Log.e("Player", "BufferHIT:StartPlay");
setSourceAndStartPlay(bufferFile);
started = true;
mBufferFile = bufferFile;
catch (MalformedURLException e)
e.printStackTrace();
catch (IOException e)
e.printStackTrace();
public void setSourceAndStartPlay(File bufferFile)
try
mPlayerPosition=videoView.getCurrentPosition();
videoView.setVideoPath(bufferFile.getAbsolutePath());
videoView.start();
catch (IllegalArgumentException e)
e.printStackTrace();
catch (IllegalStateException e)
e.printStackTrace();
catch (Exception e)
e.printStackTrace();
public void onCompletion(MediaPlayer mp)
mPlayerPosition = mp.getCurrentPosition();
try
mp.reset();
videoView.setVideoPath(new File("mnt/sdcard/YoutubeBuff/"
+ mBufferFile).getAbsolutePath());
mp.seekTo(mPlayerPosition);
videoView.start();
catch (IllegalArgumentException e)
e.printStackTrace();
catch (IllegalStateException e)
e.printStackTrace();
catch (Exception e)
e.printStackTrace();
在我的 xml 中,我只有 videoview
【讨论】:
很好的解决方案 嗨@MeenalSharma 我正在尝试做与您在这里所做的相同的事情,但我没有使用本地文件,而是使用来自网络的字节块......但是它不起作用...当我运行 videoView.start() 时,我的视频视图显示“无法播放视频”...您能帮帮我吗? @jguilhermeam 可能你得到的视频格式不正确或者接收到的数据会有一些错误.. 是的@MeenalSharma 我发现了一种适用于流媒体的格式。但在 seekTo() 部分,视频看起来并不流畅。现在我正在寻找其他解决方案 @Kanchan 经过大量探索后,我发现做到这一点并使视频看起来流畅的唯一方法是使用 RTP 流。在android中你可以使用这个库github.com/fyhertz/libstreaming以上是关于如何以字节块播放视频?的主要内容,如果未能解决你的问题,请参考以下文章
MediaRecorder - 如何在录制时播放视频块/块?
VS2008中Slider控件制作播放器的进度条,如何在视频播放时改变滑块位置