androidlistactivityaudio-playerandroid-music-player

List activity is taking time to open with a black screen


I am trying open song list after press of a button in a activity which starts the songlist.java activity. However after pressing the button 'btsonglist' button there is some black screen and it takes time to open the list everytime. Also is there any way that the list loads in the background and just shows up after the button is pressed. MainActivity.java

    public class MainActivity extends Activity implements 
    View.OnClickListener{
    Button playbutton, forwardbutton, backwardbutton,songlistbutton;
    ImageView iv;
    Bitmap bmp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        iv = (ImageView)findViewById(R.id.imageView);
        InputStream is = getResources().openRawResource(R.raw.background);
        bmp = BitmapFactory.decodeStream(is);
        iv.setImageBitmap(bmp);
        initiate();


    }
    public void initiate(){
        playbutton = (Button)findViewById(R.id.btplay);
        forwardbutton = (Button)findViewById(R.id.btforward);
        backwardbutton = (Button)findViewById(R.id.btbackward);
        songlistbutton = (Button)findViewById(R.id.btsonglist);

    }
    @Override
    public void onClick(View view) {

        switch (view.getId()){

            case R.id.btplay:
                break;
            case R.id.btforward:
                break;
            case R.id.btbackward:
                break;
            case R.id.btsonglist:
                AsyncTask1 AsyncTask_obj = new AsyncTask1();
                AsyncTask_obj.execute();


        }

    }
    private class AsyncTask1 extends AsyncTask<String,String,String>{
        ProgressDialog progressDialog;

        @Override
        protected String doInBackground(String... strings) {
            try {
                Class ourclass = Class.forName("com.example.musicchatv10.songlist");
                Intent songlistint = new Intent(getApplicationContext(),ourclass);
                startActivityForResult(songlistint, 100);
            }catch(ClassNotFoundException e) {
                e.printStackTrace();

            }
            return null;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
        }
    }
}

This is songlist.java

    public class songlist extends Activity{
    ArrayList<String> arrayList;
    ListView listView;
    ArrayAdapter<String> adapter;
    MediaPlayer mediaPlayer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.song_item);
        listView = (ListView)findViewById(R.id.list);
        arrayList = new ArrayList<>();
        dostuff();

    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    public void dostuff(){
        final ArrayList<File> mysongs;

        mysongs = findsongs(Environment.getExternalStorageDirectory());

    for(int j=0; j<mysongs.size();j++){
        //Toast.makeText(getApplicationContext(),"Loading Files Please wait",Toast.LENGTH_LONG ).show();
        arrayList.add(j,mysongs.get(j).getName().toString());
    }
    adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,arrayList);
    listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (mediaPlayer !=null){
                    mediaPlayer.release();
                }
                else{
                    //int resID = getResources().getIdentifier(arrayList.get(i),"raw",getPackageName());
                    Uri uri = Uri.fromFile(mysongs.get(i));
                    mediaPlayer = MediaPlayer.create(getApplicationContext(),uri );
                    if (mediaPlayer.isPlaying()){
                        mediaPlayer.stop();

                        mediaPlayer.start();
                    }
                    else{

                        mediaPlayer.start();

                    }
                }
            }
        });
}


    public ArrayList<File> findsongs (File root){
            ArrayList<File> al = new ArrayList<File>();
            File[] files = root.listFiles();
            for (File singlefile: files){
                if (singlefile.isDirectory() && !singlefile.isHidden()){
                    al.addAll((findsongs(singlefile)));
                }
                else {
                    if(singlefile.getName().endsWith(".mp3")){
                        al.add(singlefile);
                    }
                }
            }return al;
        }

}

Solution

  • Launching the songlist intent from MainActivity on the background thread with the AsyncTask is not necessary. What you should do instead to make a smoother loading is add an indefinite progress bar to your songlist layout and in onCreate in songlist, show the progress bar and hide the other views. Then launch an AsyncTask that performs your dostuff routine in the background. When that is done (in onPostExecute) you can then hide the progress bar and show the regular views.

    Like this:

    //songlist
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.song_item);
        listView = (ListView)findViewById(R.id.list);
        listView.setVisibility(View.INVISIBLE);
    
        new DoStuffTask(this).execute();
    }
    
    private static class DoStuffTask extends AsyncTask<String,String,String>{
        private WeakReference<songlist> context;
        private WeakReference<ProgressBar> prog;
        private WeakReference<ListView> lv;
    
        DoStuffTask(songlist c) {
            this.context = new WeakReference<>(c);
            this.prog = new WeakReference<>((ProgressBar)c.findViewById(R.id.list_progress));
            this.lv = new WeakReference<>((ListView)c.findViewById(R.id.list));
        }
    
        @Override
        protected void onPreExecute() {
            ProgressBar p = prog.get();
            if( p != null ) {
                p.setVisibility(View.VISIBLE);
            }            
        }
    
        @Override
        protected String doInBackground(String... strings) {
            songlist a = context.get();
    
            if( a != null ) {
                // You may need to have this routine just generate the adapter,
                // (store it in the activity) then actually set the adapter 
                // on the ListView in onPostExecute
                a.dostuff();
            }
            return null;
        }
    
        @Override
        protected void onPostExecute(String s) {
            ProgressBar p = prog.get();
            ListView l = lv.get();
            if( p != null && sl!= null ) {
                p.setVisibility(View.INVISIBLE);
                l.setVisibility(View.VISIBLE);
            }
        }
    }