繁体   English   中英

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

[英]how to play all songs from sd card in android programmatically

这是我的代码,现在可以通过使用button (位于listview外部)在列表视图中显示sd卡中的所有歌曲。 我想播放列表中的所有歌曲,通过使用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);
}

在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; } }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM