Playing music from Parse.com android

淺唱寂寞╮ 提交于 2020-01-05 14:11:10

问题


so I'm implementing like music playlist app, my audios are uploaded to Parse.com as mp3 , I want to retrieve those audios ..

  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.


回答1:


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. Please, take note of the README.md

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:

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. This is the part where you get the mp3 file and play it:

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


来源:https://stackoverflow.com/questions/31340046/playing-music-from-parse-com-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!