1 /*
   2  * Copyright (c) 2010, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.media.jfxmedia;
  27 
  28 import com.sun.media.jfxmedia.events.MediaErrorListener;
  29 import com.sun.media.jfxmedia.locator.Locator;
  30 import com.sun.media.jfxmediaimpl.NativeMediaManager;
  31 import java.util.List;
  32 
  33 /**
  34  * Factory class used to create media objects, players, and recorders, and to
  35  * manage other global functionality.
  36  *
  37  * @see MediaPlayer
  38  * @see MediaRecorder
  39  */
  40 public class MediaManager {
  41 
  42     private MediaManager() {
  43         // prevent instantiation of this class
  44     }
  45 
  46     /**
  47      * @return {@link String} array of supported content types.
  48      */
  49     public static String[] getSupportedContentTypes() {
  50         return NativeMediaManager.getDefaultInstance().getSupportedContentTypes();
  51     }
  52 
  53     /**
  54      * Whether a media source having the indicated content type may be
  55      * played.
  56      *
  57      * @throws IllegalArgumentException if <code>contentType</code> is
  58      * <code>null</code>.
  59      */
  60     public static boolean canPlayContentType(String contentType) {
  61         if (contentType == null) {
  62             throw new IllegalArgumentException("contentType == null!");
  63         }
  64         return NativeMediaManager.getDefaultInstance().canPlayContentType(contentType);
  65     }
  66 
  67     /**
  68      * Whether a media source having the indicated protocol may be
  69      * played.
  70      *
  71      * @throws IllegalArgumentException if <code>contentType</code> is
  72      * <code>null</code>.
  73      */
  74     public static boolean canPlayProtocol(String protocol) {
  75         if (protocol == null) {
  76             throw new IllegalArgumentException("protocol == null!");
  77         }
  78         return NativeMediaManager.getDefaultInstance().canPlayProtocol(protocol);
  79     }
  80 
  81 
  82     // XXX javadoc
  83     public static MetadataParser getMetadataParser(Locator locator) {
  84         if (locator == null) {
  85             throw new IllegalArgumentException("locator == null!");
  86         }
  87         return NativeMediaManager.getDefaultInstance().getMetadataParser(locator);
  88     }
  89 
  90     /**
  91      * Gets a Media object for the clip.  It cannot be played without attaching
  92      * to a MediaPlayer.
  93      *
  94      * @param locator
  95      * @return Media object
  96      * @throws IllegalArgumentException if <code>locator</code> is
  97      * <code>null</code>.
  98      */
  99     public static Media getMedia(Locator locator) {
 100         if (locator == null) {
 101             throw new IllegalArgumentException("locator == null!");
 102         }
 103         return NativeMediaManager.getDefaultInstance().getMedia(locator);
 104     }
 105 
 106     /**
 107      * Get a player for the media locator
 108      *
 109      * @param locator
 110      * @return MediaPlayer object
 111      * @throws IllegalArgumentException if <code>locator</code> is
 112      * <code>null</code>.
 113      */
 114     public static MediaPlayer getPlayer(Locator locator) {
 115         if (locator == null) {
 116             throw new IllegalArgumentException("locator == null!");
 117         }
 118         return NativeMediaManager.getDefaultInstance().getPlayer(locator);
 119     }
 120 
 121     /**
 122      * Add a global listener for warnings. This listener will receive warnings
 123      * which occur fall outside the context of a particular player or recorder.
 124      *
 125      * @param listener The listener to add.
 126      * @throws IllegalArgumentException if <code>listener</code> is
 127      * <code>null</code>.
 128      */
 129     public static void addMediaErrorListener(MediaErrorListener listener) {
 130         if (listener == null) {
 131             throw new IllegalArgumentException("listener == null!");
 132         }
 133         NativeMediaManager.getDefaultInstance().addMediaErrorListener(listener);
 134     }
 135 
 136     /**
 137      * Remove a global listener for warnings.
 138      *
 139      * @param listener The listener to remove.
 140      * @throws IllegalArgumentException if <code>listener</code> is
 141      * <code>null</code>.
 142      */
 143     public static void removeMediaErrorListener(MediaErrorListener listener) {
 144         if (listener == null) {
 145             throw new IllegalArgumentException("listener == null!");
 146         }
 147         NativeMediaManager.getDefaultInstance().removeMediaErrorListener(listener);
 148     }
 149 
 150     /**
 151      * This function will register MediaPlayer for disposing when obj parameter
 152      * does not have any strong reference.
 153      *
 154      * @param obj - Object to watch for strong references
 155      * @param player - MediaPlayer to dispose
 156      */
 157     public static void registerMediaPlayerForDispose(Object obj, MediaPlayer player) {
 158         NativeMediaManager.registerMediaPlayerForDispose(obj, player);
 159     }
 160 
 161     /**
 162      * Retrieve all un-disposed {@link MediaPlayer}s.
 163      * @return a {@link List} of all un-disposed players or <code>null</code>.
 164      */
 165     public static List<MediaPlayer> getAllMediaPlayers() {
 166         return NativeMediaManager.getDefaultInstance().getAllMediaPlayers();
 167     }
 168 }