直接贴代码:
#include <stdio.h>
#include <stdlib.h>
#include "playWav.h"
#include <alsa/asoundlib.h>
#include <iostream>
using namespace std;
void playSound(const char* filepath)
{
;
int i;
int ret;
int buf[128];
unsigned int val;
int dir=0;
char *buffer;
int size;
snd_pcm_uframes_t frames;
snd_pcm_uframes_t periodsize;
snd_pcm_t *playback_handle;//PCM设备句柄pcm.h
snd_pcm_hw_params_t *hw_params;//硬件信息和PCM流配置
/* if (argc != 2) {
printf("error: alsa_play_test [music name]\n");
exit(1);
}*/
cout << "start playing... wav from: /" << filepath << endl;
FILE *fp = fopen(filepath, "rb");
if(fp == NULL)
return 0;
fseek(fp, 100, SEEK_SET);
//1. 打开PCM,最后一个参数为0意味着标准配置
ret = snd_pcm_open(&playback_handle, "default", SND_PCM_STREAM_PLAYBACK, 0);
if (re