android:ListActivity 无法打开 sdcard mp3 既不能播放 mp3
Posted
技术标签:
【中文标题】android:ListActivity 无法打开 sdcard mp3 既不能播放 mp3【英文标题】:android:ListActivity unable to open sdcard mp3 neither play mp3 【发布时间】: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_
android:layout_
android:orientation="vertical" >
<ListView android:id="@android:id/list"
android:layout_
android:layout_
android:layout_weight="1"
android:drawSelectorOnTop="false"/>
<TextView android:id="@id/android:empty"
android:layout_
android:layout_
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_
android:layout_
android:orientation="vertical" >
<TextView android:id="@android:id/text1"
android:layout_
android:layout_/>
</LinearLayout>
强制关闭选项菜单上的新类 Droid
case R.id.droid:
Intent dr = new Intent(MainActivity.this, Droid.class);
startActivity(dr);
return true;
【问题讨论】:
需要查看堆栈跟踪 检查答案并给出答复 没有获取外部 sdcard mp3 来播放 可能是您的歌曲列表为空或为空 sdcard 里还有,但是无法获取文件和播放 【参考方案1】:你没有在你的 Manifest 中声明你的 Drod Activity。
如果所有其他工作正常,则您应该发布堆栈跟踪。
【讨论】:
【参考方案2】:改变这个
public class Droid extends ListActivity
到
public class Droid extends Activity
// U already define listview in xml so no need to extends activity as ListActivity
// implements this method,
listview.setOnItemClickListener(new OnItemClickListener()
@Override
public void onItemClick(AdapterView<?> arg0, View view,
int position, long id)
currentPosition = position;
playSong(MEDIA_PATH + songs.get(position));
);
【讨论】:
onListItemClick
err 也想玩以上是关于android:ListActivity 无法打开 sdcard mp3 既不能播放 mp3的主要内容,如果未能解决你的问题,请参考以下文章