📄 audiosystem.java
字号:
Mixer.Info[] infos = getMixerInfo(); for (int i = 0; i < infos.length; ++i) { Mixer mix = getMixer(infos[i]); try { return mix.getLine(info); } catch (LineUnavailableException _) { // Try the next provider. } } throw new LineUnavailableException("no Clip available"); } /** * Return a mixer matching the provided description. All the providers * on the system are searched for a matching mixer. * @param info description of the mixer * @return the matching mixer * @throws IllegalArgumentException if no provider supplies a matching mixer */ public static Mixer getMixer(Mixer.Info info) { Iterator i = ServiceFactory.lookupProviders(MixerProvider.class); while (i.hasNext()) { MixerProvider prov = (MixerProvider) i.next(); if (prov.isMixerSupported(info)) return prov.getMixer(info); } throw new IllegalArgumentException("mixer not found"); } /** * Return an array of descriptions of all the mixers provided on the system. */ public static Mixer.Info[] getMixerInfo() { HashSet result = new HashSet(); Iterator i = ServiceFactory.lookupProviders(MixerProvider.class); while (i.hasNext()) { MixerProvider prov = (MixerProvider) i.next(); Mixer.Info[] is = prov.getMixerInfo(); for (int j = 0; j < is.length; ++j) result.add(is[j]); } return (Mixer.Info[]) result.toArray(new Mixer.Info[result.size()]); } /** * Return a source data line matching the given audio format. * @param fmt the audio format * @throws LineUnavailableException if no source data line matching * this format is available * @since 1.5 */ public static SourceDataLine getSourceDataLine(AudioFormat fmt) throws LineUnavailableException { DataLine.Info info = new DataLine.Info(SourceDataLine.class, fmt); Mixer.Info[] mixers = getMixerInfo(); for (int i = 0; i < mixers.length; ++i) { Mixer mix = getMixer(mixers[i]); if (mix.isLineSupported(info)) return (SourceDataLine) mix.getLine(info); } throw new LineUnavailableException("source data line not found"); } /** * Return a target data line matching the given audio format. * @param fmt the audio format * @throws LineUnavailableException if no target data line matching * this format is available * @since 1.5 */ public static SourceDataLine getSourceDataLine(AudioFormat fmt, Mixer.Info mixer) throws LineUnavailableException { DataLine.Info info = new DataLine.Info(SourceDataLine.class, fmt); Mixer mix = getMixer(mixer); if (mix.isLineSupported(info)) return (SourceDataLine) mix.getLine(info); throw new LineUnavailableException("source data line not found"); } /** * Return an array of descriptions of all the source lines matching * the given line description. * @param info description of the lines to match */ public static Line.Info[] getSourceLineInfo(Line.Info info) { HashSet result = new HashSet(); Mixer.Info[] infos = getMixerInfo(); for (int i = 0; i < infos.length; ++i) { Mixer mix = getMixer(infos[i]); Line.Info[] srcs = mix.getSourceLineInfo(info); for (int j = 0; j < srcs.length; ++j) result.add(srcs[j]); } return (Line.Info[]) result.toArray(new Line.Info[result.size()]); } /** * Find and return a target data line matching the given audio format. * @param fmt the format to match * @throws LineUnavailableException if no matching line was found * @since 1.5 */ public static TargetDataLine getTargetDataLine(AudioFormat fmt) throws LineUnavailableException { DataLine.Info info = new DataLine.Info(TargetDataLine.class, fmt); Mixer.Info[] mixers = getMixerInfo(); for (int i = 0; i < mixers.length; ++i) { Mixer mix = getMixer(mixers[i]); if (mix.isLineSupported(info)) return (TargetDataLine) mix.getLine(info); } throw new LineUnavailableException("target data line not found"); } /** * Return a target data line matching the given audio format and * mixer. * @param fmt the audio format * @param mixer the mixer description * @return a target data line * @throws LineUnavailableException if no matching target data line was * found * @since 1.5 */ public static TargetDataLine getTargetDataLine(AudioFormat fmt, Mixer.Info mixer) throws LineUnavailableException { DataLine.Info info = new DataLine.Info(TargetDataLine.class, fmt); Mixer mix = getMixer(mixer); if (mix.isLineSupported(info)) return (TargetDataLine) mix.getLine(info); throw new LineUnavailableException("target data line not found"); } /** * Given a source encoding, return an array of all target encodings to which * data in this form can be converted. * @param source the source encoding */ public static AudioFormat.Encoding[] getTargetEncodings(AudioFormat.Encoding source) { HashSet result = new HashSet(); Iterator i = ServiceFactory.lookupProviders(FormatConversionProvider.class); while (i.hasNext()) { FormatConversionProvider prov = (FormatConversionProvider) i.next(); if (! prov.isSourceEncodingSupported(source)) continue; AudioFormat.Encoding[] es = prov.getTargetEncodings(); for (int j = 0; j < es.length; ++j) result.add(es[j]); } return (AudioFormat.Encoding[]) result.toArray(new AudioFormat.Encoding[result.size()]); } /** * Given a source format, return an array of all the target encodings to * which data in this format can be converted. * @param source the source format */ public static AudioFormat.Encoding[] getTargetEncodings(AudioFormat source) { HashSet result = new HashSet(); Iterator i = ServiceFactory.lookupProviders(FormatConversionProvider.class); while (i.hasNext()) { FormatConversionProvider prov = (FormatConversionProvider) i.next(); AudioFormat.Encoding[] es = prov.getTargetEncodings(source); for (int j = 0; j < es.length; ++j) result.add(es[j]); } return (AudioFormat.Encoding[]) result.toArray(new AudioFormat.Encoding[result.size()]); } /** * Given a target encoding and a source audio format, return an array of all * matching audio formats to which data in this source format can be converted. * @param encoding the target encoding * @param sourceFmt the source format */ public static AudioFormat[] getTargetFormats(AudioFormat.Encoding encoding, AudioFormat sourceFmt) { HashSet result = new HashSet(); Iterator i = ServiceFactory.lookupProviders(FormatConversionProvider.class); while (i.hasNext()) { FormatConversionProvider prov = (FormatConversionProvider) i.next(); AudioFormat[] es = prov.getTargetFormats(encoding, sourceFmt); for (int j = 0; j < es.length; ++j) result.add(es[j]); } return (AudioFormat[]) result.toArray(new AudioFormat[result.size()]); } /** * Given a line description, return an array of descriptions of all * the matching target lines. * @param info the line description */ public static Line.Info[] getTargetLineInfo(Line.Info info) { HashSet result = new HashSet(); Mixer.Info[] infos = getMixerInfo(); for (int i = 0; i < infos.length; ++i) { Mixer mix = getMixer(infos[i]); Line.Info[] targs = mix.getTargetLineInfo(info); for (int j = 0; j < targs.length; ++j) result.add(targs[j]); } return (Line.Info[]) result.toArray(new Line.Info[result.size()]); } /** * Return true if the currently installed providers are able to * convert data from the given source format to the given target encoding. * @param targ the target encoding * @param source the source format */ public static boolean isConversionSupported(AudioFormat.Encoding targ, AudioFormat source) { Iterator i = ServiceFactory.lookupProviders(FormatConversionProvider.class); while (i.hasNext()) { FormatConversionProvider prov = (FormatConversionProvider) i.next(); if (prov.isConversionSupported(targ, source)) return true; } return false; } /** * Return true if the currently installed providers are able to convert * the given source format to the given target format. * @param targ the target format * @param source the source format */ public static boolean isConversionSupported(AudioFormat targ, AudioFormat source) { Iterator i = ServiceFactory.lookupProviders(FormatConversionProvider.class); while (i.hasNext()) { FormatConversionProvider prov = (FormatConversionProvider) i.next(); if (prov.isConversionSupported(targ, source)) return true; } return false; } private static boolean isFileTypeSupported(AudioFileFormat.Type[] types, AudioFileFormat.Type type) { for (int i = 0; i < types.length; ++i) { if (types[i].equals(type)) return true; } return false; } /** * Return true if the given audio file format is supported by one of * the providers installed on the system. * @param type the audio file format type */ public static boolean isFileTypeSupported(AudioFileFormat.Type type) { return isFileTypeSupported(getAudioFileTypes(), type); } /** * Return true if the given audio file format is supported for the * given audio input stream by one of the providers installed on the * system. * @param type the audio file format type * @param ais the audio input stream */ public static boolean isFileTypeSupported(AudioFileFormat.Type type, AudioInputStream ais) { return isFileTypeSupported(getAudioFileTypes(ais), type); } /** * Return true if some provider on the system supplies a line * matching the argument. * @param info the line to match */ public static boolean isLineSupported(Line.Info info) { Mixer.Info[] infos = getMixerInfo(); for (int i = 0; i < infos.length; ++i) { if (getMixer(infos[i]).isLineSupported(info)) return true; } return false; } /** * Write an audio input stream to the given file, using the specified * audio file format. All the providers installed on the system will * be searched to find one that supports this operation. * @param ais the audio input stream to write * @param type the desired audio file format type * @param out the file to write to * @return the number of bytes written * @throws IOException if an I/O error occurs while writing * @throws IllegalArgumentException if the file type is not supported */ public static int write(AudioInputStream ais, AudioFileFormat.Type type, File out) throws IOException { Iterator i = ServiceFactory.lookupProviders(AudioFileWriter.class); while (i.hasNext()) { AudioFileWriter w = (AudioFileWriter) i.next(); if (w.isFileTypeSupported(type, ais)) return w.write(ais, type, out); } throw new IllegalArgumentException("file type not supported by system"); } /** * Write an audio input stream to the given output stream, using the * specified audio file format. All the providers installed on the * system will be searched to find one that supports this operation. * @param ais the audio input stream to write * @param type the desired audio file format type * @param os the output stream to write to * @return the number of bytes written * @throws IOException if an I/O error occurs while writing * @throws IllegalArgumentException if the file type is not supported */ public static int write(AudioInputStream ais, AudioFileFormat.Type type, OutputStream os) throws IOException { Iterator i = ServiceFactory.lookupProviders(AudioFileWriter.class); while (i.hasNext()) { AudioFileWriter w = (AudioFileWriter) i.next(); if (w.isFileTypeSupported(type, ais)) return w.write(ais, type, os); } throw new IllegalArgumentException("file type not supported by system"); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -