views:

37

answers:

1

I need help/pointer to doc/sample code on how to load multiple audio files from a specific folder on the SD card and have it play a random file back(i think i can figure out the last step if i could just figure out how to load multiple files). Here is my incredibly poorly written app so far, don't judge too harshly as I'm learning as I go.

public class zazenbox extends Activity implements OnClickListener{

File filecheck;
MediaPlayer player;
Button playerButton;
Integer var1;
String path1;
AlertDialog.Builder alertbox;

public void onClick(View v) {
    if (v.getId() == R.id.play) {
        playPause();
    }
}

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    demoLoad();

    playerButton = (Button) this.findViewById(R.id.play);
    playerButton.setText(R.string.stop);
    playerButton.setOnClickListener(this);

    demoPlay();
}

@Override
public void onPause() {
    super.onPause();
    player.pause();
}

@Override
public void onStop() {
    super.onStop();
    player.stop();
}

private void demoLoad() {

    dirfilecheck();

    player = new MediaPlayer();
    player.setLooping(true);
    try {
        player.setDataSource(path1);
        player.prepare();
    }
    catch (IOException e) { e.printStackTrace(); }
    catch (IllegalArgumentException e) { e.printStackTrace(); }
    catch (IllegalStateException e) { e.printStackTrace(); }
}

private void dirfilecheck() {
    filecheck = new File(Environment.getExternalStorageDirectory() + "/zazenbox");

    if(filecheck.exists() && filecheck.isDirectory()) {
        // load files.
        var1 = 1;
        path1 = filecheck + "/bm10" + var1 + ".wav";
    } else {
        // create folder, dl sample loop, and instruct user how to add music/loops.
        filecheck.mkdirs();
        alertbox = new AlertDialog.Builder(this);
        alertbox.setMessage("Please put loopable media in zazenbox on your sdcard.");
        alertbox.setNeutralButton("Ok, I will.", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface arg0, int arg1) {
                Toast.makeText(getApplicationContext(), "Please plug in your device now", Toast.LENGTH_LONG).show();
            }
        });

        alertbox.show();
    }
}

private void demoPause() {
    player.pause();
    playerButton.setText(R.string.play);
}

private void demoStop() {
    player.stop();
    playerButton.setText(R.string.play);
}


private void demoPlay() {
    player.start();
    playerButton.setText(R.string.stop);
}

private void playPause() {
    if(player.isPlaying()) {
        demoStop();
        //demoPause();
        //player.release();
        var1++;
        path1 = filecheck + "/bm10" + var1 + ".wav";
        /*try {
            player.setDataSource(path1);
            player.prepare();
        }
        catch (IOException e) { e.printStackTrace(); }
        catch (IllegalArgumentException e) { e.printStackTrace(); }
        catch (IllegalStateException e) { e.printStackTrace(); }*/
        //player.start();
        //demoPlay();
    } else {
        //do stuff
        demoPlay();
    }
}

}

+1  A: 

Memory is extremely limited on mobile devices, so you wouldn't want to load songs you're not going to play. So what you should do is find all of the audio files in that folder, and then choose one and THEN load and play it.

You just need to stop the current player and create a new instance.

MediaPlayer player = MediaPlayer.create(this, Uri.parse("Path/To/Media"));
player.start();
// change track
player.stop();
player = MediaPlayer.create(this, Uri.parse("New/Path/To/Media"));
player.start();
CaseyB
zer0her0
If you know where all the files are stored (say a specific folder on the sdcard) then getting a list of the files is easy: File folder = new File("path/to/folder"); File[] files = folder.listFiles(); OR File[] files = folder.listFiles(fileFilter);
mtmurdock
Ok maybe I should clarify, I can load the multiple files into an array/list well enough(as far as I can tell). What I can't do is get the mediaplayer to release currently playing audio file and play a different one.
zer0her0
I added to my answer to show how to do this.
CaseyB