Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

there is no sun.audio jar in MusicUtil #16

Open
123qsa opened this issue Oct 18, 2022 · 1 comment
Open

there is no sun.audio jar in MusicUtil #16

123qsa opened this issue Oct 18, 2022 · 1 comment

Comments

@123qsa
Copy link

123qsa commented Oct 18, 2022

there is no sun.audio jar in MusicUtil as well as Internet.
So how can I get this jar

@pretendhigh
Copy link

sun.audio.AudioPlayer 和 sun.audio.AudioStream 是 Sun 的专有 Java API,它们并不是 Java 标准的一部分。在 Java 9 及以后的版本中,JDK 的模块化系统已经不再允许直接访问这些专有 API,而在 Java 11 及以后的版本中,这些 API 已经完全被移除。

如果你的项目依赖于这些 API,你需要将它们替换为标准的 Java API 或者其他的音频处理库。Java 的 javax.sound.sampled 包提供了一套完整的音频处理 API,你可以使用它来替换 sun.audio。

对于本例,可以修改 MusicUtil.java

package com.kingyu.flappybird.util;

import javax.sound.sampled.*;
import java.io.File;
import java.io.IOException;

/**
 * 音乐工具类
 * 使用 javax.sound.sampled 包来播放 WAV 音频
 */
public class MusicUtil {

    private static Clip fly;
    private static Clip crash;
    private static Clip score;

    // 初始化 Clip 并加载音频文件
    private static Clip loadClip(String filename) {
        try {
            AudioInputStream audioIn = AudioSystem.getAudioInputStream(new File(filename));
            Clip clip = AudioSystem.getClip();
            clip.open(audioIn);
            return clip;
        } catch (UnsupportedAudioFileException | IOException | LineUnavailableException e) {
            e.printStackTrace();
            return null;
        }
    }

    // 播放飞行音效
    public static void playFly() {
        if (fly == null) {
            fly = loadClip("resources/wav/fly.wav");
        }
        if (fly != null) {
            if (fly.isRunning()) {
                fly.stop();   // 停止音频,如果它正在播放
            }
            fly.setFramePosition(0); // 重新开始
            fly.start();
        }
    }

    // 播放碰撞音效
    public static void playCrash() {
        if (crash == null) {
            crash = loadClip("resources/wav/crash.wav");
        }
        if (crash != null) {
            if (crash.isRunning()) {
                crash.stop();
            }
            crash.setFramePosition(0);
            crash.start();
        }
    }

    // 播放得分音效
    public static void playScore() {
        if (score == null) {
            score = loadClip("resources/wav/score.wav");
        }
        if (score != null) {
            if (score.isRunning()) {
                score.stop();
            }
            score.setFramePosition(0);
            score.start();
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants