问题
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?
回答1:
Finally I've got the solution! :) This was my mistake:
byte[] data = outputFile.getBytes();
The object that I was trying to convert into a byte array (outputFile) was not the mp3 file, but the resource path of the file (/storage/sdcard0/audiocapturetest.mp3), so it was basically a string!
If you want to save an mp3 file on Parse.com, this is the right code:
FileInputStream fileInputStream = null;
File fileObj = new File(outputFile);
byte[] data = new byte[(int) fileObj.length()];
try {
//convert file into array of bytes
fileInputStream = new FileInputStream(fileObj);
fileInputStream.read(data);
fileInputStream.close();
} catch(Exception e) {
e.printStackTrace();
}
Calendar c = Calendar.getInstance();
mUploadName = c.get(Calendar.SECOND) + "_recording.mp3";
ParseFile file = new ParseFile(mUploadName, data);
file.saveInBackground();
_PostStructure new_audiofile = new _PostStructure();
new_audiofile.setNome(mUploadName);
new_audiofile.setAudioFile(file);
new_audiofile.saveInBackground();
I hope it can be helpful to you! :)
EDIT
I uploaded an example on github, you can find it at https://github.com/fullwipe/ParseAudioFileExample
来源:https://stackoverflow.com/questions/27219822/how-to-retrieve-and-play-mp3-files-from-parse-com