Search code examples
androidplayback

Play Song continously


I am working on a Music Application , Where I have recorded certain audio files, I can Play the song individually but I have a button Play all on which I need to play all the Song present in that particular folder

Can anyone suggest the best way to achieve the task

Thanks in Advance


Solution

  • Hello Guys I Got the answer to my question finally

    Please check the below code:

       public class ShowAllRecords extends ListActivity {
        private File file;
        private List<String> myList;
        MediaPlayer mp;
        ListView listv;
        int currentposition = 0;
    
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            mp = new MediaPlayer();
            myList = new ArrayList<String>();
            listv = getListView();
            listv.setBackgroundResource(R.drawable.musicapp);
            String root_sd = Environment.getExternalStorageDirectory().toString();
            file = new File(root_sd + "/mymusicapp/");
            File list[] = file.listFiles();
    
            for (int j = 0; j < list.length; j++) {
                myList.add(list[j].getName());
            }
    
            listv.setAdapter(new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_1, myList));
    
        }
    
        protected void onListItemClick(ListView l, View v, int position, long id) {
            super.onListItemClick(l, v, position, id);
    
            File temp_file = new File(file, myList.get(position));
    
            if (!temp_file.isFile()) {
                file = new File(file, myList.get(position));
                File list[] = file.listFiles();
    
                myList.clear();
    
                for (int i = 0; i < list.length; i++) {
                    myList.add(list[i].getName());
                }
    
                Toast.makeText(getApplicationContext(), file.toString(), Toast.LENGTH_LONG).show();
    
    
                //+l.getAdapter().getItem(position));
                listv.setAdapter(new ArrayAdapter<String>(this,
                        android.R.layout.simple_list_item_1, myList));
                playSong(file.toString() + "/" + myList.get(currentposition));
    
    
                l.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        System.out.println("path is :--" + file.toString() + "/" + myList.get(position));
    
                    }
                });
    //
            }
    
        }
    
        private void playSong(String Songpath) {
            try {
                mp.reset();
                mp.setDataSource(Songpath);
                mp.prepare();
                // mp.setLooping(true);
                mp.start();
    
                mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer mediaPlayer) {
                        Toast.makeText(getApplicationContext(), "Media Completed with Success", Toast.LENGTH_SHORT).show();
                        System.out.println("In Oncompletion");
                        try {
                            currentposition++;
                            //   mediaPlayer.reset();
                            if (currentposition <=myList.size()) {
    
                                System.out.println("in if of postion");
                                mediaPlayer = new MediaPlayer();
                                mediaPlayer.reset();
                                mediaPlayer.setDataSource(file.toString() + "/"+myList.get(currentposition));
                                System.out.println("FIle Path on Completion :--"+file.toString() + myList.get(currentposition));
                                mediaPlayer.prepare();
                                // mp.setLooping(true);
                                mediaPlayer.start();
                            }
    
    
                            // playSong(file.toString() + myList.get(currentposition));
                            System.out.println("path is :--" + file.toString() + currentposition);
    
                            //  nextSong();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
    
                });
    
            } catch (IOException e) {
                Log.v(getString(R.string.app_name), e.getMessage());
            }
        }
    }