简体   繁体   English

使用SDL在后台播放MP3时产生声音效果

[英]Generating sound effect while playing MP3 in the background in with SDL

I'm writing a program which constantly generates sound effects and output them to a sound buffer in a SDL callback function. 我正在编写一个程序,该程序会不断生成声音效果,并将其输出到SDL回调函数中的声音缓冲区。 Now I need to play a MP3 in the background as well. 现在,我还需要在后台播放MP3。 How do I do this? 我该怎么做呢? Do I need to decode the MP3 and mix it with the sound effects myself? 我需要解码MP3并将其自己与声音效果混合吗?

I can change to another library if necessary. 如有必要,我可以转到另一个图书馆。

you have to create a thread that play your MP3 , you can use the "pthread lib " you just have to add the pthread lib in your project ( do not need to download anything ) 您必须创建一个播放MP3的线程,您可以使用“ pthread lib”,只需在项目中添加pthread lib(无需下载任何内容)

in a thread your song will be running without distrurb your main 在一个线程中,您的歌曲将在不干扰主音的情况下运行

i write you an example using thread 我用线程给你写一个例子

#include <pthread.h>

void * Play(void * ptr)
{
   /*
      HERE PLAY your MP3 
      you can also do a loop inside your thread
   */


   return NULL;
}

//in your main for example
int main()
{

    //create a thread that play yout song in backround of main 
    pthread_t thread_play_MP3;
    pthread_create(&thread_play_MP3,NULL,Play,NULL);

    while(1)
    {
       //  HERE : your main code 
    }

    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM