A
A
alexeytmp2015-04-16 12:23:54
Java
alexeytmp, 2015-04-16 12:23:54

How to play sound in JAVA?

AudioFormat audioFormat = new AudioFormat(8000, 16, 1, true, false);
    private TargetDataLine targetDataLine;
    private SourceDataLine sourceDataLine;
    private Object sourceDataLineMutex;
    private DataLine.Info targetInfo;
    private DataLine.Info sourceInfo;
...
AccessController.doPrivileged(
            new PrivilegedAction<Void>() {

                @Override
                public Void run() {
                    try {
                        targetDataLine = (TargetDataLine) AudioSystem.getLine(targetInfo);
                        targetDataLine.open(audioFormat);
                    } catch (LineUnavailableException e) {
                        logger.error("target line unavailable", e);
                        return null;
                    } catch (SecurityException e) {
                        logger.error("security exception", e);
                        return null;
                    } catch (Throwable t) {
                        logger.error("throwable " + t.getMessage());
                        return null;
                    }
                    targetDataLine.start();
                    synchronized (sourceDataLineMutex) {
                        try {
                            sourceDataLine = (SourceDataLine) AudioSystem.getLine(sourceInfo);
                            sourceDataLine.open(audioFormat);
                        } catch (LineUnavailableException e) {
                            logger.error("source line unavailable", e);
                            return null;
                        }
                        sourceDataLine.start();
                    }
                    return null;
                }
        });

Audio playback works fine, but when I try to play another file, I get an exception ( targetDataLine.open(audioFormat); - at this location):
javax.sound.sampled.LineUnavailableException: line with format PCM_SIGNED 8000.0 Hz, 16 bit, mono, 2 bytes /frame, little-endian not supported.
at com.sun.media.sound.DirectAudioDevice$DirectDL.implOpen(DirectAudioDevice.java:513)
at com.sun.media.sound.AbstractDataLine.open(AbstractDataLine.java:124)
at com.sun.media.sound.AbstractDataLine .open(AbstractDataLine.java:156)
at net.sourceforge.peers.javaxsound.JavaxSoundManager$1.run(JavaxSoundManager.java:106)
at net.sourceforge.peers.javaxsound.JavaxSoundManager$1.run(JavaxSoundManager.java:98)
at java.security.AccessController.doPrivileged(Native Method)
at net.sourceforge.peers.javaxsound.JavaxSoundManager.init(JavaxSoundManager.java:97)
at net.sourceforge.peers.media.MediaManager.successResponseReceived(MediaManager.java:98)
at net.sourceforge.peers.sip.core.useragent.handlers.InviteHandler.successResponseReceived(InviteHandler.java:547)
at net.sourceforge.peers.sip.transaction.InviteClientTransactionStateProceeding.received2xx(InviteClientTransactionStateProceeding.java:44
) .peers.sip.transaction.InviteClientTransaction.receivedResponse(InviteClientTransaction.java:154)
at net.sourceforge.peers.sip.transport.MessageReceiver.processMessage(MessageReceiver.java:184)
at net.sourceforge.peers.sip.transport.UdpMessageReceiver.listen(UdpMessageReceiver.java:85)
at net.sourceforge.peers.sip.transport.MessageReceiver.run(MessageReceiver.java:69)
at java.lang.Thread.run (Thread.java:722)
how to bypass?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
chichmarino, 2015-10-11
@chichmarino

https://github.com/ccidral/tomighty/issues/17
Solved by switching to oracle jdk 7 latest versions

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question