我正在开发一个非常简单的音频播放器。它使用 Gtk 和 portaudio/libsndfile。我用浏览、播放等几个按钮创建了一个简单的测试界面。我的播放器正确选择了文件名,按下播放后开始播放。但是一切都在等待播放完成。没有什么是活跃的,我想知道如何在它自己完成之前阻止它。
我的 Gtk 代码是:
#include <stdio.h>
#include <gtk\gtk.h>
static GtkWidget *window;
const char *filename;
static void play_file (GtkButton *button, gpointer data)
{
sndFile_play(filename);
}
static gboolean delete_event( GtkWidget *widget,
GdkEvent *event,
gpointer data )
{
return FALSE;
}
static void destroy( GtkWidget *widget,
gpointer data )
{
gtk_main_quit ();
}
int main( int argc,
char *argv[] )
{
GtkWidget *button;
GtkWidget *box1;
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (window), "MyPlayer");
g_signal_connect (window, "delete-event",
G_CALLBACK (delete_event), NULL);
g_signal_connect (window, "destroy",
G_CALLBACK (destroy), NULL);
gtk_container_set_border_width (GTK_CONTAINER (window), 20);
box1 = gtk_hbox_new (FALSE, 0);
gtk_container_add (GTK_CONTAINER (window), box1);
button = gtk_button_new_with_label ("Browse...");
g_signal_connect (button, "clicked",
G_CALLBACK (browse_clicked), NULL); //not given here
gtk_box_pack_start (GTK_BOX(box1), button, TRUE, TRUE, 0);
gtk_widget_show (button);
button = gtk_button_new_with_label ("Play");
g_signal_connect (button, "clicked",
G_CALLBACK (play_file), NULL);
gtk_box_pack_start (GTK_BOX(box1), button, TRUE, TRUE, 0);
gtk_widget_show (button);
gtk_widget_show (box1);
gtk_widget_show (window);
gtk_main ();
return 0;
}
如果有必要,我会给出播放声音的代码:
#include <stdio.h>
#include <stdlib.h>
#include <portaudio.h>
#include <sndfile.h>
#define FRAMES_PER_BUFFER (1024)
#define PA_SAMPLE_TYPE paInt16
typedef short SAMPLE;
#define BUFFER_LEN 128
#define MAX_CHANNELS 2
int sndFile_play (const char *infilename){
PaStreamParameters outputParameters;
PaStream *stream;
PaError err;
static short data[BUFFER_LEN];
SNDFILE *infile;
SF_INFO sfinfo;
sf_count_t readcount;
int channels;
int srate;
err = Pa_Initialize();
if ( !( infile = sf_open( infilename, SFM_READ, &sfinfo ) ) ) {
printf ("Not able to open input file %s.\n", infilename);
puts (sf_strerror (NULL));
return 1;
}
if ( sfinfo.channels > MAX_CHANNELS ){
printf ("Not able to process more than %d channels\n", MAX_CHANNELS);
return 1;
}
/* FILE INFO */
channels = sfinfo.channels; // убрать channels
printf("number of channels %d\n", sfinfo.channels);
srate = sfinfo.samplerate; //убрать
printf("sample rate %d\n", sfinfo.samplerate);
outputParameters.device = Pa_GetDefaultOutputDevice();
outputParameters.channelCount = sfinfo.channels;
outputParameters.sampleFormat = PA_SAMPLE_TYPE;
outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
outputParameters.hostApiSpecificStreamInfo = NULL;
printf("Begin playback.....\n"); fflush(stdout);
err = Pa_OpenStream(
&stream,
NULL,
&outputParameters,
sfinfo.samplerate,
FRAMES_PER_BUFFER,
paClipOff,
NULL,
NULL );
if( stream ) {
err = Pa_StartStream( stream );
printf("Waiting for playback to finish....\n"); fflush(stdout);
while ( ( readcount = sf_read_short( infile, data, BUFFER_LEN*sfinfo.channels ) ) ){
err = Pa_WriteStream( stream, data, BUFFER_LEN );
}
err = Pa_CloseStream( stream );
printf("Done.\n"); fflush(stdout);
}
sf_close( infile );
Pa_Terminate();
return 0;
}