|
- importClass(android.media.MediaPlayer);
- importClass(android.media.AudioManager);
- importClass(java.io.File);
- importClass(android.widget.Toast);
- var mediaPlayer = new MediaPlayer();
- var mp3List = ["/sdcard/Pictures/1.mp3", "/sdcard/Music/2.mp3", "/sdcard/Audio/3.mp3"]; // MP3ÎļþÁбí
- var currentIndex = 0; // µ±Ç°²¥·ÅµÄÎļþË÷Òý
- var ac = new activity();
- ac.loadSXML(`
- <vertical>
- <button text="²¥·Å" id="play"/>
- <button text="Í£Ö¹" id="stop"/>
- <text id="status" text="×¼±¸²¥·Å..."/>
- </vertical>`);
- function playNext() {
- if (currentIndex < mp3List.length) {
- var mp3 = mp3List[currentIndex];
- if (file.isFile(mp3)) {
- try {
- mediaPlayer.reset(); // ÖØÖÃMediaPlayerÒÔ·Àֹ֮ǰµÄ²¥·Å״̬ӰÏìÐÂÎļþ
- mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
- mediaPlayer.setDataSource(mp3);
- mediaPlayer.prepare();
- mediaPlayer.start();
- ac.findViewById("status").setText("ÕýÔÚ²¥·Å: " + mp3);
- currentIndex++;
- } catch (e) {
- Toast.makeText(ac, "´íÎó: " + e.message, Toast.LENGTH_SHORT).show();
- ac.findViewById("status").setText("´íÎó: " + mp3 + " ²¥·Åʧ°Ü");
- }
- } else {
- Toast.makeText(ac, "´íÎó: Îļþ " + mp3 + " ²»´æÔÚ", Toast.LENGTH_SHORT).show();
- ac.findViewById("status").setText("´íÎó: Îļþ " + mp3 + " ²»´æÔÚ");
- }
- } else {
- Toast.makeText(ac, "²¥·Å½áÊø", Toast.LENGTH_SHORT).show();
- ac.findViewById("status").setText("²¥·Å½áÊø");
- mediaPlayer.stop();
- mediaPlayer.release(); // ÊÍ·Å×ÊÔ´
- currentIndex = 0; // ÖØÖÃË÷ÒýÒÔ±ãÖØпªÊ¼²¥·Å
- }
- }
- ac.findViewById("play").setOnClickListener(() => {
- Toast.makeText(ac, "²¥·Å", Toast.LENGTH_SHORT).show();
- playNext(); // ¿ªÊ¼²¥·ÅµÚÒ»¸ö»òÏÂÒ»¸öÎļþ
- });
- ac.findViewById("stop").setOnClickListener(() => {
- Toast.makeText(ac, "Í£Ö¹", Toast.LENGTH_SHORT).show();
- mediaPlayer.stop();
- mediaPlayer.release(); // ÊÍ·Å×ÊÔ´
- ac.findViewById("status").setText("²¥·ÅÍ£Ö¹");
- currentIndex = 0; // Í£Ö¹ºóÖØÖò¥·ÅË÷Òý
- });
- // Ìí¼ÓÒ»¸ö¼àÌýÆ÷À´×Ô¶¯²¥·ÅÏÂÒ»Ê×
- mediaPlayer.setOnCompletionListener(function() {
- playNext(); // µ±Ç°¸èÇú²¥·ÅÍê±Ïºó×Ô¶¯²¥·ÅÏÂÒ»Ê×
- });
¸´ÖÆ´úÂë
|
|