1
我需要關於如何從SD卡上的特定文件夾加載多個音頻文件並讓它播放隨機文件的幫助/指向文檔/示例代碼的指針(我想我可以弄清楚最後一步,如果我可以弄清楚如何加載多個文件)。到目前爲止,這是我寫得非常差的應用程序,不要像我一直在學習時那樣苛刻地判斷。從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();
}
}
}
夠公平了,我明白了。所以我的問題沒有正確說明(即我的意思是加載一個列表或一個數組而不是實際的文件),但你明白我想要做什麼。這些不是歌曲,這些音頻文件通常長20-30秒,並且設計爲易於循環。 – zer0her0 2010-06-21 21:56:29
如果你知道所有的文件存儲在哪裏(說SD卡上的特定文件夾),然後獲得文件列表很容易:文件夾=新文件(「路徑/到/文件夾」); File [] files = folder.listFiles(); OR File [] files = folder.listFiles(fileFilter); – mtmurdock 2010-06-21 22:08:20
好吧,也許我應該澄清,我可以加載多個文件到一個數組/列表足夠好(據我所知)。我不能做的是讓mediaplayer發佈當前正在播放的音頻文件並播放另一個。 – zer0her0 2010-06-22 14:31:34