The Artima Developer Community
Sponsored Link

Java Answers Forum
please help

6 replies on 1 page. Most recent reply: May 23, 2002 1:36 AM by Maysoon

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 6 replies on 1 page
Maysoon

Posts: 64
Nickname: hm
Registered: Mar, 2002

please help Posted: May 18, 2002 1:02 AM
Reply to this message Reply
Advertisement
hello all...


what is the problem with the following code:

import javax.media.*;
import javax.media.bean.playerbean.*;
import javax.media.format.*;
import javax.media.protocol.*;
import java.io.*;
import java.util.*;


class fi{
static void main(String argv[]){
AudioFormat format= new AudioFormat (AudioFormat.LINEAR,
8000,
8,
1);

Vector devices= CaptureDeviceManager.getDeviceList( format);

CaptureDeviceInfo di= null;

if (devices.size() > 0) {
di = (CaptureDeviceInfo) devices.elementAt( 0);
}
else {
System.out.print("no capture found");
System.exit(-1);
}
}}



when i run the program the "no capture found" message appears,although
the microphone is already connected to my computer and it works well
in other programs.

please help..

thanks


Jay Kandy

Posts: 77
Nickname: jay
Registered: Mar, 2002

Re: please help Posted: May 18, 2002 1:26 PM
Reply to this message Reply
Not fixed yet huh? If it helps, how about a different sample rate like 16000 and sample size 16? Like:
new AudioFormat(AudioFormat.LINEAR, 16000, 16, 1);

Sincerely,
Jay

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: please help Posted: May 18, 2002 3:49 PM
Reply to this message Reply
On my machine, I am not getting "no capture found" messages.

If you are trying to record something from your microphone, you might try this little demo code:


/* RecordAndPlay.java
* @author: jbell
* @version: Mar 28, 2001
*/

import java.awt.*;
import java.awt.event.*;
import java.io.*;

import javax.swing.*;
import javax.sound.sampled.*;

public class RecordAndPlay implements ActionListener, Runnable {

Thread thread;
AudioInputStream ais;
boolean recordstatus = false;
boolean playstatus = false;
float samplerate = 11025.0f;
float framerate = 11025.0f;
int buffersize = 16384;

public static void main(String[] args){

RecordAndPlay recordandplay = new RecordAndPlay();
recordandplay.init();
}

public void init(){
JFrame frame = new JFrame("RecordAndPlay");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton recordbutton = new JButton("Record");
JButton stopbutton = new JButton("Stop");
JButton playbutton = new JButton("Play");
JButton savebutton = new JButton("Save");
JButton exitbutton = new JButton("Exit");
recordbutton.addActionListener(this);
stopbutton.addActionListener(this);
playbutton.addActionListener(this);
savebutton.addActionListener(this);
exitbutton.addActionListener(this);
JPanel panel = new JPanel();
panel.add(recordbutton);
panel.add(stopbutton);
panel.add(playbutton);
panel.add(savebutton);
panel.add(exitbutton);
frame.getContentPane().add(panel,"Center");
frame.show();
frame.pack();

}

public void actionPerformed(ActionEvent actionevent){
String command = actionevent.getActionCommand();
System.out.println(command);
if (command.compareTo("Record")==0){
recordstatus = true;
playstatus = false;
start();
} else if (command.compareTo("Stop")==0){
stop();
} else if (command.compareTo("Play")==0){
recordstatus = false;
playstatus = true;
start();
}else if (command.compareTo("Save")==0){
saveToFile();
}else if (command.compareTo("Exit")==0){
if (thread != null) stop();
System.exit(0);
}

}

public void start() {
thread = new Thread(this);
thread.start();
}

public void stop() {
recordstatus = false;
playstatus = false;
thread = null;
}

public void run(){
if ((recordstatus)&&(!playstatus)) record();
if ((playstatus)&& (!recordstatus)) playback();
}

public void playback(){
SourceDataLine line;
if (ais == null) {
System.out.println("You need to record something first");
playstatus = false;
return;
}
// reset to the beginnning of the stream
try {
ais.reset();
} catch (Exception e) {
System.out.println("Unable to reset the stream\n" + e);
return;
}

// get an AudioInputStream of the desired audioformat for playback
AudioFormat audioformat = new AudioFormat(AudioFormat.Encoding.PCM_UNSIGNED,
samplerate, 8, 1, 1, framerate,false);
AudioInputStream playbackstream = AudioSystem.getAudioInputStream(audioformat, ais);

if (playbackstream == null) {
System.out.println("Unable to convert stream of audioformat " + ais + " to audioformat " + audioformat);
return;
}


// define the required attributes for our line,
// and make sure a compatible line is supported.

DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioformat);
if (!AudioSystem.isLineSupported(info)) {
System.out.println("Line matching " + info + " not supported.");
return;
}
// get and open the source data line for playback.
try{
line = (SourceDataLine) AudioSystem.getLine(info);
line.open(audioformat, buffersize);
}catch (LineUnavailableException lue) {
System.out.println("Unable to open the line to the microphone input.");
System.out.println("LineUnavailableException: " + lue.getMessage());
return;
}
// play back the captured audio data
int frameSizeInBytes = audioformat.getFrameSize();
int bufferLengthInFrames = line.getBufferSize() / 8;
int bufferLengthInBytes = bufferLengthInFrames * frameSizeInBytes;
byte[] data = new byte[bufferLengthInBytes];
int bytenumber = 0;
// start the source data line
line.start();
while (thread != null) {
try {
if ((bytenumber = playbackstream.read(data)) == -1) {
break;
}
int numBytesRemaining = bytenumber;
while (numBytesRemaining > 0) {
numBytesRemaining -= line.write(data, 0, numBytesRemaining);
}
} catch (Exception e) {
System.out.println("Error during playback: " + e);
System.out.println("Exception: " + e.getMessage());
break;
}

}
if (thread != null) {
line.drain();
}
line.stop();
line.close();
line = null;
}

