下载的歌曲不是从SD卡播放的?

时间:2014-02-05 07:24:11

标签: java android soundcloud

我可以在SD卡中从Sound Cloud下载声音文件。我可以将它设置为我的铃声,它正常工作。但是当我试图从SD卡播放它时,我得到“文件不支持”消息,音乐没有播放。我想我没有得到文件扩展名,这就是音乐没有播放的原因请帮助我。

    case PARSE_GET_TRACK:
    ...
    case PARSE_DOWNLOAD_TRACK:
                String fileName = mTrack.getTrackTitle()
                        + mTrack.getTrackId();
                String path = mTrack.getDownloadUrl()
                        +"?client_id=xxx";


                URL u = new URL(path);

                httpRequest = new HttpGet(u.toURI());
                httpclient = new DefaultHttpClient();
                response = (HttpResponse) httpclient.execute(httpRequest);

                HttpURLConnection c = (HttpURLConnection) u
                        .openConnection();
                c.setRequestMethod("GET");
                c.setDoInput(true);
                c.setDoOutput(true);
                c.connect();


                File sdCard = Environment.getExternalStorageDirectory();
                File dir = new File(sdCard.getAbsolutePath() + "/dir1/dir2");
                dir.mkdirs();
                File file = new File(dir, fileName);

                mTrack.setAboluteFilePath(file.getAbsolutePath());

                boolean redirect = false;

                int status = c.getResponseCode();
                if (status != HttpURLConnection.HTTP_OK) {
                    if (status == HttpURLConnection.HTTP_MOVED_TEMP
                            || status == HttpURLConnection.HTTP_MOVED_PERM
                            || status == HttpURLConnection.HTTP_SEE_OTHER)
                        redirect = true;

                }

                if (redirect) {


                    String newUrl = c.getHeaderField("Location");

                    c = (HttpURLConnection) new URL(newUrl)
                            .openConnection();

                }

                InputStream inputStream = response.getEntity().getContent();
                InputStreamReader inputStreamReader = new InputStreamReader(
                        inputStream);

                BufferedReader bufferedReader = new BufferedReader(
                        inputStreamReader);

                BufferedInputStream bis = new BufferedInputStream(
                        inputStream, 1024 * 50);
                FileOutputStream fos = new FileOutputStream(file);
                byte[] buffer = new byte[1024 * 50];

                int current = 0;
                while ((current = bis.read(buffer)) != -1) {
                    fos.write(buffer, 0, current);
                }
                fos.flush();
                StringBuilder stringBuilder = new StringBuilder();

                String bufferedStrChunk = null;

                while ((bufferedStrChunk = bufferedReader.readLine()) != null) {
                    stringBuilder.append(bufferedStrChunk);
                }

                fos.close();
                bis.close();
                return stringBuilder.toString();

            case PARSE_GET_VID_THUMB:
  ....

1 个答案:

答案 0 :(得分:0)

是的,尝试了一些不同的方式,我解决了它。您只需要更改Track Id的字符串。

 String fileName = mTrack.getTrackTitle()
                    + mTrack.getTrackId()+ ".3gp";