我正在开发一个应用程序,您可以在其中录制音频剪辑,将其上传到Parse.com上的数据库,然后从Parse.com检索音频剪辑并进行播放。
我能够在Parse.com上录制和上传音频文件,但我不知道如何处理最后一步!在这里,您可以看到我如何在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检索和播放音频片段:
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上时,一切正常。当我触摸&#34;播放&#34;按钮,应用程序不会崩溃,但没有播放声音。你能帮我找到错误吗?
答案 0 :(得分:5)
最后我得到了解决方案! :)这是我的错误:
byte[] data = outputFile.getBytes();
我试图转换为字节数组(outputFile)的对象不是mp3文件,而是文件的资源路径(/storage/sdcard0/audiocapturetest.mp3),所以它基本上是一个字符串!<如果你想在Parse.com上保存一个mp3文件,这是正确的代码:
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找到它