访问音频限制

时间:2014-11-05 20:27:45

标签: java audio restriction

我只是想在这里做一个简单的程序,这样我就可以尝试将它包含在游戏中。

package practs;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import sun.audio.*;
import javax.swing.JButton;
import javax.swing.JFrame;

public class Project1{

    public static void main(String[] args){
        JFrame frame = new JFrame();
        frame.setSize(200,200);
        frame.setLocationRelativeTo(null);
        JButton button = new JButton("Click me");
        frame.add(button);
        button.addActionListener(new AL());
        frame.setVisible(true);
    }

    public static class AL implements ActionListener{
        public final void actionPerformed(ActionEvent e){
            music();
        }
    }

    public static void music(){
        AudioPlayer MGP = AudioPlayer.player;
        AudioStream BGM;
        AudioData MD;

        ContinousAudioDataStream loop = null;

        try{

            InputStream test = new FileInputStream("backgroundMusic.m4a");
            BGM = new AudioStream(test);
            AudioPlayer.player.start(BGM);
            MD = BGM.getData();
            loop = new ContinousAudioDataStream(MD);

        }catch(FileNotFoundException e){
            e.printStackTrace();
        }catch(IOException error){
            error.printStackTrace();
        }
        MGP.start(loop);
    }
}

问题是,是所有错误,(ContinousAudioDataStream除外):

Access restriction: The type 'AudioPlayer' is not API (restriction on required library '/Library/Java/JavaVirtualMachines/jdk1.8.0_20.jdk/Contents/Home/jre/lib/rt.jar')

这些错误发生在

  • AudioPlayer
  • 语音串流
  • AudioData

  • 这就是ContinousAudioDataStream

  • ContinousAudioDataStream ContinousAudioDataStream cannot be resolved to a type

1 个答案:

答案 0 :(得分:5)

如果使用的库不是官方JDK API的一部分,则基本上会发生错误。您可以在Eclipse首选项中禁用这些警告。

转到

Windows -> Preferences -> Java -> Compiler -> Errors/Warnings Project -> Properties -> Java Compiler -> Errors/Warnings

现在转到Forbidden reference (access rules)” option under “Deprecated and restricted API

默认选项设置为error,这限制了此类API的使用。您可以将其更改为警告或忽略,然后清理并构建代码。

或者您可以尝试从构建路径中删除库并将其重新添加到项目中。