以下程序是可以正常运行的:
#include <alsa/asoundlib.h>
#include <math.h>
#include <pthread.h>
#include <string.h>
#define BUFFERSIZE 4096
#define PERIOD_SIZE 1024
#define PERIODS 2
#define SAMPLE_RATE 16000
#define CHANNELS 1
#define FSIZE 2*CHANNELS
/* Use the newer ALSA API */
#define ALSA_PCM_NEW_HW_PARAMS_API
long loops; //define the record time.
int rc; //return code.
int size;
snd_pcm_t *handle;
snd_pcm_hw_params_t *params;//定义参数变量
unsigned int val;
int dir;
snd_pcm_uframes_t frames;
char *buffer;
int err;
char *file;
int fd_pcm;
int read_flag1=0;
/*放音初始化*/
int init_pcm_play_dri(void)
{
/*snd_pcm_open (&pcm_handle, “default” , mode , 0)
snd_pcm_open是Alsa库提供的打开设备调用函数,
这里我们指定打开缺省的音频设备,并根据参数mode将
设备置为录音或是播放状态,如果设备打开成功,
pcm_handle便指向该设备句柄,我们用全局变量保存
起来,方便以后使用。*/
rc = snd_pcm_open(&handle, "default",SND_PCM_STREAM_PLAYBACK, 0);
if (rc < 0) {
fprintf(stderr,"unable to open pcm device: %s\n",snd_strerror(rc));
exit(1);
}
/* Allocate a hardware parameters object. */
snd_