public void record() {
TargetDataLine line = null;
// define the required attributes for our line,
// and make sure a compatible line is supported.
AudioFormat audioformat = new AudioFormat(AudioFormat.Encoding.PCM_UNSIGNED,
samplerate, 8, 1, 1, framerate,false);
DataLine.Info info = new DataLine.Info(TargetDataLine.class,audioformat);
if (!AudioSystem.isLineSupported(info)) {
System.out.println("Line matching " + info + " not supported.");
System.exit(0);
}
// get and open the target data line for capture.
try{
line = (TargetDataLine) AudioSystem.getLine(info);
line.open(audioformat, line.getBufferSize());
}catch (LineUnavailableException lune) {
System.err.println("Unable to open the line: " + lune.getMessage());
System.exit(0);
}catch (SecurityException se) {
System.err.println("SecurityException: " + se.toString());
System.exit(0);
}catch(Exception ex) {
System.err.println("Exception: " + ex.getMessage());
System.exit(0);
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int frameSizeInBytes = audioformat.getFrameSize();
int bufferLengthInFrames = line.getBufferSize() / 8;
int bufferLengthInBytes = bufferLengthInFrames * frameSizeInBytes;
byte[] data = new byte[bufferLengthInBytes];
int bytenumber = 0;
line.start();
System.out.println("Started Recording");
while (thread != null) {
if((bytenumber = line.read(data, 0, bufferLengthInBytes)) == -1) {
break;
}
baos.write(data, 0, bytenumber);
}
System.out.println("Stopped Recording");
//stop and close the line
line.stop();
line.close();
line = null;
// stop and close the output stream
try{
baos.flush();
baos.close();
}catch (IOException ioe) {
System.err.println(ioe.getMessage());
}
// load bytes into the audio input stream for playback
byte audioBytes[] = baos.toByteArray();
ByteArrayInputStream bais = new ByteArrayInputStream(audioBytes);
ais = new AudioInputStream(bais, audioformat, audioBytes.length / frameSizeInBytes);
try{
ais.reset();
}catch (Exception e) {
System.err.println("Exception: " + e.getMessage());
return;
}
}

public void saveToFile() {

AudioFileFormat.Type audiofileformattype = AudioFileFormat.Type.WAVE;

if (ais == null) {
System.out.println("No recorded audio to save");
return;
}

// reset to the beginnning of the recorded data
try{
ais.reset();
}catch (Exception e) {
System.err.println("Unable to reset stream " + e);
return;
}

File file = getFileToSave();
try {
if (AudioSystem.write(ais, audiofileformattype, file) == -1) {
throw new IOException("Problems writing to file");
}
}catch (IOException ioe) {
System.err.println("IOException: " + ioe.getMessage());
}catch (Exception e) {
System.err.println("Exception: " + e.getMessage());
}
}
/** Uses the showSaveDialog method of JFileChooser to request the user to
* select the file to save to
*/
public File getFileToSave(){
File file = null;
JFrame jframe = new JFrame();
JFileChooser chooser = new JFileChooser(".");
int returnvalue = chooser.showSaveDialog(jframe);
if(returnvalue == JFileChooser.APPROVE_OPTION) {
file = chooser.getSelectedFile();
}
return file;
}

}

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: please help Posted: May 18, 2002 3:51 PM
Reply to this message Reply
The following program will print out a lot of info about your audio system:


