0

我想要在我的项目中做的是播放我的 Box 帐户中的音频歌曲,因为我正在使用 box api。据我所知,我们无法为 Box api 中的音频文件提供直接音频流,因为我正在尝试从 sd 卡实现渐进式下载和播放音频文件。我知道我可以在完整的下载方法中播放歌曲,但这需要更多的时间来下载而不是播放文件。为此,我在下载文件的进度方法中编写了用于在其中播放音频的代码,但是由于同一首歌曲一次播放多次,因此该方法被调用了很多次。

那么有什么方法可以编写用于在 Box api 中播放渐进式音频的代码。如果是的话,我应该在哪里写呢?

* 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();
                    }
                });

谢谢

4

1 回答 1

1

使用类似这样的东西: 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

基本上对于渐进式流媒体,您像往常一样继续下载(在后台)并运行流代理(如后台服务器)并将数据推送到您的媒体播放器(您可以使用外部媒体播放器或编写一个简单的你自己,它只是与Android媒体框架的几行代码)

我已经成功地使用了非常相似的东西。
事实上,我正在使用这篇文章的答案(稍作修改)
MediaPlayer 在开始播放 mp3 时出现口吃

于 2014-01-09T14:43:04.190 回答