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

如何在android中以编程方式播放sd卡中的所有歌曲,android,Android,下面是我的代码,可以在列表视图中显示sd卡中的所有歌曲,现在可以使用按钮(在listview之外)。我想播放列表中的所有歌曲,通过使用onCompletionlistener,我们可以实现这一点,但不知道如何实现,请帮助我。谢谢 public class ListFileActivity extends ListActivity implements OnClickListener, OnCompletionListener, OnItemClickListener { public

下面是我的代码,可以在列表视图中显示sd卡中的所有歌曲,现在可以使用
按钮
(在listview之外)。我想播放列表中的所有歌曲,通过使用
onCompletion
listener,我们可以实现这一点,但不知道如何实现,请帮助我。谢谢

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);
}
公共类ListFileActivity扩展ListActivity实现OnClickListener,
OnCompletionListener,OnItemClickListener{
公共字符串路径;
媒体播放器mda;
int电流_index2=0;
字符串文件名;
按钮返回到录制、所有语音、btnpause、btnplay;
切换按钮激活删除;
列表视图myList;
列表值;
阵列适配器;
MediaPlayerActivity mp=新的MediaPlayerActivity();
私人歌曲经理;
private ArrayList songsList=new ArrayList();
公共int currentSongIndex=0;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.soundrecords);
setRequestedOrientation(ActivityInfo.SCREEN\u ORIENTATION\u Picture);
backToRecord=(按钮)findViewById(R.id.buttonrecord);
activateDelete=(ToggleButton)findViewById(R.id.buttonedit);
allvoices=(按钮)findViewById(R.id.ButtonConcontinuous);
btnpause=(按钮)findViewById(R.id.buttonpause);
btnplay=(按钮)findViewById(R.id.buttonlay);
myList=(ListView)findViewById(android.R.id.list);
backToRecord.setOnClickListener(此);
activateDelete.setOnClickListener(此);
allvoices.setOnClickListener(此);
btnpause.setOnClickListener(此);
btnplay.setOnClickListener(此);
myList.setonicmclicklistener(this);
//使用当前目录作为标题
path=“/sdcard/”;
if(getIntent().hasExtra(“路径”)){
path=getIntent().getStringExtra(“路径”);
}
设置标题(路径);
//读取排序到值数组中的所有文件
值=新的ArrayList();
文件目录=新文件(路径);
如果(!dir.canRead()){
setTitle(getTitle()+“(不可访问)”;
}
最后一个字符串[]list=dir.list();
如果(列表!=null){
用于(字符串文件:列表){
if(file.contains(“.3gp”)){
添加(文件);
}
}
}
集合。排序(值);
//将数据放入列表中
adapter=new ArrayAdapter(这是android.R.layout.simple\u list\u item\u 2,
android.R.id.text1,值);
setListAdapter(适配器);
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; } }
if(isRepeat){//repeat正在播放
播放歌曲(当前歌曲索引);
}否则,如果(isShuffle){//shuffle打开,则播放一首随机歌曲
Random rand=新的Random();
currentSongIndex=rand.nextInt((songsList.size()-1)-0+1)+0;
playSong(currentSongIndex);}否则{//不重复或随机播放-播放下一首歌曲
如果(当前歌曲索引<(歌曲列表大小()-1)){
播放歌曲(当前歌曲索引+1);
currentSongIndex=currentSongIndex+1;
}否则{//播放第一首歌//
播放歌曲(0);currentSongIndex=0;}

我想在点击按钮时播放音频文件,如何在点击按钮时播放该文件?我已经通过蓝牙发送了该文件,这意味着我将意图传递给mediaSTore,但这不是gud的替代方案。提前感谢