如何从 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),所以基本上是一个字符串!
如果您想在 Parse.com 上保存 mp3 文件,这是正确的代码:
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();
希望对你有帮助!:)
编辑
我在 github 上上传了一个示例,您可以在 https 中找到它://github.com/fullwipe/ParseAudioFileExample
这篇关于如何从 Parse.com 检索和播放 mp3 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!