从网上找到了一个 读取声音的程序

 

/*

Ubuntu 10.04下测试通过

 *

 * Records several seconds of sound, then echoes it back.

 * Runs until Control-C is pressed.

 */

 

#include <unistd.h>

#include <fcntl.h>

#include <sys/types.h>

#include <sys/ioctl.h>

#include <stdlib.h>

#include <stdio.h>

#include <linux/soundcard.h>

 

#define LENGTH 3    /* how many seconds of speech to store */

#define RATE 8000   /* the sampling rate */

#define SIZE 8      /* sample size: 8 or 16 bits */

#define CHANNELS 1  /* 1 = mono 2 = stereo */

 

/* this buffer holds the digitized audio */

unsigned char buf[LENGTH*RATE*SIZE*CHANNELS/8];

 

int main()

{

  int fd; /* sound device file descriptor */

  int arg; /* argument for ioctl calls */

  int status;   /* return status of system calls */

 

  char *audio = "/dev/audio";

 

  /* open sound device */

  fd = open(audio, O_RDWR);

  if (fd < 0) {

    perror("open of failed");

    exit(1);

  }

 

  /* set sampling parameters */

  arg = SIZE;   /* sample size */

  status = ioctl(fd, SOUND_PCM_WRITE_BITS, &arg);

  if (status == -1)

    perror("SOUND_PCM_WRITE_BITS ioctl failed");

  if (arg != SIZE)

    perror("unable to set sample size");

 

  arg = CHANNELS;  /* mono or stereo */

  status = ioctl(fd, SOUND_PCM_WRITE_CHANNELS, &arg);

  if (status == -1)

    perror("SOUND_PCM_WRITE_CHANNELS ioctl failed");

  if (arg != CHANNELS)

    perror("unable to set number of channels");

 

  arg = RATE;   /* sampling rate */

  status = ioctl(fd, SOUND_PCM_WRITE_RATE, &arg);

  if (status == -1)

    perror("SOUND_PCM_WRITE_WRITE ioctl failed");

 

  while (1) { /* loop until Control-C */

    printf("Say something:/n");

    status = read(fd, buf, sizeof(buf)); /* record some sound */

    if (status != sizeof(buf))

      perror("read wrong number of bytes");

    printf("You said:/n");

    status = write(fd, buf, sizeof(buf)); /* play it back */

    if (status != sizeof(buf))

      perror("wrote wrong number of bytes");

    /* wait for playback to complete before recording again */

    status = ioctl(fd, SOUND_PCM_SYNC, 0);

  if (status == -1)

    perror("SOUND_PCM_SYNC ioctl failed");

  }

}

 

在 Java 中,我们可以利用 `AudioSystem` 类以及其内的 `getLineInfo()` 和 `getLine()` 方法来获取和设置音频设备的信息,包括音量。不过要注意的是,直接控制麦克风输入(即扬声器的声音大小)通常是操作系统级别的操作,并不容易通过普通的 Java 应用程序直接实现。 下面是一个简单的示例,展示如何读取音频设备信息: ```java import javax.sound.sampled.AudioSystem; import javax.sound.sampled.Line.Info; public class VolumeControlExample { public static void main(String[] args) throws Exception { // 获取所有音频输入流信息 Info[] info = AudioSystem.getLines(new Line.Info(TargetDataLine.class)); if (info != null && info.length > 0) { for (Info i : info) { System.out.println("Device Info: " + i.getName()); TargetDataLine line = (TargetDataLine) AudioSystem.getLine(i); // 初始化并打开设备 line.open(line.getDefaultSampleRate(), line.getType().getPrecision(), line.getFormat()); line.start(); int currentVolume = ((Line) line).getMixer().getControl(FloatControl.Type.MASTER_GAIN).getValue(); System.out.println("Current volume level is: " + Math.abs(currentVolume / 65535f * 100) + "%"); // 关闭设备 line.close(); } } else { System.err.println("No audio devices found."); } } } ``` 这段代码首先尝试找到所有可用的音频输入设备,并对每个设备打印出相关信息,包括当前的音量水平。请注意,获取和修改音频设备的音量通常需要特定的操作系统权限,并且对于非用户级应用程序来说可能不可行。 **
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值