views:

1304

answers:

2

I have an application that will record and play audio files. Some of the audio files are downloaded using simple standard http downloads using httpclient. It worked like a charm for a long time. Now all of a sudden I cannot play the files I download. It fails with this stack. I store the files on the SDCard and I experience the problem both on a handset and a USB connected device.

I have checked that the downloaded file is cool on the server, and I can play it without any issues.

These are the code snippets I use ( I know that recordingFile is a valid path for the file).

    // inside the activity class
    private void playRecording() throws IOException{
        File recordingFile = new File(recordingFileName);
        FileInputStream recordingInputStream = new FileInputStream(recordingFile); 
        audioMediaPlayer.playAudio(recordingInputStream);
    }

Here is the media player code:

    // inside my media player class which handles the recordings
    public void playAudio(FileInputStream audioInputStream) throws IOException {
        mediaPlayer.reset();
        mediaPlayer.setDataSource(audioInputStream.getFD());
        mediaPlayer.prepare();
        mediaPlayer.start();
}

Here is the exception:

E/MediaPlayerService(  555): offset error
E/MediaPlayer(  786): Unable to to create media player
W/System.err(  786): java.io.IOException: setDataSourceFD failed.: status=0x80000000
W/System.err(  786):    at android.media.MediaPlayer.setDataSource(Native Method)
W/System.err(  786):    at android.media.MediaPlayer.setDataSource(MediaPlayer.java:632)
W/System.err(  786):    at net.xxx.xxx.AudioMediaPlayer.playAudio(AudioMediaPlayer.java:69)
W/System.err(  786):    at net.xxx.xxx.Downloads.playRecording(Downloads.java:299)
W/System.err(  786):    at net.xxx.xxx.Downloads.access$0(Downloads.java:294)
W/System.err(  786):    at net.xxx.xxx.Downloads$1.onClick(Downloads.java:135)

I have tried seeking some answer of the offset error, but not really clear what this issue might be.

PS I download the file with this code:

    public FileOutputStream executeHttpGet(FileOutputStream fileOutputStream) throws ClientProtocolException, IOException{
        try {     
            // Execute HTTP Post Request  
            httpResponse = httpClient.execute(httpPost, localContext);
            int status = httpResponse.getStatusLine().getStatusCode();

            // we assume that the response body contains the error message
            if (status != HttpStatus.SC_OK) {
                ByteArrayOutputStream ostream = new ByteArrayOutputStream();
                httpResponse.getEntity().writeTo(ostream);
                fileOutputStream = null;
            } else {
                InputStream content = httpResponse.getEntity().getContent();

                byte[] buffer = new byte[1024];
                int len = 0;
                while ( (len = content.read(buffer)) > 0 ) {
                    fileOutputStream.write(buffer,0, len);
                }
                fileOutputStream.close();
                content.close(); // this will also close the connection
            }

        } catch (ClientProtocolException e1) {  
            // TODO Auto-generated catch block 
            e1.printStackTrace();
            fileOutputStream = null;
        } catch (IOException e2) {  
            // TODO Auto-generated catch block  
            e2.printStackTrace();
            fileOutputStream = null;
        }
        return fileOutputStream;
    }
A: 

First, make sure your device is not mounted. Either Android or the host PC can access the SD card, but not both simultaneously.

Second, it is unclear why you are using a FileInputStream and getFD(). Just pass the path to the file on the SD card to the MediaPlayer (e.g., new File(Environment.getExternalStorageDirectory(), "yourfile.mp3")) and let the player open the file.

CommonsWare
Thanks for the answer. Yet I found the issue, which was not related not checking for a null pointer for the fileOutputStream after the call. I have a webserver that keeps different playlists in sync. When i refactored part of the code I made a typo on a hash code I use to allow downloads and not. Unfortunately I didn't have the proper catch when I downloaded the file forcing the file to be empty. Basically I send a bad request header if you try to retrieve a file without a proper activation code.It is now solved and working fine. :-)
Erik
I use the getFD to be sure I can open files, and not have to worry about the permissions. I have found this to be "safer" when working with files on the SDCard.
Erik
+2  A: 

I solved it on my own. As I put it my comment above the solution was this:

When I refactored part of the code I made a typo on a hash code I use to allow downloads and not. Unfortunately I didn't have the proper catch when I downloaded the file forcing the file to be empty. Basically I send a bad request header if you try to retrieve a file without a proper activation code.

The culprit was here:

        if (status != HttpStatus.SC_OK) {
            ByteArrayOutputStream ostream = new ByteArrayOutputStream();
            httpResponse.getEntity().writeTo(ostream);
            fileOutputStream = null;
        } else {
            InputStream content = httpResponse.getEntity().getContent();

            byte[] buffer = new byte[1024];
            int len = 0;
            while ( (len = content.read(buffer)) > 0 ) {
                fileOutputStream.write(buffer,0, len);
            }
            fileOutputStream.close();
           content.close(); // this will also close the connection
    }

For cases where the status code came back a as bad (i.e. bad request header for blocked accesses). What I missed was to capture the case of a null pointer there and that caused a SQLite entry to be updated claiming to the app that the download was successful but yet it wasn't.

Lesson learnt: Always put in the null checks for these cases even for prototypes. :-)

Erik