Home > Back-end >  Play music under Linux code, run directly no problem, sudo run error, the great god, please give dir
Play music under Linux code, run directly no problem, sudo run error, the great god, please give dir

Time:01-28

#include
#include
#include

Int main ()
{
int i; Int argc11=2;
Int ret.
Int buf [128].
Unsigned int val.
Int dir=0;
Char * buffer;
int size; Char * argv;
Argv="/opt/. Wc. Wav";
Snd_pcm_uframes_t frames;
Snd_pcm_uframes_t periodsize;
Snd_pcm_t * playback_handle;//PCM device handle PCM. H
Snd_pcm_hw_params_t * hw_params;//hardware information and PCM flow configuration
If (argc11!=2) {
Printf (" error: alsa_play_test [music name] \ n ");
The exit (1);
}
Printf (" play by Wolf song % s \ n ", argv);
The FILE * fp=fopen (argv, "rb");
If (fp==NULL)
return 0;
The fseek (fp, 100, SEEK_SET);

//1. Open the PCM, the last parameter is 0 means that the standard
Ret=snd_pcm_open (& amp; Playback_handle, "default", SND_PCM_STREAM_PLAYBACK, 0);
If (ret & lt; 0 {
Perror (" snd_pcm_open ");
The exit (1);
}

//2. Allocation snd_pcm_hw_params_t structure
Ret=snd_pcm_hw_params_malloc (& amp; Hw_params);
If (ret & lt; 0 {
Perror (" snd_pcm_hw_params_malloc ");
The exit (1);
}
//3. Initialization hw_params
Ret=snd_pcm_hw_params_any (playback_handle hw_params);
If (ret & lt; 0 {
Perror (" snd_pcm_hw_params_any ");
The exit (1);
}
//4. Initialize access
Ret=snd_pcm_hw_params_set_access (playback_handle hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
If (ret & lt; 0 {
Perror (" snd_pcm_hw_params_set_access ");
The exit (1);
}
//5. Initial sampling format SND_PCM_FORMAT_U8, eight
Ret=snd_pcm_hw_params_set_format (playback_handle hw_params, SND_PCM_FORMAT_S16_LE);
If (ret & lt; 0 {
Perror (" snd_pcm_hw_params_set_format ");
The exit (1);
}
//6. Set the sampling rate, if the hardware does not support we set the sampling rate, will use the most close to
//val=44100, some fixed 8 KHZ sampling frequency recording


Val=16000;
Ret=snd_pcm_hw_params_set_rate_near (playback_handle hw_params, & amp; Val, & amp; Dir);
If (ret & lt; 0 {
Perror (" snd_pcm_hw_params_set_rate_near ");
The exit (1);
}
//7. Set the channel number 1
Ret=snd_pcm_hw_params_set_channels (playback_handle hw_params, 1);
If (ret & lt; 0 {
Perror (" snd_pcm_hw_params_set_channels ");
The exit (1);
}

/* the Set period size to 32 frames. */
Frames=32;
Periodsize=frames;
Ret=snd_pcm_hw_params_set_buffer_size_near (playback_handle hw_params, & amp; Periodsize);
If (ret & lt; 0)
{
Printf (" Unable to set the buffer size % li: % s \ n ", frames * 2, snd_strerror (ret));

}
Periodsize/=2;

Ret=snd_pcm_hw_params_set_period_size_near (playback_handle hw_params, & amp; Periodsize, 0);
If (ret & lt; 0)
{
Printf (" Unable to set period li: size % % s \ n ", periodsize, snd_strerror (ret));
}

//8. Set hw_params
Ret=snd_pcm_hw_params (playback_handle hw_params);
If (ret & lt; 0 {
Perror (" snd_pcm_hw_params ");
The exit (1);
}

/* Use a buffer large enough to hold one period */
Snd_pcm_hw_params_get_period_size (hw_params, & amp; Frames, & amp; Dir);

Size=frames * 2;/* 2 bytes per sample, 2 channels */
Buffer=(char *) malloc (size);
Fprintf (stderr,
"Size=% d \ n",
The size);

While (1)
{
Ret=fread (buffer, 1, the size, fp);
If (ret==0)
{
Fprintf (stderr, "the end of the file on the input \ n");
break;
}
Else if (ret!=the size)
{
}
//9. Writing audio data to the PCM equipment
While (ret=snd_pcm_writei (playback_handle, buffer, frames) & lt; 0)
{
Usleep (2000);
If (ret==- EPIPE)
{
/* EPIPE means underrun */
Fprintf (stderr, "underrun occurred \ n");
//complete hardware parameter Settings, make the equipment ready to
Snd_pcm_prepare (playback_handle);
}
Else if (ret & lt; 0)
{
Fprintf (stderr,
"The error from writei: % s \ n",
Snd_strerror (ret));
}
}

}
//10. Close the PCM equipment to handle
Snd_pcm_close (playback_handle);

return 0;
}
  • Related