Build error using Audioplayer class

Asked

Viewed 96 times

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 fix this?

  • What’s the mistake you’re making? Could you elaborate more on the question?

  • 1

    Greetings, it turns out that when I compile, the same gives the following error: audio player is Internal Proprietary api and may be Removed in a Future release

  • 3

    Is that a mistake or is it just a warning (Warning)? The program finishes compiling and runs?

  • This is a (Warning), but the program did not end of complicating, until they gave me the solution below, by @Victor.

1 answer

3

Your problem is with Rts:

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

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

To fix this, I managed by making use of the package classes javax.sound.sampled:

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 error it gave to help you fix it.

  • Greetings, thanks for the help given, complicated without errors, nor warnings.

  • 2

    @Augustotomás Compliu or compiled?

  • Compiled, sorry for the dictionary error, Victor

  • @Ok Augustotomas, then. Do not forget to give the acceptance in my answer if there is no more doubt. :)

  • So far this was my concern. Thanks for the help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.