gpt4 book ai didi

java - 从 Parse.com android 播放音乐

转载 作者:行者123 更新时间:2023-11-29 20:37:22 25 4
gpt4 key购买 nike

所以我正在实现类似音乐播放列表的应用程序,我的音频以 mp3 的形式上传到 Parse.com,我想检索这些音频..

  final Button btn = (Button) v.findViewById(R.id.button);
final ParseFile fileObject = object.getParseFile("music");
if (fileObject != null) {
fileObject.getDataInBackground(new GetDataCallback() {


@Override
public void done(byte[] bytes, com.parse.ParseException e) {

final String audioFileURL = fileObject.getUrl();
mediaPlayer = new MediaPlayer();
mediaPlayer.reset();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
btn.setOnClickListener(new OnClickListener() {

public void onClick(View arg0) {
try {
mediaPlayer.setDataSource(audioFileURL);
mediaPlayer.prepare();
mediaPlayer.start();
} catch (IllegalArgumentException e1) {
e1.printStackTrace();
}//end catch
catch (IOException e1) {
e1.printStackTrace();
}
}//end on click
}//end listener
);
}//end done
});//end get data
}//end if

这就是我从 Parse.com 检索音乐的方式,但这特别花费了很多时间,以至于我有一个音频列表..我想要一种在后台下载音频组的方法..所以当我点击按钮,音乐播放速度如此之快..任何帮助将不胜感激。

最佳答案

我现在没有时间了解为什么你的代码不起作用,但你可以在 github 上获取我的示例应用程序(刚刚提交),你应该解决你的问题......如果没有,请告诉我。请注意 README.md

https://github.com/fullwipe/ParseAudioFileExample

希望对你有帮助

编辑
这是我存储库的重要部分。
记录并保存:

String outputFile = Environment.getExternalStorageDirectory().
getAbsolutePath() + "/rumor.mp3";
myRecorder = new MediaRecorder();
myRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
myRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
myRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
myRecorder.setOutputFile(outputFile);

然后,开始录制...

try {
myRecorder.prepare();
myRecorder.start();
} catch (IllegalStateException e) {
// start:it is called before prepare()
// prepare: it is called after start() or before setOutputFormat()
e.printStackTrace();
} catch (IOException e) {
// prepare() fails
e.printStackTrace();
}

当你停止录制时,以这种方式保存在 Parse.com 上:

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();
}

ParseFile parseAudioFile = new ParseFile("audiofile.mp3", data);
parseAudioFile.saveInBackground();

ParseObject parseObject = new ParseObject("AudioFileClass");
parseObject.put("audiofile", parseAudioFile);
parseObject.saveInBackground(new SaveCallback() {
public void done(ParseException e) {
if (e == null) {
Toast.makeText(getApplicationContext(),"Audio file saved successfully", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(),"Error: audio file not saved", Toast.LENGTH_SHORT).show();
}
}
});

从 Parse.com 检索和播放非常简单,我使用了一个 ParseQueryAdapter。这是您获取 mp3 文件并播放它的部分:

ParseFile descr = object.getParseFile("audiofile");
if (descr != null) {
String audioFileURL = descr.getUrl();
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

try {
mediaPlayer.setDataSource(audioFileURL);
mediaPlayer.prepare();
mediaPlayer.start();
}
...
...

关于java - 从 Parse.com android 播放音乐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31340046/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com