2016-02-22 166 views
0

我在音樂應用,在那裏我有記錄某些音頻文件時, 我也可以單獨播放的歌曲,但我有一個按鈕播放所有關於這一點我需要在特定的文件夾播放所有歌曲本播放歌曲汽車無

任何人都可以提出來實現任務提前

感謝

+1

創建該文件夾中的所有歌曲的列表,並在當前歌曲即將結束,開始支付另一個,直到列表達到其最後一首歌曲。 – Rohit5k2

+0

您好我已經試過這個東西,但我的循環是位置不OnCompletion –

+0

增加你需要發佈您的代碼。 – Rohit5k2

回答

2

你好,大家好我得到的回答我的問題終於

請檢查下面的代碼:

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

您可以保存所有歌曲的路徑,該文件夾中的一個ArrayList(一件容易的事)的最佳方式。

在您的mediaplayer對象上添加oncompletionlistener()。

而且在第一首歌曲完成時播放另一首歌曲。

簡單任務兄弟!