|  | 
| |  |  |  |  |  | 复制代码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(); // 当前歌曲播放完毕后自动播放下一首
});
 |  |  |  |  |  | 
 | 
 |