我怎样才能播放视频块的字节? [英] How can i play a video in chunks of bytes?
本文介绍了我怎样才能播放视频块的字节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经把它改成了一个字节数组,它使用下面的代码成功地播放了:
private String getDataSource )抛出IOException {
InputStream stream = getAssets()。open(famous.3gp);
if(stream == null)
throw new RuntimeException(stream is null);
文件temp = File.createTempFile(test,mp4);
temp.deleteOnExit();
String tempPath = temp.getAbsolutePath();
int totalRead = 0;
int bytesToRead = 1 * 1024;
FileOutputStream out = new FileOutputStream(temp);
字节buf [] =新字节[128];
int numread = 0;
do {
numread = stream.read(buf);
totalRead + = numread;
if(numread< = 0)
break;
out.write(buf,0,numread);
} while(totalRead< bytesToRead);
尝试{
stream.close();
} catch(IOException ex){
Log.e(mini,error:+ ex.getMessage(),ex);
}
return tempPath;
$ b code
$ b 和in oncreate
videoView.setVideoPath(getDataSource());
但是我的要求是,我想玩数组中的前100个字节,完成后,按顺序播放下100个字节,然后再播放100个字节。
解决方案b
$ b
public class VideoDemo extends Activity {
private MediaController ctlr;
VideoView videoView = null;
上下文context = null;
long totalRead = 0;
int bytesToRead = 50 * 1024;
private int mPlayerPosition;
私有文件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;
$ b $ public public GetYouTube(){
}
@Override
public void run(){
super.run );
尝试{
文件bufferingDir =新文件(
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();
文件bufferFile = File.createTempFile(test,mp4);
BufferedOutputStream bufferOS = new BufferedOutputStream(
FileOutputStream(bufferFile));
InputStream is = getAssets()。open(famous.3gp);
BufferedInputStream bis = new BufferedInputStream(is,2048);
byte [] buffer = new byte [16384];
int numRead;
boolean started = false; ((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();
$ b public void setSourceAndStartPlay(File bufferFile){
try {
mPlayerPosition = videoView.getCurrentPosition ();
videoView.setVideoPath(bufferFile.getAbsolutePath());
videoView.start();
$ b $ catch(IllegalArgumentException e){
e.printStackTrace();
} catch(IllegalStateException e){
e.printStackTrace();
} catch(Exception e){
e.printStackTrace();
public void onCompletion(MediaPlayer mp){
mPlayerPosition = mp.getCurrentPosition();
尝试{
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();
$ b $ / code $ / pre
videoview
I want to play a video in android,which i had saved in my assets folder.
I have changed it into a byte array and it is playing successfully,using the below code
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;
// }
}
and in oncreate
videoView.setVideoPath(getDataSource());
But my requirement is, i want to play like, first 100 bytes from the array, and when it is finished ,play next 100 bytes in sequence and then another 100 like this.
解决方案 I got the solution
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();
}
}
and in my xml i had only videoview
这篇关于我怎样才能播放视频块的字节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文