0

I am new to Android, I need a path to fulfill my requirements. In my application I need to provide an option for the user to select their playlist from the application. Is this possible?, please confirm.

I tried this with the following sample code, in the following code I tried to get the files from sd card and display them in list view. When I try to run the application I got force close. Could you please look into this and suggest me if I am doing anything wrong.

My test class.

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));
}
}


}

And supporting class is ,

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"));
   }

}

And the error in the log cat is....

 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

It's definitely possible. You'll probably want to use a ListView to display the songs using checkable list items. Then hook the ListView up to a SimpleAdapter that you've written. The Adapter will need to fetch the song titles from the SD card (or wherever you're keeping them), and then provide the ListView with the appropriate list items.

EDIT

File home = new File(MEDIA_PATH);

is the problem. home is null. The reason it's null is because the file isn't found. You shouldn't use /sdcard/ to access the SD card because the SD card directory is device-dependent. What you want is:

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

I've never tried that with Audio, but can't you just create an intent with filter "audio/*" and call activity for result on it?

于 2012-08-15T08:23:00.850 に答える