如何为用户提供在Android应用程序中选择音乐的选项?

时间:2012-08-15 06:10:08

标签: android android-music-player

我是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

2 个答案:

答案 0 :(得分:0)

这绝对有可能。您可能希望使用ListView来显示使用可检查列表项的歌曲。然后将ListView挂钩到您已撰写的SimpleAdapterAdapter需要从SD卡(或者您保留它们的任何地方)获取歌曲标题,然后为ListView提供相应的列表项。

修改

File home = new File(MEDIA_PATH);

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

File sdCard = Environment.getExternalStorageDirectory();

答案 1 :(得分:0)

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