简体   繁体   English

从Parse.com android播放音乐

[英]Playing music from Parse.com android

so I'm implementing like music playlist app, my audios are uploaded to Parse.com as mp3 , I want to retrieve those audios .. 所以我实现了像音乐播放列表应用一样的功能,我的音频以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

this is how I retrieve music from Parse.com but this is taking so much time specially that I have a list of audios .. I want a way to download group of the audios in the background .. so when I click the button, the music play so fast .. any help would be greatly appreciated. 这是我从Parse.com检索音乐的方式,但是这特别耗时,以至于我有一个音频列表..我想要一种在后台下载音频组的方法..因此,当我单击按钮时,音乐播放如此之快..任何帮助将不胜感激。

I have no time right now to understand why your code doesn't work, but you can take my sample app on github (committed just now), you should solve your problem...if not, let me know. 我现在没有时间去理解为什么您的代码不起作用,但是您可以将我的示例应用程序放在github(刚刚提交)上,您应该解决问题...如果没有,请告诉我。 Please, take note of the README.md 请注意README.md

https://github.com/fullwipe/ParseAudioFileExample https://github.com/fullwipe/ParseAudioFileExample

Hope it helps... 希望能帮助到你...

Edit 编辑
This is the essential part of my repository. 这是我的存储库的必要部分。
Record and save: 记录并保存:

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

Then, start recording... 然后,开始录制...

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

When you stop recording, save it on Parse.com in this way: 停止记录时,请按以下方式将其保存在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();
                                }
                            }
                        });

Retrieve and play from Parse.com is very simple, I have used a ParseQueryAdapter. 从Parse.com检索和播放非常简单,我使用了ParseQueryAdapter。 This is the part where you get the mp3 file and play it: 这是您获得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