如何从Android中的应用程序访问音乐播放器中的曲目?我必须以某种方式导入所有曲目,并通过蓝牙连接在其他设备上播放。
答案 0 :(得分:1)
好的,使用下面的代码从SD卡中获取列表视图中的所有音乐文件。
public class Recording_List extends Activity{
ListView mListView;
ImageView mPlayPause;
private File file;
private MediaPlayer mp = new MediaPlayer();
private Handler mHandler = new Handler();
private List<String> myList = new ArrayList<String>();
private static final String MEDIA_PATH = new String(
Environment.getExternalStorageDirectory() + "/AudioRecorder/");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recordlist);
mListView = (ListView) findViewById(R.id.recordList);
mPlayPause = (ImageView) findViewById(R.id.play_pause);
file = new File(MEDIA_PATH);
if (!file.exists()) {
file.mkdir();
}
File list[] = file.listFiles();
for (int i = 0; i < list.length; i++) {
myList.add(list[i].getName());
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, myList);
adapter.notifyDataSetChanged();
mListView.setAdapter(adapter);
mListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
mListView.setCacheColorHint(Color.TRANSPARENT);
mListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
playSong(MEDIA_PATH + myList.get(position));
}
});
mPlayPause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
if (mp.isPlaying()) {
if (mp != null) {
mp.pause();
mPlayPause.setImageResource(R.drawable.play);
}
} else {
if (mp != null) {
mp.start();
mPlayPause.setImageResource(R.drawable.pause);
}
}
}
});
}
private void playSong(String songPath) {
try {
mp.reset();
mp.setDataSource(songPath);
mp.prepare();
mp.start();
} catch (IOException e) {
Log.v(getString(R.string.app_name), e.getMessage());
}
}
}
所以基本上上面的代码会列出列表视图中的所有文件,从SD卡文件夹名称录音机,您可以在那里指定您的路径并获取您的所有音频文件。当您点击列表视图项时,它将播放该歌曲。