Advertisement
Guest User

Native Android music

a guest
May 30th, 2012
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.66 KB | None | 0 0
  1. int AudioManager::load_music(const char* source_file)
  2. {
  3.   // Attempt to open the music file
  4.   music_file = SDL_RWFromFile(source_file, "rb"); // read binary
  5.   ASSERT(music_file, source_file); // print the name of the file being opened
  6.  
  7.   #ifdef __ANDROID__
  8.     // initialise RWops structure from file in SD card
  9.     FILE* sdcard = fopen("/sdcard/data/music.ogg", "wb");
  10.     ASSERT(sdcard, "Opening file to export music from APK to filesystem");
  11.     SDL_RWops* sdcard_rw = SDL_RWFromFP(sdcard, SDL_TRUE); // autoclose
  12.     ASSERT(sdcard_rw, "Creating SDL_RWops structure from file pointer");
  13.  
  14.     // externalise music data from APK assets folder to the SD card
  15.     char buffer[io::MAX_BLOCKS];
  16.     int read_amount = SDL_RWread(music_file, buffer, 1, io::MAX_BLOCKS);
  17.     while(read_amount > 0)
  18.     {
  19.       SDL_RWwrite(sdcard_rw, buffer, 1, read_amount);
  20.       SDL_RWseek(music_file, SEEK_CUR, read_amount*io::BLOCK_SIZE);
  21.       read_amount = SDL_RWread(music_file, buffer, 1, io::MAX_BLOCKS);
  22.     }
  23.     SDL_RWclose(music_file);
  24.     SDL_RWclose(sdcard_rw);
  25.  
  26.     // load the music from the filesystem, not the archive
  27.     sdcard = fopen("/sdcard/data/music.ogg", "rb");
  28.     ASSERT(sdcard, "Opening file to import music from filesystem");
  29.     music_file = SDL_RWFromFP(sdcard, SDL_TRUE); // autoclose
  30.     ASSERT(music_file, "Creating SDL_RWops structure from file pointer");
  31.  
  32.   #endif //ifdef __ANDROID__
  33.  
  34.   // Attempt to read the file contents as music
  35.   ASSERT_MIX(music = Mix_LoadMUS_RW(music_file),
  36.               "Extracting music from SDL_RWops structure");
  37.  
  38.   /// NB - file is NOT closed as music data must be streamed
  39.  
  40.  
  41.   // Success !
  42.   return EXIT_SUCCESS;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement