I'm developing an app in which you can record an audio clip, upload it into a database on Parse.com and then retrieve the audio clip from Parse.com and play it.
I'm able to record and upload the audio file on Parse.com, but I don't know what to do about the last step! Here you can see how I save and store the audio clip on Parse.com:
byte[] data = outputFile.getBytes();
//ParseUser currentUser = ParseUser.getCurrentUser();
//String usernameText = currentUser.getUsername();
Calendar c = Calendar.getInstance();
String mUploadName = c.get(Calendar.SECOND) + "_recording.mp3";
ParseFile file = new ParseFile(mUploadName, data);
file.saveInBackground();
// _PostStructure is the class where I've wrote the "set" and "get" methods to use the database on Parse.com
_PostStructure new_audiofile = new _PostStructure();
new_audiofile.setAudioFile(file);
new_audiofile.saveInBackground();
And this is my try to retrieve and play the audio clip from Parse.com:
mediaPlayer = new MediaPlayer();
ParseQuery<ParseObject> query = ParseQuery.getQuery("MyClass");
query.getInBackground("jZAetQnISj", new GetCallback<ParseObject>() {
public void done(ParseObject recording, com.parse.ParseException e) {
if (e != null) {
//do nothing
}
else {
ParseFile audioFile = recording.getParseFile("AudioFile");
String audioFileURL = audioFile.getUrl();
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(audioFileURL);
mediaPlayer.prepare();
mediaPlayer.start();
text.setText("Recording Point: Playing");
finalTime = mediaPlayer.getDuration();
startTime = mediaPlayer.getCurrentPosition();
if(oneTimeOnly == 0){
seekbar.setMax((int) finalTime);
oneTimeOnly = 1;
}
endTimeField.setText(String.format("%d min, %d sec",
TimeUnit.MILLISECONDS.toMinutes((long) finalTime),
TimeUnit.MILLISECONDS.toSeconds((long) finalTime) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.
toMinutes((long) finalTime)))
);
startTimeField.setText(String.format("%d min, %d sec",
TimeUnit.MILLISECONDS.toMinutes((long) startTime),
TimeUnit.MILLISECONDS.toSeconds((long) startTime) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.
toMinutes((long) startTime)))
);
seekbar.setProgress((int)startTime);
myHandler.postDelayed(UpdateSongTime,100);
pauseButton.setEnabled(true);
playButton.setEnabled(false);
} 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();
}
}
}
});
So, when I record an audio clip and save it on Parse.com, everything works fine. When I touch the "play" button, the app doesn't crash, but no sound is played. Can you help me to find the mistake?