/* AudioSystemExplorer.java
* @author: Charles Bell
* @version: May 12, 2001
*/

import javax.sound.sampled.*;

public class AudioSystemExplorer{

public static void main(String[] args){

AudioSystemExplorer audiosystemexplorer = new AudioSystemExplorer();
audiosystemexplorer.explore();
System.exit(0);
}


public void explore(){
displayMixerInfo();
displayAudioSystemInfo();
}

public void displayAudioSystemInfo(){
try{
Line compactdiskline = null;
Line headphoneline = null;
Line lineinline = null;
Line lineoutline = null;
Line speakerline = null;
Line microphoneline = null;
System.out.println("This System supports:");
if (AudioSystem.isLineSupported(Port.Info.COMPACT_DISC)) {
System.out.println("COMPACT_DISC");
compactdiskline = (Port) AudioSystem.getLine(
Port.Info.COMPACT_DISC);
}
if (AudioSystem.isLineSupported(Port.Info.HEADPHONE)) {
System.out.println("HEADPHONE");
headphoneline = (Port) AudioSystem.getLine(
Port.Info.HEADPHONE);
}
if (AudioSystem.isLineSupported(Port.Info.LINE_IN)) {
System.out.println("LINE_IN");
lineinline = (Port) AudioSystem.getLine(
Port.Info.LINE_IN);
}
if (AudioSystem.isLineSupported(Port.Info.LINE_OUT)) {
System.out.println("LINE_OUT");
lineoutline = (Port) AudioSystem.getLine(
Port.Info.LINE_OUT);
}
if (AudioSystem.isLineSupported(Port.Info.MICROPHONE)) {
System.out.println("MICROPHONE");
microphoneline = (Port) AudioSystem.getLine(
Port.Info.MICROPHONE);
}
if (AudioSystem.isLineSupported(Port.Info.SPEAKER)) {
System.out.println("SPEAKER");
speakerline = (Port) AudioSystem.getLine(
Port.Info.SPEAKER);
}


}catch(LineUnavailableException lue){
System.out.println("LineUnavailableException: " + lue.getMessage());
}
}

public void displayMixerInfo(){
Mixer.Info[] mixerinfo = AudioSystem.getMixerInfo();
System.out.println("This System has the following Mixers which can be used as Sound Resources:");
for (int i=0;i<mixerinfo.length;i++){
String description = mixerinfo[i].getDescription();
String name = mixerinfo[i].getName();
String vendor = mixerinfo[i].getVendor();
String version = mixerinfo[i].getVersion();
System.out.println("Name: " + name);
System.out.println("Vendor: " + vendor);
System.out.println("Version: " + version);
System.out.println("Description: " + description);
Mixer mixer = AudioSystem.getMixer(mixerinfo[i]);
Line.Info[] sourcelineinfo = mixer.getSourceLineInfo();
System.out.println("This Mixer has the following SourceLines:");
for (int s = 0;s<sourcelineinfo.length;s++){
System.out.println(sourcelineinfo[s].toString());
Class sourcelineclass = sourcelineinfo[s].getLineClass();
System.out.println("Source Line Class name: " + sourcelineclass.getName());
try{
Line line = AudioSystem.getLine(sourcelineinfo[s]);
if (line.isOpen()){
System.out.println("This line is open.");
}else{
System.out.println("This line is closed.");
}
if (sourcelineclass.getName().indexOf("Clip") >= 0){
Clip clip = (Clip)line;
System.out.println("FrameLength: " + String.valueOf(clip.getFrameLength()));
System.out.println("MicrosecondLength: " + String.valueOf(clip.getMicrosecondLength()));
}else if (sourcelineclass.getName().indexOf("SourceDataLine") >= 0){
if (((DataLine)line).isActive()) {
System.out.println("This line is active.");
}else{
System.out.println("This line is inactive.");
}
}else if (sourcelineclass.getName().indexOf("TargetDataLine") >= 0){
if (((DataLine)line).isActive()) {
System.out.println("This line is active.");
}else{
System.out.println("This line is inactive.");
}

}else if (sourcelineclass.getName().indexOf("DataLine") >= 0){
if (((DataLine)line).isActive()) {
System.out.println("This line is active.");
}else{
System.out.println("This line is inactive.");
}

}
}catch(LineUnavailableException lue){
System.out.println("LineUnavailableException: " + lue.getMessage());
}
}
Line.Info[] targetlineinfo = mixer.getTargetLineInfo();
System.out.println("This System has the following TargetLines:");
for (int t = 0;t<targetlineinfo.length;t++){
System.out.println(targetlineinfo[t].toString());
Class targetlineclass = targetlineinfo[t].getLineClass();
System.out.println("Target Line Class name: " + targetlineclass.getName());
try{
Line line = AudioSystem.getLine(targetlineinfo[t]);
if (line.isOpen()){
System.out.println("This line is open.");
}else{
System.out.println("This line is closed.");
}
if (targetlineclass.getName().indexOf("Clip") >= 0){
Clip clip = (Clip)line;
System.out.println("FrameLength: " + String.valueOf(clip.getFrameLength()));
System.out.println("MicrosecondLength: " + String.valueOf(clip.getMicrosecondLength()));
}else if (targetlineclass.getName().indexOf("SourceDataLine") >= 0){
if (((DataLine)line).isActive()) {
System.out.println("This line is active.");
}else{
System.out.println("This line is inactive.");
}
}else if (targetlineclass.getName().indexOf("TargetDataLine") >= 0){
if (((DataLine)line).isActive()) {
System.out.println("This line is active.");
}else{
System.out.println("This line is inactive.");
}

}else if (targetlineclass.getName().indexOf("DataLine") >= 0){
if (((DataLine)line).isActive()) {
System.out.println("This line is active.");
}else{
System.out.println("This line is inactive.");
}

}

}catch(LineUnavailableException lue){
System.out.println("LineUnavailableException: " + lue.getMessage());
}
}

System.out.println("-----------------------------");
}
}
}

