Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Playing a downloaded video Error: 1, -2147483648

I'm trying to download a video file and then play it in an app, if I put the video file in the res folder I'm able to play it without a problem, however when I try and download the video then play it I get the VideoView Error: 1, -2147483648.

This is the code I use to download the video:

    try
    {

        URL url = new URL(videoURL);
        URLConnection conexion = url.openConnection();
        conexion.connect();

        int lengthOfFile = conexion.getContentLength();
        Log.d("ANDRO_ASYNC", "Length of file: " + lengthOfFile);

        InputStream input = new BufferedInputStream(url.openStream());




        final File dir = new File(this.getFilesDir() + "/videos");
        dir.mkdirs(); //create folders where write files
        final File file = new File(dir, "my_video.mp4");

        Log.d("writePath", file.getPath());

        FileOutputStream output = new FileOutputStream(file);

        byte data[] = new byte[1024];
        long total = 0;

        while((count = input.read(data)) != -1)
        {
            total += count;
            //publishProgress("" + (int)((total*100) / lengthOfFile));
            output.write(data, 0, count);
        }

        output.flush();
        output.close();
        input.close();

        Log.d("download", "finished");

    }
    catch(Exception e)
    {

        Log.d("Exception", e.getMessage());
    }

This appears to complete successfully, but when I try to play the file with the followed code, I get the error.

 // Play Video
    videoView = (VideoView)findViewById(R.id.surface_view);
    //

    File f = new File("/data/data/com.companyname.appname/files/videos/videoFilename.mp4");



    if(f.exists())
    {
        long len = f.length();
        String length = String.valueOf(len);
        Log.d("file", "exists");
        Log.d("length", (String)length);
    }
    else
    {
        Log.d("file", "does not exist");
    }

    //






    videoView.setMediaController(new MediaController(this));


    videoView.setVideoPath(f.getPath());
    videoView.start();

As seen in the code, before playing I check if the file exists and also it's size (it is the same size as the downloaded file) but it won't play, I'm guessing the file is corrupted but I don't know why.

I have the following permissions set in the manifest

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_INTERAL_STORAGE" />
like image 780
matthewp85 Avatar asked Nov 14 '22 01:11

matthewp85


1 Answers

In my case the error was due to the mediaplayer not having file permissions on the locally stored video. Try storing the video onto the external storage.

like image 78
droiding Avatar answered Jan 10 '23 20:01

droiding