0

Im downloading files from a server, some big, some small.

protected Long doInBackground(Object... params) {
        Context context = (Context) params[0];
        String name = (String) params[1];
        String urlString = (String) params[2];
        File mediadir = context.getDir("tvr", Context.MODE_PRIVATE);
        try {
            URL url = new URL(urlString);
            URLConnection connection = url.openConnection();
            connection.connect();
            int lenghtOfFile = connection.getContentLength();
            InputStream is = url.openStream();
            Log.d("DOWNLOAD NAME",name);
            File new_file = new File(mediadir, name);
            FileOutputStream fos = new FileOutputStream(new_file.getPath());
            byte data[] = new byte[1024];
            int count = 0;
            long total = 0;
            int progress = 0;
            while ((count=is.read(data)) != -1){
                total += count;
                int progress_temp = (int)total*100/lenghtOfFile;
                if(progress_temp%10 == 0 && progress != progress_temp){
                    progress = progress_temp;
                }
                fos.write(data, 0, count);
            }
            is.close();
            fos.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            Log.e("DOWNLOAD ERROR", e.toString());
        }
        return null;
    }

As I now understand it, Im downloading the file via a stream and streaming the data into a file. So in effect the file is created since the first byte of data.

Now, on the other end of things Im playing files from my internal directory by looking for files in a directory and then playing them:

mediaPlayer = new MediaPlayer();
mediaPlayer.setOnCompletionListener(this);
mediaPlayer.setDisplay(holder);
FileInputStream fileInputStream = new FileInputStream(path);
mediaPlayer.setDataSource(fileInputStream.getFD());
fileInputStream.close();
mediaPlayer.prepare();
mediaPlayer.start();

Now this issue is that the file will be played even though its not fully downloaded!

Is this true? If so, how must I check if the file is complete before trying to play it?

Harry
  • 13,091
  • 29
  • 107
  • 167

1 Answers1

3

Yes, this is a valid scenario, as in many cases your download speed will be slower than the reading and playing speed of the MediaPlayer.

Seeing as you're using an AsyncTask, you can fix this problem by calling the playing code in the onPostExecute() method, as that only runs after all work in the doInBackground() method has completed.

Raghav Sood
  • 81,899
  • 22
  • 187
  • 195
  • What about creating the streaming file in a temp directory, the after the stream is close, I move it to the real directory and then delete them temp directory file? – Harry Feb 26 '13 at 13:32
  • @Harry That would work too, but it would really be a waste of file I/O and disc operations. – Raghav Sood Feb 26 '13 at 13:33
  • Ok, Ill try what you suggest. Its gonna be allot of moving code around :-) – Harry Feb 26 '13 at 13:35
  • @Harry Just put the code in a method, and call the method from the onPostExecute(). That way if this doesn't work for you for some reason, you can just shift the method call instead of chunks of code – Raghav Sood Feb 26 '13 at 13:36