Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/198.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何以字节块播放视频?_Android_File Io_Video Streaming_Android Videoview - Fatal编程技术网

Android 如何以字节块播放视频?

Android 如何以字节块播放视频?,android,file-io,video-streaming,android-videoview,Android,File Io,Video Streaming,Android Videoview,我想在android中播放一段视频,这段视频我保存在我的资产文件夹中。 我已经将其更改为字节数组,并使用以下代码成功播放 private String getDataSource() throws IOException { InputStream stream = getAssets().open("famous.3gp"); if (stream == null) throw new RuntimeException("stream i

我想在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;

         // }

     }
但我的要求是,我想先播放数组中的100个字节,完成后,按顺序播放下一个100个字节,然后再播放另一个100个这样的字节。

我找到了解决方案

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

great you find solutionhi@MeenalSharma我正在尝试做与您在这里做的相同的事情,但我没有使用本地文件,而是使用来自网络的字节块。。。但它不起作用。。。当我运行videoView.start()时,我的视频视图显示“无法播放视频”。。。你能帮我吗?@jguilhermam可能你正在获取的视频格式不正确,或者接收到的数据中会有一些错误。@yes@MeenalSharma我发现了一种适用于流媒体的格式。但是在seekTo()部分,视频看起来并不流畅。现在我在找另一个solutions@Kanchan经过大量的探索,我发现要做到这一点并使视频看起来流畅,唯一的方法就是使用RTP流媒体。在android中,您可以使用这个库
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();
    }

}