Java Code Examples for javax.sound.sampled.AudioPermission

Following code examples demonstrate how to use javax.sound.sampled.AudioPermissionfrom java. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use javax.sound.sampled.AudioPermissionand various code implementation of this class.

    public JavaSoundHub() {

        System.out.println("---------------------------------------------");
        assert (the == null);
        the = this;
        assert (inited == false);

        inited = true;
        int iflag = 0;
        if (synth != null) {
            return;
        }

        try {

            double sampleRate = 44100;
            synth = JavaSoundSynth.the();

            Conductor.init();

            SecurityManager sm = System.getSecurityManager();

            boolean noRecord = false;
            if (sm != null) {
                System.out.println(
                        " Security manager exists. Asking about record");
                Object context = sm.getSecurityContext();
                AudioPermission perm = new AudioPermission("record");
                try {
                    sm.checkPermission(perm, context);
                } catch (SecurityException ex) {
                    ex.printStackTrace();
                    noRecord = true;
                }
            } else {
                System.out.println(" No Security manager.");
            }

            if (!noRecord) {
                feed = new TargetDataLineFeed(sampleRate);
                Thread t = new Thread(feed);
                t.setPriority(Thread.MAX_PRIORITY);

                peakAnalyst = new PeakAnalyst(feed);
                t.start();
            }
        } catch (Exception e) {

            e.printStackTrace();

        }


    } 


    public void open() {

        
        AudioPermission perm = new AudioPermission("record", null);
        perm.checkGuard(null);

        super.open();

        PulseAudioMixer parent = PulseAudioMixer.getInstance();
        parent.addSourceLine(this);
    } 

    public void close() {

        
        AudioPermission perm = new AudioPermission("record", null);
        perm.checkGuard(null);

        if (!isOpen) {
            throw new IllegalStateException("Port is not open; so cant close");
        }

        PulseAudioMixer parent = PulseAudioMixer.getInstance();
        parent.removeSourceLine(this);

        super.close();
    } 

  
  static void checkRecordPermission()
    throws SecurityException
  {
    SecurityManager localSecurityManager = System.getSecurityManager();
    if (localSecurityManager != null) {
      localSecurityManager.checkPermission(new AudioPermission("record"));
    }
  } 

    @Test
    public default void verifyGenericType()
    throws Exception {
        final Class<SUT> sut = createNewSUT();
        assertTrue("This j8unit test interface is used with a generic type that is illegaly not assignable to AudioPermission.class!",
                   AudioPermission.class.isAssignableFrom(sut));
    } 

  
  public static final void checkPermissions(Permission permission)
  {
    SecurityManager sm = System.getSecurityManager();
    if (sm != null)
      {
        String perm = null;
        switch (permission)
          {
          case PLAY:
            perm = "play";
            break;
            
          case RECORD:
            perm = "record";
            break;
            
          case ALL: default:
            perm = "*";
            break;
          }
        
        sm.checkPermission(new AudioPermission(perm));
      }
  } 

Advertisement
Javadoc
The AudioPermission class represents access rights to the audio system resources. An AudioPermission

contains a target name but no actions list; you either have the named permission or you don't.

The target name is the name of the audio permission (see the table below). The names follow the hierarchical property-naming convention. Also, an asterisk can be used to represent all the audio permissions.

The following table lists the possible AudioPermission target names. For each name, the table provides a description of exactly what that permission allows, as well as a discussion of the risks of granting code the permission. Permission target name, what the permission allows, and associated risks Permission Target Name What the Permission Allows Risks of Allowing this Permission play Audio playback through the audio device or devices on the system. Allows the application to obtain and manipulate lines and mixers for audio playback (rendering). In some cases use of this permission may affect other applications because the audio from one line may be mixed with other audio being played on the system, or because manipulation of a mixer affects the audio for all lines using that mixer. record Audio recording through the audio device or devices on the system. Allows the application to obtain and manipulate lines and mixers for audio recording (capture). In some cases use of this permission may affect other applications because manipulation of a mixer affects the audio for all lines using that mixer. This permission can enable an applet or application to eavesdrop on a user. @author Kara Kytle @since 1.3

Read More
Advertisement