如何以编程方式在android中播放sd卡中的所有歌曲

时间:2013-12-30 06:25:41

标签: android

这是我的代码,在列表视图中显示sd卡中的所有歌曲,现在使用button(在listview之外)。我想播放列表中的所有歌曲,通过使用onCompletion听众,我们可以实现这一点但是没有得到如何做到这一点,请帮助我。感谢。

public class ListFileActivity extends ListActivity implements OnClickListener,
    OnCompletionListener, OnItemClickListener {

public String path;
MediaPlayer mda;
int current_index2 = 0;
String filename;
Button backToRecord, allvoices, btnpause, btnplay;
ToggleButton activateDelete;
ListView myList;
List values;
ArrayAdapter adapter;
MediaPlayerActivity mp = new MediaPlayerActivity();
private SongsManager songManager;
private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
public int currentSongIndex = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.soundrecords);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    backToRecord = (Button) findViewById(R.id.buttonrecord);
    activateDelete = (ToggleButton) findViewById(R.id.buttonedit);
    allvoices = (Button) findViewById(R.id.buttoncontinuous);
    btnpause = (Button) findViewById(R.id.buttonpause);
    btnplay = (Button) findViewById(R.id.buttonplay);

    myList = (ListView) findViewById(android.R.id.list);
    backToRecord.setOnClickListener(this);
    activateDelete.setOnClickListener(this);
    allvoices.setOnClickListener(this);
    btnpause.setOnClickListener(this);
    btnplay.setOnClickListener(this);

    myList.setOnItemClickListener(this);
    // Use the current directory as title
    path = "/sdcard/";
    if (getIntent().hasExtra("path")) {
        path = getIntent().getStringExtra("path");
    }
    setTitle(path);
    // Read all files sorted into the values-array
    values = new ArrayList();
    File dir = new File(path);
    if (!dir.canRead()) {
        setTitle(getTitle() + " (inaccessible)");
    }
    final String[] list = dir.list();
    if (list != null) {
        for (String file : list) {
            if (file.contains(".3gp")) {
                values.add(file);
            }
        }
    }
    Collections.sort(values);
    // Put the data into the list
    adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_2,
            android.R.id.text1, values);
    setListAdapter(adapter);
    registerForContextMenu(myList);
}

1 个答案:

答案 0 :(得分:0)

在onCompletion方法中使用以下代码。

    if(isRepeat){ // repeat is on play
      playSong(currentSongIndex); 
      } else if(isShuffle){ //  shuffle is on - play a random song 
      Random rand = new Random();
      currentSongIndex = rand.nextInt((songsList.size() - 1) - 0 + 1) + 0;
      playSong(currentSongIndex); } else{ // no repeat or shuffle ON - play next song 
      if(currentSongIndex < (songsList.size() - 1)){
      playSong(currentSongIndex + 1); 
      currentSongIndex = currentSongIndex + 1; 
      }else{ // play first song //
      playSong(0); currentSongIndex = 0; } }