0

我是 Android 新手,我需要一条途径来满足我的要求。在我的应用程序中,我需要为用户提供一个选项来从应用程序中选择他们的播放列表。这可能吗?请确认。

我使用以下示例代码进行了尝试,在以下代码中,我尝试从 sd 卡获取文件并在列表视图中显示它们。当我尝试运行应用程序时,我强制关闭。如果我做错了什么,请您调查一下并建议我。

我的测试课。

public class TestMediaActivity extends Activity {
/** Called when the activity is first created. */

private static final String MEDIA_PATH = new String("/sdcard/");
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.main);

    updateSongList();
}
public void updateSongList() 
{
    System.out.println("In the updateSongList");
    File home = new File(MEDIA_PATH);
    if (home.listFiles(new Mp3Filter()).length > 0) 
    {
        System.out.println("\n In the condition length>0");
            for (File file : home.listFiles(new Mp3Filter())) {
                    songs.add(file.getName());
            }

            setListAdapter( new ArrayAdapter<String>(this,R.layout.test_item, songs));
    }

}

private void setListAdapter(ArrayAdapter<String> arrayAdapter) {
    // TODO Auto-generated method stub
    System.out.println("In the setListAdapter");
}
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));
}
}


}

支持类是,

public class Mp3Filter implements FilenameFilter 
{

@Override
public boolean accept(File dir, String filename) {
    // TODO Auto-generated method stub
    System.out.println("In the Mp3Filter");
    return (filename.endsWith(".mp3"));
   }

}

而日志猫中的错误是....

 08-15 14:49:35.800: E/AndroidRuntime(31871): FATAL EXCEPTION: main
 08-15 14:49:35.800: E/AndroidRuntime(31871): java.lang.RuntimeException: Unable to       start activity     ComponentInfo{com.vsoft.TestMedia/com.vsoft.TestMedia.TestMediaActivity}:  java.lang.NullPointerException
08-15 14:49:35.800: E/AndroidRuntime(31871):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2781)
08-15 14:49:35.800: E/AndroidRuntime(31871):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2797)
08-15 14:49:35.800: E/AndroidRuntime(31871):    at android.app.ActivityThread.access$2300(ActivityThread.java:135)
08-15 14:49:35.800: E/AndroidRuntime(31871):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2132)
08-15 14:49:35.800: E/AndroidRuntime(31871):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-15 14:49:35.800: E/AndroidRuntime(31871):    at android.os.Looper.loop(Looper.java:143)
08-15 14:49:35.800: E/AndroidRuntime(31871):    at android.app.ActivityThread.main(ActivityThread.java:4914)
08-15 14:49:35.800: E/AndroidRuntime(31871):    at java.lang.reflect.Method.invokeNative(Native Method)
08-15 14:49:35.800: E/AndroidRuntime(31871):    at java.lang.reflect.Method.invoke(Method.java:521)
08-15 14:49:35.800: E/AndroidRuntime(31871):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
08-15 14:49:35.800: E/AndroidRuntime(31871):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
08-15 14:49:35.800: E/AndroidRuntime(31871):    at dalvik.system.NativeStart.main(Native Method)
08-15 14:49:35.800: E/AndroidRuntime(31871): Caused by: java.lang.NullPointerException
08-15 14:49:35.800: E/AndroidRuntime(31871):    at  com.vsoft.TestMedia.TestMediaActivity.updateSongList(TestMediaActivity.java:36)
08-15 14:49:35.800: E/AndroidRuntime(31871):    at com.vsoft.TestMedia.TestMediaActivity.onCreate(TestMediaActivity.java:29)
08-15 14:49:35.800: E/AndroidRuntime(31871):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1065)
08-15 14:49:35.800: E/AndroidRuntime(31871):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2745)
08-15 14:49:35.800: E/AndroidRuntime(31871):    ... 11 more
4

2 回答 2

0

这绝对是可能的。您可能希望ListView使用可检查的列表项来显示歌曲。然后连接ListViewSimpleAdapter你写的一个。Adapter需要从 SD 卡(或保存它们的任何地方)中获取歌曲名称,然后提供适当ListView的列表项。

编辑

File home = new File(MEDIA_PATH);

是问题所在。homenull。原因null是找不到文件。您不应该使用它/sdcard/来访问 SD 卡,因为 SD 卡目录是设备相关的。你想要的是:

File sdCard = Environment.getExternalStorageDirectory();
于 2012-08-15T06:18:25.967 回答
0

我从来没有尝试过使用音频,但你不能只用过滤器“audio/*”创建一个意图并调用活动以获得结果吗?

于 2012-08-15T08:23:00.850 回答