hybrid
hybrid

Reputation: 1384

Downloaded mp3 file in android throwing IOException

I am using the below code to download an mp3 file from my server to android

public class DownloadService extends IntentService {

    private int result = Activity.RESULT_CANCELED;
    public static final String RESULT = "result";
    public static final String NOTIFICATION = "!@#$%%^";

    public DownloadService() {
        super("DownloadService");
    }

    // will be called asynchronously by Android
    @Override
    protected void onHandleIntent(Intent intent) {
        Integer serverTrackId=intent.getIntExtra(Constants.INTENT_PARAM_SERVER_TRACK_ID, 0);
        String serverUrl=intent.getStringExtra(Constants.INTENT_PARAM_SERVER_TRACK_URL);
        String trackName=intent.getStringExtra(Constants.INTENT_PARAM_SERVER_TRACK_NAME);
        String filePath=intent.getStringExtra(Constants.INTENT_PARAM_ROOT_FILE_PATH);
        Integer localTrackId=intent.getIntExtra(Constants.INTENT_PARAM_LOCAL_TRACK_ID, 0);

        File output = new File(filePath+"/"+trackName);
        if (output.exists()) {
            result = Activity.RESULT_OK;
            publishResults(output.getAbsolutePath(), result);
        }
        else {

            InputStream stream = null;
            FileOutputStream fos = null;
            try {

                URL url = new URL(serverUrl);
                stream = url.openConnection().getInputStream();
                InputStreamReader reader = new InputStreamReader(stream);
                fos = new FileOutputStream(output.getPath());
                int next = -1;
                while ((next = reader.read()) != -1) {
                    fos.write(next);
                }
                // successfully finished
                result = Activity.RESULT_OK;


            } catch (Exception e) {
                e.printStackTrace();
                result = Activity.RESULT_CANCELED;
            } finally {
                if (stream != null) {
                    try {
                        stream.close();
                    } catch (IOException e) {
                        result = Activity.RESULT_CANCELED;
                        e.printStackTrace();
                    }
                }
                if (fos != null) {
                    try {
                        fos.close();
                    } catch (IOException e) {
                        result = Activity.RESULT_CANCELED;
                        e.printStackTrace();
                    }
                }
            }
            publishResults(output.getAbsolutePath(), result);
        }
    }

    private void publishResults(String outputPath, int result) {
try {
    FileInputStream fileInputStream = new FileInputStream(outputPath);

        Intent intent = new Intent(NOTIFICATION);
        intent.putExtra(FILEPATH, outputPath);
        intent.putExtra(RESULT, result);
        sendBroadcast(intent);
}catch(Exception e){
    e.printStackTrace();
}
    }
}

After downloaded broadcast is made , and I try to play the mp3 file by the below code

 if (trackPath != null) {
                FileInputStream fileInputStream = new FileInputStream(trackPath);
                mediaPlayer.setDataSource(fileInputStream.getFD());
            } else {
                AssetFileDescriptor afd = getResources().openRawResourceFd(R.raw.spacer_audio);
                mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
            }
            mediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
            mediaPlayer.setLooping(false);
            mediaPlayer.prepare();
            mediaPlayer.setVolume(1f, 1f);
            mediaPlayer.start();

I get IOException thrown from "mediaPlayer.prepare()"

I tried to play the downloaded music file through android default music player and it shows "cannot play this media".

I tried copying it to computer to try play it and I noticed there is a size difference of several KBs from the original track and the downloaded one.

Please help me find the bug.

Upvotes: 1

Views: 147

Answers (1)

shhp
shhp

Reputation: 3703

You use InputStreamReader to read a binary file, it may produce some unexpected problems. I suggest you use BufferedInputStream instead.

BufferedInputStream reader = new BufferedInputStream(stream);
fos = new FileOutputStream(output.getPath());
int length = -1;
byte[] buffer = new byte[1024 * 8];
while ((length = reader.read(buffer)) != -1) {
    fos.write(buffer, 0, length);
}

Upvotes: 2

Related Questions