Compile error using AudioPlayer class

3

When trying to compile the code below:

import javax.swing.*;
import sun.audio.*;
import com.sun.java.util.*; 
import java.awt.*;
import java.awt.Event.*;
import java.io.*;

public class filechus {
    public static void main(String[]args) {

        JFrame tela = new JFrame("Telex"); 
        JFileChooser filex = new JFileChooser();

        int opcao = filex.showOpenDialog(tela);

        if (opcao==JFileChooser.APPROVE_OPTION) { 
            File nomearquivo = filex.getSelectedFile();
            try {
                InputStream arq = new FileInputStream(nomearquivo); 
                AudioStream som = new AudioStream(arq); 
                AudioPlayer.player.start(som); 
                System.out.println("Tocando");
            }
            catch(Exception e) {
                System.out.println("Deu erro");
            }
        }
        tela.setBounds(10,10,800,600);
        tela.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        tela.setVisible(true);
    }
}

I get the following error message:

  

AudioPlayer is internal proprietary api and may be removed in a future release

How can I resolve this?

    
asked by anonymous 12.12.2014 / 00:26

1 answer

3

Your problem is with imports:

import sun.audio.*;
import com.sun.java.util.*;

The sun.* , sunw.* , and com.sun.* packages are proprietary and internal JDK packages that should not be used by applications, so your compiler complains about them. In my case at least, the com.sun.java.util package does not even exist.

To fix this, I was able to use the classes in the javax.sound.sampled package:

import javax.swing.*;
import java.io.*;
import javax.sound.sampled.*;

public class filechus {
    public static void main(String[]args) {
        JFrame tela = new JFrame("Telex");
        JFileChooser filex = new JFileChooser();
        int opcao = filex.showOpenDialog(tela);
        if (opcao == JFileChooser.APPROVE_OPTION) { 
            File nomearquivo = filex.getSelectedFile();

            try {
                InputStream arq = new BufferedInputStream(new FileInputStream(nomearquivo));
                AudioInputStream som = AudioSystem.getAudioInputStream(arq);
                Clip clip = AudioSystem.getClip();
                clip.open(som);
                clip.start();
                System.out.println("Tocando");
            }
            catch(Exception e) {
                e.printStackTrace();
            }
        }
        tela.setBounds(10,10,800,600);
        tela.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        tela.setVisible(true);
    }
}

Also note that when handling the error, it is important that the program at least tells you what the error was to help you solve it.

    
12.12.2014 / 02:24