wiki-export/wiki-content/DevFaqUseSounds.xml (181 lines of code) (raw):
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you 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.
--><mediawiki xmlns="http://www.mediawiki.org/xml/export-0.3/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="0.3" xml:lang="en" xsi:schemaLocation="http://www.mediawiki.org/xml/export-0.3/ http://www.mediawiki.org/xml/export-0.3.xsd">
<siteinfo>
<sitename>NetBeans Wiki</sitename>
<base>http://wiki.netbeans.org/Main_Page</base>
<generator>MediaWiki 1.15.1</generator>
<case>first-letter</case>
<namespaces>
<namespace key="-2">Media</namespace>
<namespace key="-1">Special</namespace>
<namespace key="0"/>
<namespace key="1">Talk</namespace>
<namespace key="2">User</namespace>
<namespace key="3">User talk</namespace>
<namespace key="4">NetBeans Wiki</namespace>
<namespace key="5">NetBeans Wiki talk</namespace>
<namespace key="6">File</namespace>
<namespace key="7">File talk</namespace>
<namespace key="8">MediaWiki</namespace>
<namespace key="9">MediaWiki talk</namespace>
<namespace key="10">Template</namespace>
<namespace key="11">Template talk</namespace>
<namespace key="12">Help</namespace>
<namespace key="13">Help talk</namespace>
<namespace key="14">Category</namespace>
<namespace key="15">Category talk</namespace>
</namespaces>
</siteinfo>
<page>
<title>DevFaqUseSounds</title>
<id>16516</id>
<revision>
<id>47562</id>
<timestamp>2011-09-19T21:46:28Z</timestamp>
<contributor>
<username>Javydreamercsw</username>
<id>2603</id>
</contributor>
<comment>Created page with 'This is quite straight forward. This is code from a module in SodBeans but its so simple that it is easier just to copy and paste the code. Add a dependency on Create your modu…'</comment>
<text xml:space="preserve">This is quite straight forward. This is code from a module in SodBeans but its so simple that it is easier just to copy and paste the code.
Add a dependency on
Create your module and add a file named SoundPlayer with this contents:
<pre>
package <your package>;
import java.io.File;
import org.openide.modules.InstalledFileLocator;
/**
*
* @author Andreas Stefik, with code borrowed from the web
*/
public class SoundPlayer {
private static String soundFileRoot = "sound";
private static String codeNameBase = "<code name base>";
private static File root = null;
private static SoundPlayer player = null;
/**
* @return the soundFileRoot
*/
public static String getSoundFileRoot() {
return soundFileRoot;
}
/**
* @param aSoundFileRoot the soundFileRoot to set
*/
public static void setSoundFileRoot(String aSoundFileRoot) {
soundFileRoot = aSoundFileRoot;
}
/**
* @return the codeNameBase
*/
public static String getCodeNameBase() {
return codeNameBase;
}
/**
* @param aCodeNameBase the codeNameBase to set
*/
public static void setCodeNameBase(String aCodeNameBase) {
codeNameBase = aCodeNameBase;
}
private SoundPlayer() {
File file = InstalledFileLocator.getDefault().locate(
soundFileRoot, codeNameBase, false);
root = file;
}
public static synchronized SoundPlayer instance() {
root = InstalledFileLocator.getDefault().locate(
soundFileRoot, codeNameBase, false);
if (player == null) {
player = new SoundPlayer();
}
return player;
}
public void play(String name) {
ThreadedSound sound = new ThreadedSound();
File file = new File(root.getAbsolutePath() + "/" + name);
String path = file.getAbsolutePath();
sound.setSoundFile(path);
Thread thread = new Thread(sound);
thread.start();
}
}
</pre>
Create your module and add a file named ThreadedSound with this contents:
<pre>
package <your package>;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sound.sampled.*;
/**
*
* @author Andreas Stefik
*/
public class ThreadedSound implements Runnable {
private final int BUFFER_SIZE = 128000;
private AudioInputStream audioStream;
private AudioFormat audioFormat;
private SourceDataLine sourceLine;
private String soundFile = "";
@Override
public void run() {
play();
}
private void play() {
try {
File path = new File(getSoundFile());
audioStream = AudioSystem.getAudioInputStream(path);
audioFormat = audioStream.getFormat();
DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
try {
sourceLine = (SourceDataLine) AudioSystem.getLine(info);
sourceLine.open(audioFormat);
} catch (LineUnavailableException ex) {
Logger.getLogger(ThreadedSound.class.getName()).log(Level.SEVERE, null, ex);
} catch (Exception ex) {
Logger.getLogger(SoundPlayer.class.getName()).log(Level.SEVERE, null, ex);
}
sourceLine.start();
int nBytesRead = 0;
byte[] abData = new byte[BUFFER_SIZE];
while (nBytesRead != -1) {
try {
nBytesRead = audioStream.read(abData, 0, abData.length);
} catch (IOException ex) {
Logger.getLogger(ThreadedSound.class.getName()).log(Level.SEVERE, null, ex);
}
if (nBytesRead >= 0) {
sourceLine.write(abData, 0, nBytesRead);
}
}
sourceLine.drain();
sourceLine.close();
} catch (UnsupportedAudioFileException ex) {
Logger.getLogger(ThreadedSound.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(ThreadedSound.class.getName()).log(Level.SEVERE, null, ex);
}
}
/**
* @return the soundFile
*/
public String getSoundFile() {
return soundFile;
}
/**
* @param soundFile the soundFile to set
*/
public void setSoundFile(String soundFile) {
this.soundFile = soundFile;
}
}
</pre>
To use it just place the sound file in the appropriate folder and use:
<pre>
SoundPlayer.instance().play("<file name>");
</pre>
To make your application talk, see [[DevFaqMakeItTalk]]</text>
</revision>
</page>
</mediawiki>