簡體   English   中英

從SD卡加載多個音頻文件

[英]Loading Multiple Audio files from SD card

我需要有關如何從SD卡上的特定文件夾加載多個音頻文件並使其播放隨機文件的文檔/示例代碼的幫助/指針(我想我可以弄清楚最后一步,如果我能弄清楚如何加載多個文件)。 到目前為止,這是我迄今為止編寫得非常差的應用程序,在我邊學邊學習的過程中,請不要過分苛刻。

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

}

行動裝置上的記憶體非常有限,因此您不希望載入不想播放的歌曲。 因此,您應該做的是找到該文件夾​​中的所有音頻文件,然后選擇一個,然后加載並播放它。

您只需要停止當前播放器並創建一個新實例即可。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM