0

I have an application that records audio and saves it on the server on Parse. I'm trying to retrieve it to play the recorded audio back.
The server is in between because the app sends the audio from one user to another. I'm just a bit confused as to how the ParseFile actually works and what's the best way to play it back.
At the moment I'm querying the object, get it's audio property as a ParseFile, get the url then stream that from the server.

// result is the object I got from the query, it holds the ParseFile
ParseFile audioFile = result.getAudioFile();
String audioFileURL = audioFile.getUrl();
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setAudioStreamType(android.media.AudioManager.STREAM_MUSIC);
try {
      mMediaPlayer.setDataSource(audioFileURL);
      mMediaPlayer.prepare();
      mMediaPlayer.start();
}catch (Exception e){}

I'm thinking this isn't the best way to do it, and it's probably better to save it locally. I know you can use getDataInBackground, but I'm just confused as to what that does to the actual ParseFile. Once I get the data in the background, as an array of byte, what is the best way to store that so that I don't have to call getDataInBackground everytime? Should I overwrite the ParseFile with a new one I make from the data I get in getDataInBackground?

Thanks.

EscapeArtist
  • 776
  • 8
  • 15

1 Answers1

0

from a question of kurtis92 here: How to retrieve and play mp3 files from Parse.com

MediaPlayer mediaPlayer;

public void play(View view) {  // function onClick of a button
        mediaPlayer = new MediaPlayer();
        ParseQuery<ParseObject> query = ParseQuery.getQuery("sveglia"); // sveglia is the name of the Class
        query.getInBackground("hQHeNCqccH", new GetCallback<ParseObject>() { //hQHeNCqccH is the ID of the file audio u wanna stream
            public void done(ParseObject recording, com.parse.ParseException e) {
                if (e != null) {
                     //do nothing
                } else {
                     ParseFile audioFile = recording.getParseFile("audio"); // audio is the column of the file audio
                      String audioFileURL = audioFile.getUrl();
                      mediaPlayer = new MediaPlayer();
                      mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                      try {
                          mediaPlayer.setDataSource(audioFileURL);
                          mediaPlayer.prepare();
                          mediaPlayer.start();

                      } catch (IllegalArgumentException e1) {
                          // TODO Auto-generated catch block
                          e1.printStackTrace();
                      } catch (SecurityException e1) {
                          // TODO Auto-generated catch block
                          e1.printStackTrace();
                      } catch (IllegalStateException e1) {
                          // TODO Auto-generated catch block
                          e1.printStackTrace();
                      } catch (IOException e1) {
                          // TODO Auto-generated catch block
                          e1.printStackTrace();
                      }
                  }
             }
         });
      }
Community
  • 1
  • 1
Manza
  • 86
  • 1
  • 8