我试图在Eclipse中创建一个简单的Android Media Player应用程序,播放sdcard中的歌曲。所有歌曲都在屏幕上显示为列表,但在单击时不播放。 这是我的MainActivity.java文件:
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import android.app.ListActivity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
class Mp3Filter implements FilenameFilter {
public boolean accept(File dir, String name) {
return (name.endsWith(".mp3"));
}
}
public class MainActivity extends ListActivity {
Button stop, play;
String SD_PATH = Environment.getExternalStorageDirectory().getPath();
List<String> songs = new ArrayList<String>();
MediaPlayer mp = new MediaPlayer();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
updatePlaylist();
stop = (Button) findViewById(R.id.button1);
play = (Button) findViewById(R.id.button2);
play.setVisibility(View.GONE);
stop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mp.stop();
}
});
play.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mp.pause();
if(mp.isPlaying()==false){
play.setText("Resume");
}
}
});
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
try{
mp.reset();
mp.setDataSource(SD_PATH + songs.get(position));
mp.prepare();
mp.start();
if(mp.isPlaying()== true){
play.setText("Pause");
play.setVisibility(View.VISIBLE);
}
} catch(IOException e){
Log.v(getString(R.string.app_name),e.getMessage());
}
}
private void updatePlaylist() {
// TODO Auto-generated method stub
File home = new File(SD_PATH);
if(home.listFiles( new Mp3Filter()).length > 0){
for(File file : home.listFiles( new Mp3Filter())) {
songs.add(file.getName());
}
ArrayAdapter<String> songList = new ArrayAdapter<String>(this,R.layout.song_items,songs);
setListAdapter(songList);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Activity_Main.xml文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.studs.mediaplayer.MainActivity" >
<ListView
android:id="@+id/android:list"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginBottom="50dp" >
</ListView>
<Button
android:id="@+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/android:list"
android:layout_alignParentBottom="true"
android:layout_marginLeft="23dp"
android:text="@string/pp" />
<Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignRight="@+id/android:list"
android:layout_marginRight="52dp"
android:text="@string/st" />
</RelativeLayout>
和song_items.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</TextView>
提前致谢!
答案 0 :(得分:0)
你的问题就在这一行
mp.setDataSource(SD_PATH + songs.get(position));
songs.get(position)
只是文件的名称,因为文件可能位于SD卡的某个子文件夹中,所以您没有设置正确的数据源..
<强>更新强>
可能是您可以使用第二个列表
List<String> songsPaths = new ArrayList<String>();
然后更新你的方法
private void updatePlaylist() {
// TODO Auto-generated method stub
File home = new File(SD_PATH);
if(home.listFiles( new Mp3Filter()).length > 0){
for(File file : home.listFiles( new Mp3Filter())) {
songs.add(file.getName());
songsPaths.add(file.getAbsolutePath());
}
ArrayAdapter<String> songList = new ArrayAdapter<String>(this,R.layout.song_items,songs);
setListAdapter(songList);
}
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
try{
mp.reset();
mp.setDataSource(songsPaths.get(position));
mp.prepare();
mp.start();
if(mp.isPlaying()== true){
play.setText("Pause");
play.setVisibility(View.VISIBLE);
}
} catch(IOException e){
Log.v(getString(R.string.app_name),e.getMessage());
}
}