从Parse.com的Andr​​oid音乐播放 [英] Playing music from Parse.com android

查看:193
本文介绍了从Parse.com的Andr​​oid音乐播放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我喜欢实现音乐播放列表应用程序,我的音频被上传到Parse.com如MP3,我想检索这些音频..

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

这是我如何找回音乐Parse.com但这需要这么多时间特别,我有音频的名单。我希望有一个方法来下载音频组的背景..所以当我点击按钮,音乐播放这么快..任何帮助将不胜AP preciated。

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.

推荐答案

我没有时间,现在明白为什么你的code不起作用,但你可以采取在github上我的示例应用程序(致力于刚才) ,您应该解决您的问题......如果没有,让我知道。请注意该README.md的

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

当你停止录制,将它保存在Parse.com是这样的:

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

检索并播放从Parse.com很简单,我使用了一个ParseQueryAdapter。这是你在​​哪里得到的MP3文件,并发挥它的一部分:

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

这篇关于从Parse.com的Andr​​oid音乐播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