java - Playing music from Parse.com android -


so i'm implementing music playlist app, audios uploaded parse.com mp3 , want retrieve 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 data     }//end if 

this how retrieve music parse.com taking time specially have list of audios .. want way download group of audios in background .. when click button, music play fast .. appreciated.

i have no time right understand why code doesn't work, can take sample app on github (committed now), should solve problem...if not, let me know. please, take note of readme.md

https://github.com/fullwipe/parseaudiofileexample

hope helps...

edit
essential part of repository.
record , 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 called before prepare()                             // prepare: called after start() or before setoutputformat()                             e.printstacktrace();                         } catch (ioexception e) {                             // prepare() fails                             e.printstacktrace();                         } 

when stop recording, save on parse.com in way:

fileinputstream fileinputstream = null;                         file fileobj = new file(outputfile);                         byte[] data = new byte[(int) fileobj.length()];                          try {                             //convert file 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 , play parse.com simple, have used parsequeryadapter. part mp3 file , 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();                     }                     ...                     ... 

Comments