java - Downloading list of audio from URL -


so i'm implementing audio playlist app imports audios parse.com, , when retrieve them streaming them directly urls, took maybe 1 minute play after clicking play button. don't want use streaming url methods play audio server because makes slow. instead want download list of audios server (parse.com) @ once , playing them retrieving sdcard.

i have class downloads single audio single url .. want edit code make downloads more 1 audio @ once.

here class have download audio url.

class downloadmusicfrominternet extends asynctask<string, string, string> {  private context mcontext; private mediaplayer mplayer;    public downloadmusicfrominternet(context context) {     mcontext = context; }    @override protected void onpreexecute() {     super.onpreexecute();  }  // download music file internet @override protected string doinbackground(string... f_url) {     int count;     try {        url url = new url(f_url[0]);         urlconnection conection = url.openconnection();         conection.connect();         // music file length         int lenghtoffile = conection.getcontentlength();         // input stream read file - 8k buffer         inputstream input = new bufferedinputstream(url.openstream(), 10 * 1024);         // output stream write file in sd card         outputstream output = new  

also if me in how set name audios, don't want write static name because i'm downloading more 1 audio @ time .

fileoutputstream(environment.getexternalstoragedirectory().getpath() + "/"+"how put unique name each audio here ?"+".mp3");             byte data[] = new byte[1024];             long total = 0;             while ((count = input.read(data)) != -1) {                 total += count;                 publishprogress("" + (int) ((total * 100) / lenghtoffile));                  // write data file      output.write(data, 0, count);         }         // flush output         output.flush();         // close streams         output.close();         input.close();     } catch (exception e) {         log.e("error: ", e.getmessage());     }     return null; }  // while downloading music file   // once music file downloaded @override protected void onpostexecute(string file_url) {     // play music     playmusic(); }   // play music protected void playmusic() {     // read mp3 file present under sd card       uri myuri1 = uri.parse("file:///sdcard/"+"the same unique name set ! "+".mp3");     mplayer = new mediaplayer();     mplayer.setaudiostreamtype(audiomanager.stream_music);     try {         mplayer.setdatasource(mcontext.getapplicationcontext(), myuri1);         mplayer.prepare();         // start playing music file         mplayer.start();         mplayer.setoncompletionlistener(new mediaplayer.oncompletionlistener() {             public void oncompletion(mediaplayer mp) {                 // todo auto-generated method stub                 // once music completed playing, enable button                 // btnplaymusic.setenabled(true);                 toast.maketext(mcontext.getapplicationcontext(), "music completed playing", toast.length_long).show();             }         });     } catch (illegalargumentexception e) {         toast.maketext(mcontext.getapplicationcontext(), "you might not set uri correctly!", toast.length_long).show();     } catch (securityexception e) {         toast.maketext(mcontext.getapplicationcontext(), "uri cannot accessed, permissed needed", toast.length_long).show();     } catch (illegalstateexception e) {         toast.maketext(mcontext.getapplicationcontext(), "media player not in correct state", toast.length_long).show();     } catch (ioexception e) {         toast.maketext(mcontext.getapplicationcontext(), "io error occured", toast.length_long).show();     }     } } 

and adapter, , here problem occurs , crashes app. don't know how call ( downloadmusicfrominternet ) more once following adapter .

public class madapter extends parsequeryadapter<parseobject> {    public madapter (context context) {      super(context, new parsequeryadapter.queryfactory<parseobject>() {         public parsequery create() {             parsequery query = new parsequery("musicplaylist");             return query;         }     }); }  @override public view getitemview(final parseobject object, view v, viewgroup parent) {      if (v == null) {         v = view.inflate(getcontext(), r.layout.list_music, null);     }      super.getitemview(object, v, parent);        //audio retrieving     final imagebutton btn = (imagebutton) v.findviewbyid(r.id.play_btn);     final parsefile fileobject = object.getparsefile("music");  **// here put loop because want download list of audios @ once , think here problem**      if (fileobject != null) {         (int = 10; >= j; j++) {             new downloadmusicfrominternet().execute(fileobject.geturl());         }          fileobject.getdatainbackground(new getdatacallback() {               @override             public void done(byte[] bytes, com.parse.parseexception e) {                  btn.setonclicklistener(new view.onclicklistener() {                      public void onclick(view arg0) {                       }//end on click                 }//end listener                 );              }//end done         });//end data       }//end if          return v;     }//end getitem view   }//end madapter 

any appreciated ..


Comments

Popular posts from this blog

searchKeyword not working in AngularJS filter -

sequelize.js - Sequelize: sort by enum cases -

user interface - how to replace an ongoing process of image capture from another process call over the same ImageLabel in python's GUI TKinter -