如何检索和Parse.com播放MP3文件 [英] How to retrieve and play mp3 files from Parse.com
问题描述
我开发一个应用程序,你可以在其中记录音频剪辑,上传到上Parse.com一个数据库,然后检索Parse.com音频剪辑和播放。
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.
我能够录制和上传的音频文件上Parse.com,但我不知道该怎么做了最后一步!在这里,你可以看到我是如何保存和音频剪辑存储在Parse.com:
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();
这是我尝试检索和Parse.com播放音频剪辑:
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();
}
}
}
});
所以,当我录制音频剪辑,并将其保存在Parse.com,一切工作正常。当我触摸播放按钮,应用程序不会崩溃,但没有播放声音。你能帮我找到错误?
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?
推荐答案
最后,我已经得到了解决! :)这是我的错误:
Finally I've got the solution! :) This was my mistake:
byte[] data = outputFile.getBytes();
这是我想转换成字节数组(OUTPUTFILE)的目的是不是MP3文件,但该文件(/storage/sdcard0/audiocapturetest.mp3)的资源路径,所以它基本上是一个字符串!< BR>如果你想节省Parse.com MP3文件,这是正确的code:
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! :)
修改
我上传的GitHub上的例子,你可以在 HTTPS发现://github.com/fullwipe/ParseAudioFileExample
这篇关于如何检索和Parse.com播放MP3文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!