Maysoon

Posts: 64
Nickname: hm
Registered: Mar, 2002

Re: please help Posted: May 19, 2002 12:00 AM
Reply to this message Reply
ok charles,thank you for your program record and play
it works well ,but this program uses javax.sound.sampled;
but i am using jmf package "javax.media.*;

so i want to know what is the problem with my code?

sometimes the following message appears:
"no capture found in file registry"


thanks

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: please help Posted: May 19, 2002 8:24 AM
Reply to this message Reply
PCs vary with the type of audio format supported.
Run this program and see if one of these formats is supported: LINEAR, ALAW, ULAW, GSM.
There are other formats if you look at the API documentation. Once you have found an audio format that your sound card supports, then you have to specify the rest of the info to create a specific audio format type. Running the following program will help explore your pc, so you can specify a format that will work. Otherwise, you can put try catch blocks to try the various audio formats until one works for the general user of your program.

You can also revise the line:

AudioFormat format= new AudioFormat (AudioFormat.LINEAR, 8000,8,1);


to:

AudioFormat format= new AudioFormat (AudioFormat.LINEAR);


If all you want is just one LINEAR device that will work.




import java.util.*;

import javax.media.*;
import javax.media.format.*;
import javax.media.protocol.*;


public class ExploreCaptureDevices{

public static void main(String[] args){
/* JMF supports audio sample rates from 8KHz to 48KHz.
* Note that cross-platform version of JMF only supports the following rates:
* 8, 11.025, 11.127, 16, 22.05, 22.254, 32, 44.1, and 48 KHz.
*/
/* Get a list of CaptureDeviceInfo objects that correspond to devices that can
* capture data in the specified Format. If no Format is specified, this method
* returns a list of CaptureDeviceInfo objects for all of the available capture
* devices.
*/
System.out.println("Exploring LINEAR AudioFormat");
Vector linearCaptureDevices = CaptureDeviceManager.getDeviceList(new AudioFormat(AudioFormat.LINEAR));
if (linearCaptureDevices.size() > 0){
ListIterator iterator = linearCaptureDevices.listIterator();
while (iterator.hasNext()){
CaptureDeviceInfo nextInfoObject = (CaptureDeviceInfo)iterator.next();
System.out.println("Formats supported for " + nextInfoObject.getName());
Format[] formats = nextInfoObject.getFormats();
for (int i = 0; i < formats.length; i++){
Format nextFormat = formats[i];
System.out.println(nextFormat.toString());
}
}
}else{
System.out.println("Your PC does not support LINEAR AudioFormat");
}

System.out.println("Exploring ALAW AudioFormat");
Vector alawCaptureDevices = CaptureDeviceManager.getDeviceList(new AudioFormat(AudioFormat.ALAW));
if (alawCaptureDevices.size() > 0){
ListIterator iterator = alawCaptureDevices.listIterator();
while (iterator.hasNext()){
CaptureDeviceInfo nextInfoObject = (CaptureDeviceInfo)iterator.next();
System.out.println("Formats supported for " + nextInfoObject.getName());
Format[] formats = nextInfoObject.getFormats();
for (int i = 0; i < formats.length; i++){
Format nextFormat = formats[i];
System.out.println(nextFormat.toString());
}
}
}else{
System.out.println("Your PC does not support ALAW AudioFormat");
}

System.out.println("Exploring ULAW AudioFormat");
Vector ulawCaptureDevices = CaptureDeviceManager.getDeviceList(new AudioFormat(AudioFormat.ULAW));
if (alawCaptureDevices.size() > 0){
ListIterator iterator = ulawCaptureDevices.listIterator();
while (iterator.hasNext()){
CaptureDeviceInfo nextInfoObject = (CaptureDeviceInfo)iterator.next();
System.out.println("Formats supported for " + nextInfoObject.getName());
Format[] formats = nextInfoObject.getFormats();
for (int i = 0; i < formats.length; i++){
Format nextFormat = formats[i];
System.out.println(nextFormat.toString());
}
}
}else{
System.out.println("Your PC does not support ULAW AudioFormat");
}

System.out.println("Exploring GSM AudioFormat");
Vector gsmCaptureDevices = CaptureDeviceManager.getDeviceList(new AudioFormat(AudioFormat.GSM));
if (gsmCaptureDevices.size() > 0){
ListIterator iterator = gsmCaptureDevices.listIterator();
while (iterator.hasNext()){
CaptureDeviceInfo nextInfoObject = (CaptureDeviceInfo)iterator.next();
System.out.println("Formats supported for " + nextInfoObject.getName());
Format[] formats = nextInfoObject.getFormats();
for (int i = 0; i < formats.length; i++){
Format nextFormat = formats[i];
System.out.println(nextFormat.toString());
}
}
}else{
System.out.println("Your PC does not support GSM AudioFormat");
}
}
}

Maysoon

Posts: 64
Nickname: hm
Registered: Mar, 2002

Re: please help Posted: May 23, 2002 1:36 AM
Reply to this message Reply
ok i have run the last program ,but there is a compilation error you have assigned format[] type to format in the following:

Format[] formats = nextInfoObject.getFormats();

Format nextFormat = formats;

i have change nextFormat to format[] and the result is:

there is any type is supported.

someone tell me to use the JMFInit
i have run the JMFInit ,the following message appears in the window of
the program:

looking for Audio capturer
Finished detecting javasound capturer
Looking for video capture devices
Capture device detection failed!


and the program exits,then the following messages appear in the dos:


javasound capture supported=true
Exception on commit=java.io.ioexception can not find registry file
javaSoundAuto: committed ok
java.lang.reflect.InvocationTargetException


*********

i do not know what is the registry file,the mic is already connected
to my computer an it works well with other programs that do not use
jmf .
so what i sould do please help me...


thanks

Flat View: This topic has 6 replies on 1 page
Topic: FTP question Previous Topic   Next Topic Topic: Urgent:Can you give me the Example how to use c methods in java

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use