原创

SDL_PauseAudio函数

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://mysoft.blog.csdn.net/article/details/53508511
SDL_PauseAudio函数:

用此函数来暂停播放,或播放。根据参数来决定,如果参数是非0值就暂停,如果是0值就播放。

void SDL_PauseAudio(int pause_on)
例子:

int main(int argc, char* argv[])  
{  
    //Init  
    if(SDL_Init(SDL_INIT_AUDIO | SDL_INIT_TIMER)) {    
        printf( "Could not initialize SDL - %s\n", SDL_GetError());   
        return -1;  
    }  
    //SDL_AudioSpec  
    SDL_AudioSpec wanted_spec;  
    wanted_spec.freq = 44100;   
    wanted_spec.format = AUDIO_S16SYS;   
    wanted_spec.channels = 2;   
    wanted_spec.silence = 0;   
    wanted_spec.samples = 1024;   
    wanted_spec.callback = fill_audio;   
  
    if (SDL_OpenAudio(&wanted_spec, NULL)<0){   
        printf("can't open audio.\n");   
        return -1;   
    }   
  
    FILE *fp=fopen("../NocturneNo2inEflat_44.1k_s16le.pcm","rb+");  
    if(fp==NULL){  
        printf("cannot open this file\n");  
        return -1;  
    }  
    int pcm_buffer_size=4096;  
    char *pcm_buffer=(char *)malloc(pcm_buffer_size);  
    int data_count=0;  
  
    //Play  
    SDL_PauseAudio(0);  
  
    while(1){  
        if (fread(pcm_buffer, 1, pcm_buffer_size, fp) != pcm_buffer_size){  
            // Loop  
            fseek(fp, 0, SEEK_SET);  
            fread(pcm_buffer, 1, pcm_buffer_size, fp);  
            data_count=0;  
        }  
        printf("Now Playing %10d Bytes data.\n",data_count);  
        data_count+=pcm_buffer_size;  
        //Set audio buffer (PCM data)  
        audio_chunk = (Uint8 *) pcm_buffer;   
        //Audio buffer length  
        audio_len =pcm_buffer_size;  
        audio_pos = audio_chunk;  
          
        while(audio_len>0)//Wait until finish  
            SDL_Delay(1);   
    }  
    free(pcm_buffer);  
    SDL_Quit();  
  
    return 0;  
}  

蔡军生


老蔡学堂




展开阅读全文

没有更多推荐了,返回首页