0

What i want to do in my project is to play audio songs which are inside my Box account for that i am using box api . As i know we can not provide direct audio streaming for audio files in Box api for that i am trying to implement progressive download and playing audio file from sd card . i know i can play song inside on complete method of download but this is taking more time to download and than playing file . for that what i did i wrote my code for playing audio inside on progress method of downloading file but this method is getting called so many times because of that same song is playing multiple time at a time.

So is there any way to write code for progressive audio playing in Box api .if yes where should i write that ?

* Download a file and put it into the SD card. In your app, you can put the file wherever you have access to.
                 */
                final Box box = Box.getInstance(Constants.API_KEY);
                String PATH = Environment.getExternalStorageDirectory() + "/chaseyourmusic"+folderpath;
                File file = new File(PATH);
                file.mkdirs();
                final java.io.File destinationFile = new java.io.File(PATH + "/"
                        + URLEncoder.encode(items[position].name));
             /*   final java.io.File destinationFile = new java.io.File(Environment.getExternalStorageDirectory() + "/"
                                                                      + URLEncoder.encode(items[position].name));*/

                final ProgressDialog downloadDialog = new ProgressDialog(Browse.this);
                downloadDialog.setMessage("Downloading " + items[position].name);
                downloadDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                downloadDialog.setMax((int) items[position].file.getSize());
                downloadDialog.setCancelable(true);
                downloadDialog.show();

                Toast.makeText(getApplicationContext(), "Click BACK to cancel the download.", Toast.LENGTH_SHORT).show();

                final Cancelable cancelable = box.download(authToken, items[position].id, destinationFile, null, new FileDownloadListener() {

                    @Override
                    public void onComplete(final String status) {
                        downloadDialog.dismiss();
                        if (status.equals(FileDownloadListener.STATUS_DOWNLOAD_OK)) {
                        //Able to play audio here from sd card but this is playing after completion of download only which is taking more time . 


                        }
                        else if (status.equals(FileDownloadListener.STATUS_DOWNLOAD_CANCELLED)) {
                            Toast.makeText(getApplicationContext(), "Download canceled.", Toast.LENGTH_LONG).show();
                        }
                    }

                    @Override
                    public void onIOException(final IOException e) {
                        e.printStackTrace();
                        downloadDialog.dismiss();
                        Toast.makeText(getApplicationContext(), "Download failed " + e.getMessage(), Toast.LENGTH_LONG).show();
                    }

                    @Override
                    public void onProgress(final long bytesDownloaded) {
                         downloadDialog.setProgress((int) bytesDownloaded);
                      //Want to write code here but this method is getting called multiple times which is creating problem in playing audio files from sd card . 


                    }
                });
                downloadDialog.setOnCancelListener(new OnCancelListener() {

                    @Override
                    public void onCancel(DialogInterface dialog) {
                        cancelable.cancel();
                    }
                });

Thanks

seanrose
  • 8,185
  • 3
  • 20
  • 21
krishnendra
  • 99
  • 1
  • 1
  • 14

1 Answers1

1

Use something like these:
http://code.google.com/p/npr-android-app/source/browse/Npr/src/org/npr/android/news/StreamProxy.java?r=41487c03f461942a5747378d197320412fe99442
http://www.java2s.com/Code/Android/File/StreamProxy.htm

Basically for progressive streaming, you proceed with the download as usual (in background) and you run a stream proxy (like a server in background) and push the data to your media player (you can use external media player or write a simple one by yourself, it is only few lines of code with Android media framework)

I have use something very similar with success.
In fact I am using the answer from this post (with minor modification)
MediaPlayer stutters at start of mp3 playback

Community
  • 1
  • 1
tcboy88
  • 1,063
  • 1
  • 9
  • 23