繁体   English   中英

从Parse.com android播放音乐

[英]Playing music from Parse.com android

所以我实现了像音乐播放列表应用一样的功能,我的音频以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();
                    }
                    ...
                    ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM