All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jitsi.impl.neomedia.notify.JavaSoundClipImpl Maven / Gradle / Ivy

/*
 * Copyright @ 2015 Atlassian Pty Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jitsi.impl.neomedia.notify;

import java.applet.*;
import java.io.*;
import java.lang.reflect.*;
import java.net.*;
import java.security.*;

import org.jitsi.service.audionotifier.*;

/**
 * Implementation of SCAudioClip.
 *
 * @author Yana Stamcheva
 */
public class JavaSoundClipImpl
    extends AbstractSCAudioClip

{
    private static Constructor acConstructor = null;

    @SuppressWarnings("unchecked")
    private static Constructor createAcConstructor()
        throws ClassNotFoundException,
               NoSuchMethodException,
               SecurityException
    {
        Class class1;
        try
        {
            class1
                = Class.forName(
                    "com.sun.media.sound.JavaSoundAudioClip",
                    true,
                    ClassLoader.getSystemClassLoader());
        }
        catch (ClassNotFoundException cnfex)
        {
            class1
                = Class.forName("sun.audio.SunAudioClip", true, null);
        }
        return
            (Constructor) class1.getConstructor(InputStream.class);
    }

    /**
     * Creates an AppletAudioClip.
     *
     * @param inputstream the audio input stream
     * @throws IOException
     */
    private static AudioClip createAppletAudioClip(InputStream inputstream)
        throws IOException
    {
        if (acConstructor == null)
        {
            try
            {
                acConstructor
                    = AccessController.doPrivileged(
                            new PrivilegedExceptionAction>()
                            {
                                public Constructor run()
                                    throws ClassNotFoundException,
                                           NoSuchMethodException,
                                           SecurityException
                                {
                                    return createAcConstructor();
                                }
                            });
            }
            catch (PrivilegedActionException paex)
            {
                throw
                    new IOException(
                            "Failed to get AudioClip constructor: "
                                + paex.getException());
            }
        }

        try
        {
            return acConstructor.newInstance(inputstream);
        }
        catch (Exception ex)
        {
            throw new IOException("Failed to construct the AudioClip: " + ex);
        }
    }

    private final AudioClip audioClip;

    /**
     * Initializes a new JavaSoundClipImpl instance which is to play
     * audio stored at a specific URL using
     * java.applet.AudioClip.
     *
     * @param uri the URL at which the audio is stored and which the
     * new instance is to load
     * @param audioNotifier the AudioNotifierService which is
     * initializing the new instance and whose mute property/state is
     * to be monitored by the new instance
     * @throws IOException if a java.applet.AudioClip could not be
     * initialized or the audio at the specified url could not be read
     */
    public JavaSoundClipImpl(String uri, AudioNotifierService audioNotifier)
            throws IOException
    {
        super(uri, audioNotifier);

        audioClip = createAppletAudioClip(new URL(uri).openStream());
    }

    /**
     * {@inheritDoc}
     *
     * Stops the java.applet.AudioClip wrapped by this instance.
     */
    @Override
    protected void internalStop()
    {
        try
        {
            if (audioClip != null)
                audioClip.stop();
        }
        finally
        {
            super.internalStop();
        }
    }

    /**
     * {@inheritDoc}
     *
     * Plays the java.applet.AudioClip wrapped by this instance.
     */
    @Override
    protected boolean runOnceInPlayThread()
    {
        if (audioClip == null)
            return false;
        else
        {
            audioClip.play();
            return true;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy