从Parse.com android播放音乐

时间:2015-07-10 11:50:06

标签: java android audio parse-platform parse-android-sdk

所以我正在实施类似音乐的播放列表应用,我的音频作为mp3上传到Parse.com,我想要检索那些音频..

get page http://localhost/tmp/1.html
    url changed to http://localhost/tmp/2.html
get page http://localhost/tmp/2.html
    url changed to http://localhost/tmp/3.html
get page http://localhost/tmp/3.html
    url changed to http://localhost/tmp/4.html
get page http://localhost/tmp/4.html
    url changed to http://localhost/tmp/5.html
get page http://localhost/tmp/5.html

started with http://localhost/tmp/1.htmlthen
    http://localhost/tmp/2.html, then
    http://localhost/tmp/3.html, then
    http://localhost/tmp/4.html, then
    http://localhost/tmp/5.html

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

1 个答案:

答案 0 :(得分:2)

我现在没时间理解为什么你的代码不起作用,但你可以把我的示例应用程序放在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();
                    }
                    ...
                    ...