Part of Slepp's ProjectsPastebinTURLImagebinFilebin
Feedback -- English French German Japanese
Create Upload Newest Tools Donate

Advertising

Someone
Tuesday, October 13th, 2015 at 10:21:27am UTC 

  1. /*==============================================================================
  2. Net Stream Example
  3. Copyright (c), Firelight Technologies Pty, Ltd 2004-2015.
  4.  
  5. This example shows how to play streaming audio from an Internet source
  6. ==============================================================================*/
  7. #include "fmod.hpp"
  8. #include "common.h"
  9.  
  10. static FMOD_RESULT F_CALLBACK dummy_open(FMOD_CODEC_STATE *codec, FMOD_MODE usermode, FMOD_CREATESOUNDEXINFO *userexinfo);
  11. static FMOD_RESULT F_CALLBACK dummy_close(FMOD_CODEC_STATE *codec);
  12. static FMOD_RESULT F_CALLBACK dummy_read(FMOD_CODEC_STATE *codec, void *buffer, unsigned int size, unsigned int *read);
  13. static FMOD_RESULT F_CALLBACK dummy_setposition(FMOD_CODEC_STATE *codec, int subsound, unsigned int position, FMOD_TIMEUNIT postype);
  14.  
  15. static FMOD_CODEC_DESCRIPTION dummy_codec_desc =
  16. {
  17.     "FMOD dummy plugin",                 // Name.
  18.     0x00010000,                         // Version 0xAAAABBBB   A = major, B = minor.
  19.     0,                                  // Don't force everything using this codec to be a stream
  20.     FMOD_TIMEUNIT_PCM,                  // The time format we would like to accept into setposition/getposition.
  21.     &dummy_open,                         // Open callback.
  22.     &dummy_close,                        // Close callback.
  23.     &dummy_read,                         // Read callback.
  24.     0,                                  // Getlength callback.  (If not specified FMOD return the length in FMOD_TIMEUNIT_PCM, FMOD_TIMEUNIT_MS or FMOD_TIMEUNIT_PCMBYTES units based on the lengthpcm member of the FMOD_CODEC structure).
  25.     &dummy_setposition,                  // Setposition callback.
  26.     0,                                  // Getposition callback. (only used for timeunit types that are not FMOD_TIMEUNIT_PCM, FMOD_TIMEUNIT_MS and FMOD_TIMEUNIT_PCMBYTES).
  27.     0                                   // Sound create callback (don't need it)
  28. };
  29.  
  30.  
  31. F_DECLSPEC F_DLLEXPORT FMOD_CODEC_DESCRIPTION * F_API FMODGetDummyCodecDescription()
  32. {
  33.     return &dummy_codec_desc;
  34. }
  35.  
  36. static FMOD_RESULT F_CALLBACK dummy_open(FMOD_CODEC_STATE *codec, FMOD_MODE usermode, FMOD_CREATESOUNDEXINFO *userexinfo)
  37. {
  38.     return FMOD_ERR_FORMAT;
  39. }
  40.  
  41. static FMOD_RESULT F_CALLBACK dummy_close(FMOD_CODEC_STATE *codec)
  42. {
  43.     codec->plugindata = 0;
  44.     return FMOD_OK;
  45. }
  46.  
  47. static FMOD_RESULT F_CALLBACK dummy_read(FMOD_CODEC_STATE *codec, void *buffer, unsigned int size, unsigned int *read)
  48. {
  49.     return FMOD_ERR_FILE_BAD;
  50.  
  51. }
  52.  
  53. static FMOD_RESULT F_CALLBACK dummy_setposition(FMOD_CODEC_STATE *codec, int subsound, unsigned int position, FMOD_TIMEUNIT postype)
  54. {
  55.     return FMOD_ERR_FILE_BAD;
  56. }
  57.  
  58. int FMOD_Main()
  59. {
  60.     FMOD::System    *system = 0;
  61.     FMOD::Sound     *sound = 0;
  62.     FMOD::Channel   *channel = 0;
  63.     FMOD_RESULT      result = FMOD_OK;
  64.     FMOD_OPENSTATE   openstate = FMOD_OPENSTATE_READY;
  65.     unsigned int     version = 0;
  66.     void            *extradriverdata = 0;
  67.     const int        tagcount = 4;
  68.     int              tagindex = 0;
  69.     char             tagstring[tagcount][128] = { 0 };
  70.    
  71.     Common_Init(&extradriverdata);
  72.    
  73.     /*
  74.         Create a System object and initialize.
  75.     */
  76.     result = FMOD::System_Create(&system);
  77.     ERRCHECK(result);
  78.  
  79.     result = system->getVersion(&version);
  80.     ERRCHECK(result);
  81.  
  82.     if (version < FMOD_VERSION)
  83.     {
  84.         Common_Fatal("FMOD lib version %08x doesn't match header version %08x", version, FMOD_VERSION);
  85.     }
  86.  
  87.     result = system->init(1, FMOD_INIT_NORMAL, extradriverdata);
  88.     ERRCHECK(result);
  89.  
  90.     result = system->registerCodec(FMODGetDummyCodecDescription(), 0, 599);
  91.     ERRCHECK(result);
  92.  
  93.     /* Increase the file buffer size a little bit to account for Internet lag. */
  94.     result = system->setStreamBufferSize(128*1024, FMOD_TIMEUNIT_RAWBYTES);
  95.     ERRCHECK(result);
  96.  
  97.     FMOD_CREATESOUNDEXINFO info;
  98.     memset(&info, 0, sizeof(info));
  99.     info.cbsize = sizeof(info);
  100.  
  101.     const char* url = "http://shoutmedia.abc.net.au:10426";
  102.     url = "http://uploadserver.oss.aliyuncs.com/745/699745/1443431047-55.mp3";
  103.     result = system->createSound(url, FMOD_CREATESTREAM| FMOD_NONBLOCKING, &info, &sound);
  104.     ERRCHECK(result);
  105.  
  106.     /*
  107.         Main loop
  108.     */
  109.     do
  110.     {
  111.         unsigned int    pos = 0;
  112.         unsigned int    percent = 0;
  113.         bool            playing = false;
  114.         bool            paused = false;
  115.         bool            starving = false;
  116.         const char     *state = "Stopped";
  117.        
  118.         Common_Update();
  119.  
  120.         if (Common_BtnPress(BTN_ACTION1))
  121.         {
  122.             if (channel)
  123.             {
  124.                 bool paused = false;
  125.                
  126.                 result = channel->getPaused(&paused);
  127.                 ERRCHECK(result);
  128.                 result = channel->setPaused(!paused);
  129.                 ERRCHECK(result);
  130.             }
  131.         }
  132.        
  133.         result = system->update();
  134.         ERRCHECK(result);
  135.        
  136.         result = sound->getOpenState(&openstate, &percent, &starving, 0);
  137.         ERRCHECK(result);
  138.        
  139.         if (channel)
  140.         {
  141.             FMOD_TAG tag;
  142.        
  143.             /*
  144.                 Read any tags that have arrived, this could happen if a radio station switches
  145.                 to a new song.
  146.             */
  147.             while (sound->getTag(0, -1, &tag) == FMOD_OK)
  148.             {
  149.                 if (tag.datatype == FMOD_TAGDATATYPE_STRING)
  150.                 {
  151.                     sprintf(tagstring[tagindex], "%s = '%s' (%d bytes)", tag.name, (char *)tag.data, tag.datalen);
  152.                     tagindex = (tagindex + 1) % tagcount;
  153.                 }
  154.                 else if (tag.type == FMOD_TAGTYPE_FMOD)
  155.                 {
  156.                     /* When a song changes, the sample rate may also change, so compensate here. */
  157.                     if (!strcmp(tag.name, "Sample Rate Change"))
  158.                     {
  159.                         float frequency = *((float *)tag.data);
  160.                        
  161.                         result = channel->setFrequency(frequency);
  162.                         ERRCHECK(result);
  163.                     }
  164.                 }
  165.             }
  166.            
  167.             result = channel->getPaused(&paused);
  168.             ERRCHECK(result);
  169.            
  170.             result = channel->isPlaying(&playing);
  171.             ERRCHECK(result);
  172.            
  173.             result = channel->getPosition(&pos, FMOD_TIMEUNIT_MS);
  174.             ERRCHECK(result);
  175.            
  176.             /* Silence the stream until we have sufficient data for smooth playback. */
  177.             result = channel->setMute(starving);
  178.             ERRCHECK(result);
  179.         }
  180.         else
  181.         {
  182.              /* This may fail if the stream isn't ready yet, so don't check the error code. */
  183.             if (openstate == FMOD_OPENSTATE_READY)
  184.             {
  185.                 result = system->playSound(sound, 0, false, &channel);
  186.                 ERRCHECK(result);
  187.             }
  188.         }
  189.        
  190.         if (openstate == FMOD_OPENSTATE_BUFFERING)
  191.         {
  192.             state = "Buffering...";
  193.         }
  194.         else if (openstate == FMOD_OPENSTATE_CONNECTING)
  195.         {
  196.             state = "Connecting...";
  197.         }
  198.         else if (paused)
  199.         {
  200.             state = "Paused";
  201.         }
  202.         else if (playing)
  203.         {
  204.             state = "Playing";
  205.         }
  206.  
  207.         Common_Draw("==================================================");
  208.         Common_Draw("Net Stream Example.");
  209.         Common_Draw("Copyright (c) Firelight Technologies 2004-2015.");
  210.         Common_Draw("==================================================");
  211.         Common_Draw("");
  212.         Common_Draw("Press %s to toggle pause", Common_BtnStr(BTN_ACTION1));
  213.         Common_Draw("Press %s to quit", Common_BtnStr(BTN_QUIT));
  214.         Common_Draw("");
  215.         Common_Draw("Time = %02d:%02d:%02d", pos / 1000 / 60, pos / 1000 % 60, pos / 10 % 100);
  216.         Common_Draw("State = %s %s", state, starving ? "(STARVING)" : "");
  217.         Common_Draw("Buffer Percentage = %d", percent);
  218.         Common_Draw("");
  219.         Common_Draw("Tags:");
  220.         for (int i = tagindex; i < (tagindex + tagcount); i++)
  221.         {
  222.             Common_Draw("%s", tagstring[i % tagcount]);
  223.             Common_Draw("");
  224.         }
  225.  
  226.         Common_Sleep(50);
  227.     } while (!Common_BtnPress(BTN_QUIT));
  228.  
  229.     /*
  230.         Stop the channel, then wait for it to finish opening before we release it.
  231.     */   
  232.     if (channel)
  233.     {
  234.         result = channel->stop();
  235.         ERRCHECK(result);
  236.     }
  237.  
  238.     do
  239.     {
  240.         Common_Update();
  241.         Common_Draw("Waiting for sound to finish opening before trying to release it....", Common_BtnStr(BTN_ACTION1));
  242.         Common_Sleep(50);
  243.        
  244.         result = system->update();
  245.         ERRCHECK(result);
  246.  
  247.         result = sound->getOpenState(&openstate, 0, 0, 0);
  248.         ERRCHECK(result);
  249.     } while (openstate != FMOD_OPENSTATE_READY);
  250.  
  251.     /*
  252.         Shut down
  253.     */
  254.     result = sound->release();
  255.     ERRCHECK(result);
  256.     result = system->close();
  257.     ERRCHECK(result);
  258.     result = system->release();
  259.     ERRCHECK(result);
  260.  
  261.     Common_Close();
  262.    
  263.     return 0;
  264. }

advertising

Update the Post

Either update this post and resubmit it with changes, or make a new post.

You may also comment on this post.

update paste below
details of the post (optional)

Note: Only the paste content is required, though the following information can be useful to others.

Save name / title?

(space separated, optional)



Please note that information posted here will not expire by default. If you do not want it to expire, please set the expiry time above. If it is set to expire, web search engines will not be allowed to index it prior to it expiring. Items that are not marked to expire will be indexable by search engines. Be careful with your passwords. All illegal activities will be reported and any information will be handed over to the authorities, so be good.

comments powered by Disqus
worth-right
worth-right