执行以下代码报错:const downloadMusic = () => {
let music_url = 'http://music.163.com/song/media/outer/url?id=447925558.mp3';
let res = http.get(music_url);
if (res.statusCode != 200) return toastLog('下载音乐失败');
files.writeBytes('./npSound.mp3', res.body.bytes());
let absPath = files.path('./npSound.mp3');
if (!files.isFile(absPath)) return toastLog('文件不存在');
try {
    media.playMusic(absPath);
} catch (e) {
    toastLog('播放异常: ' + e);
}
};报错内容:播放异常: JavaException: com.stardust.pio.UncheckedIOException: java.io.IOException: Prepare failed.: status=0x1
回答
根据你提供的代码和错误信息,问题可能出在以下几个方面:
- 
文件路径问题: ./npSound.mp3是相对路径,Hamibot 的文件系统可能需要绝对路径(如/sdcard/npSound.mp3)才能正确访问文件。
- 
HTTP 请求问题:网易云音乐的 URL 可能需要添加请求头(如 User-Agent)才能正常下载,否则可能返回无效内容。
- 
文件完整性:下载的文件可能损坏或格式不正确,导致 media.playMusic无法解析。
修改后的代码示例:
// 文档地址 https://docs.hamibot.com/
const downloadMusic = () => {
    // 使用绝对路径保存文件
    let filePath = '/sdcard/npSound.mp3';
    let music_url = 'http://music.163.com/song/media/outer/url?id=447925558.mp3';
    // 添加请求头模拟浏览器访问
    let res = http.get(music_url, {
        headers: {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
        }
    });
    if (res.statusCode != 200) return toastLog('下载音乐失败');
    // 写入绝对路径
    files.writeBytes(filePath, res.body.bytes());
    // 检查文件是否存在
    if (!files.isFile(filePath)) return toastLog('文件不存在');
    try {
        media.playMusic(filePath);
    } catch (e) {
        toastLog('播放异常: ' + e);
    }
};
关键修改说明:
- 绝对路径:将文件保存到 /sdcard/目录下,确保 Hamibot 脚本能正确读取。
- 请求头:添加 User-Agent模拟浏览器访问,避免因防盗链导致下载失败。
- 权限检查:确保脚本已获取存储权限(需在 Hamibot 中手动开启)。
如果问题仍未解决,可能是音频格式不兼容,建议尝试其他 MP3 文件测试。
                ℹ️ 本回答由 AI 生成,内容仅供参考