Search code examples
javaandroidbinarymp4

Show .bin video from web in a videoView/webView


I have a camera that streams a .bin video to a website, and I want to show this video in my android app.

For some reason it doesnt show even in a WebView, what can I do to resolve this? I tried using the Picasso library, didnt work. I also tried something like this, which also didnt work:

protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 
   
        VideoView videoView = findViewById(R.id.videoView); 
        Uri uri = Uri.parse(videoUrl);
        videoView.setVideoURI(uri); 
        MediaController mediaController = new MediaController(this); 
        mediaController.setAnchorView(videoView); 
        mediaController.setMediaPlayer(videoView); 
        videoView.setMediaController(mediaController); 
        videoView.start(); 

Solution

  • I figured it out, for anyone interested here is the code:

    Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    URL url = new URL(getIP.url);
                    try {
                        //http parametre
                        connection = (HttpURLConnection) url.openConnection();
                        connection.setRequestMethod("GET");
                        connection.setConnectTimeout(1000 * 5);
                        connection.setReadTimeout(1000 * 5);
                        connection.setDoInput(true);
                        connection.connect();
    
                            InputStream in = connection.getInputStream();
                            InputStreamReader isr = new InputStreamReader(in);
                            BufferedReader br = new BufferedReader(isr);
    
                            String data;
                            int len;
                            byte[] buffer;
    
                            while ((data = br.readLine()) != null) {
                                if (data.contains("Content-Type:")) {
                                    data = br.readLine();
                                    len = Integer.parseInt(data.split(":")[1].trim());
                                    bis = new BufferedInputStream(in);
                                    buffer = new byte[len];
    
                                    int t = 0;
                                    while (t < len) {
                                        t += bis.read(buffer, t, len - t);
                                    }
    
                                    Bytes2ImageFile(buffer, getExternalFilesDir(Environment.DIRECTORY_PICTURES) + "/0A.jpg");
                                    final Bitmap bitmap = BitmapFactory.decodeFile(getExternalFilesDir(Environment.DIRECTORY_PICTURES) + "/0A.jpg");
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            //connectButton.setText("OFF");
                                            imageView.setImageBitmap(bitmap);
                                        }
                                    });
                                }
                        }
    
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        if (bis != null) {
                            bis.close();}
    
                        if (fos != null) {
                            fos.close();}
    
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        thread.start();