【发布时间】:2015-09-29 11:36:46
【问题描述】:
ListActivity 在录制活动和新活动中尝试从 SD 卡打开保存的录制文件时关闭,显示 ListView 已启动但未从 externalsdcard 打开 sdcard 内容 mp3 以在其中播放文件! 谢谢
这是我的代码
public class Droid extends ListActivity {
private static final String MEDIA_PATH = new String(Environment
.getExternalStorageDirectory().getPath());
private List<String> songs = new ArrayList<String>();
private MediaPlayer mp = new MediaPlayer();
private int currentPosition = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.songlist);
updateSongList();
}
public void updateSongList() {
File home = new File(MEDIA_PATH);
if (home.listFiles(new Filter()).length > 0) {
for (File file : home.listFiles(new Filter())) {
songs.add(file.getName());
}
ArrayAdapter<String> songList = new ArrayAdapter<String>(this,
R.layout.song_item, songs);
setListAdapter(songList);
}
}
class Filter implements FilenameFilter {
public boolean accept(File dir, String name) {
return (name.endsWith(".3gp"));
}
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
currentPosition = position;
playSong(MEDIA_PATH + songs.get(position));
}
private void playSong(String songPath) {
try {
mp.reset();
mp.setDataSource(songPath);
mp.prepare();
mp.start();
// Setup listener so next song starts automatically
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer arg0) {
nextSong();
}
});
} catch (IOException e) {
Log.v(getString(R.string.app_name), e.getMessage());
}
}
private void nextSong() {
if (++currentPosition >= songs.size()) {
// Last song, just reset currentPosition
currentPosition = 0;
} else {
// Play next song
playSong(MEDIA_PATH + songs.get(currentPosition));
}
}
}
songlist.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:drawSelectorOnTop="false"/>
<TextView android:id="@id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/media"/>
</LinearLayout>
song_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView android:id="@android:id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
强制关闭选项菜单上的新类 Droid
case R.id.droid:
Intent dr = new Intent(MainActivity.this, Droid.class);
startActivity(dr);
return true;
【问题讨论】:
-
需要查看堆栈跟踪
-
检查答案并给出答复
-
没有获取外部 sdcard mp3 来播放
-
可能是您的歌曲列表为空或为空
-
sdcard 里还有,但是无法获取文件和播放
标签: android android-intent android-activity android-listview android-studio