11

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.

Meenal
  • 2,879
  • 5
  • 19
  • 43

1 Answers1

18

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

Meenal
  • 2,879
  • 5
  • 19
  • 43
  • 2
    great you find solution – Digvesh Patel Feb 04 '14 at 10:05
  • hi @MeenalSharma i'm trying to do the same thing that you have done here but i'm not using a local file, instead i'm using byte chunks coming from the network... but it's not working... my video view says 'Can't play video' when i run videoView.start()... can you help me? – jguilhermeam Sep 12 '14 at 16:17
  • @jguilhermeam may the format of the video your are getting is not correct or there will be some error in the data received.. – Meenal Sep 15 '14 at 04:08
  • yes @MeenalSharma i've discovered a format that worked on streaming. but in the seekTo() part, the video doesn't look fluid. now i'm looking for other solutions – jguilhermeam Sep 15 '14 at 20:09
  • @jguilhermeam I am facing same error . How did you resolve this ? – WannaBeGeek Feb 11 '15 at 05:05
  • 1
    @Kanchan after a lot of exploring, i've found out that the only way to do this and make the video look fluid is using RTP streaming. in android you can use this lib https://github.com/fyhertz/libstreaming – jguilhermeam Feb 11 '15 at 12:58
  • @jguilhermeam, how did you make the library work for you? Can you share a sample? I need to broadcast video and it should play in sync with server device on rest of the devices. – Bette Devine Jun 04 '15 at 10:39
  • @BetteDevine I'm sorry, but I don't have the code anymore, I've quit the company that I was working with this library. But I remember that it was simple, the tutorial in github was enough to set everything ok. Use the sample code. – jguilhermeam Jun 08 '15 at 13:00
  • hi @Meenal this is not working when I play and buffer at the same time. If I put whole video once in buffer then it works. but while playing with buffering, it goes in error listener. I can't use `libstreaming` library because i am working in xamarin.android. and it is not available in xamarin. can you pls help ? – Gayatri Gokhale Oct 06 '18 at 08:26