Android下载mp3文件并播放它

时间:2016-05-31 15:38:04

标签: android audio

我试图从Dropbox下载一些音频文件,以供下次没有互联网的用户使用,因此代码实际上下载了该文件,但我有麻烦播放该音频,我不知道#39;我知道是否必须解析下载的文件或其他内容,希望你能帮忙

下载文件并播放它,它会在执行后播放或至少尝试

class DownloadFileFromURL extends AsyncTask<String, String, String> {

        /**
         * Before starting background thread Show Progress Bar Dialog
         */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            showDialog(progress_bar_type);
        }

        /**
         * Downloading file in background thread
         */
        @Override
        protected String doInBackground(String... f_url) {
            int count;
            try {
                URL url = new URL(f_url[0]);
                URLConnection conection = url.openConnection();
                conection.connect();

                // this will be useful so that you can show a tipical 0-100%
                // progress bar
                int lenghtOfFile = conection.getContentLength();

                // download the file
                InputStream input = new BufferedInputStream(url.openStream(),
                        8192);

                // Output stream
                OutputStream output = new FileOutputStream(Environment.getExternalStorageDirectory().getPath()+"/workout.mp3");

                byte data[] = new byte[1024];

                long total = 0;

                while ((count = input.read(data)) != -1) {
                    total += count;
                    // publishing the progress....
                    // After this onProgressUpdate will be called
                    publishProgress("" + (int) ((total * 100) / lenghtOfFile));

                    // writing data to file
                    output.write(data, 0, count);
                }

                // flushing output
                output.flush();

                // closing streams
                output.close();
                input.close();

            } catch (Exception e) {
                Log.e("Error: ", e.getMessage());
            }
            return null;
        }
        /**
         * Updating progress bar
         */
        protected void onProgressUpdate(String... progress) {
            // setting progress percentage
            pDialog.setProgress(Integer.parseInt(progress[0]));
        }
        /**
         * After completing background task Dismiss the progress dialog
         **/
        @Override
        protected void onPostExecute(String file_url) {
            // dismiss the dialog after the file was downloaded
            dismissDialog(progress_bar_type);
            Uri u = Uri.parse(Environment.getExternalStorageDirectory().getPath()+"/workout.mp3");

            MediaPlayer mediaPlayer = new MediaPlayer();
            try {
                mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                mediaPlayer.setDataSource(Environment.getExternalStorageDirectory().getPath()+"/workout.mp3");
                mediaPlayer.prepare();
            } catch (IOException e) {
                e.printStackTrace();
            }
            mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mp.start();
                }
            });
        }
    }
实际上我在日志中得到了这个错误

05-31 11:46:11.605 4638-4650/com.example.project.calisthenic E/MediaPlayer: error (1, -2147483648)

问题是尝试播放下载的文件,即使尝试使用Android原生播放器播放它也说明了#34;播放器不支持这种类型的音频文件&#34;,所以我我不知道我是否以错误的方式下载文件,试图以错误的方式播放。

我检查并且文件大小是10kb,下载后是400B,所以下载肯定有问题

1 个答案:

答案 0 :(得分:0)

我不知道问题是否来自它,但你应该在调用prepare函数之前设置prepare listener。

相关问题