| Summary: | Android : Mix_LoadMUS_RW && Mix_LoadMUS always return null and fail to load music. | ||
|---|---|---|---|
| Product: | SDL_mixer | Reporter: | Kostyarezkiy <Roung1990> |
| Component: | misc | Assignee: | Ryan C. Gordon <icculus> |
| Status: | NEW --- | QA Contact: | Sam Lantinga <slouken> |
| Severity: | normal | ||
| Priority: | P2 | CC: | sylvain.becker |
| Version: | 2.0.4 | ||
| Hardware: | x86_64 | ||
| OS: | Android (All) | ||
I'd say add printf in SDL_mixer/mixer.c, function Mix_LoadMusic_RW() so that you find more precisely why it returns null. (Maybe there are issue with Android File functions. in bug #4297, the is a patch that changes all the file functions to use the standard ones from the NDK. you can try it, though I am not sure it will helps ... Do you have more information ? |
Ia m sorry in advance as it's somewhat difficult for me to describe the bug that I am seeing because I have the same code working on multiple platforms. I can provide a github link where code can be tested . I will do my best to be clear and provide code examples where the Music portion for android will fail. Ok I have noticed some weird behaviour which only leads me to believe that Music connected functions don't function properly in Android. I cannot quite put my finger on it because , I noticed that as a solution I can play a wav as a sound effect , but as I try to use the music_mixer, the function returns null. I will provide some code examples to prove a point . I have a few projects that work fine on multiple platforms but once in Android I have to convert Mix_Music to a Mix_Chunk. In another code base , I noticed that Mix_LoadMUS_RW always returns null. /* Code Sample 1------------------------------*/ Mix_Music *music; SDL_RWops *musicRw; char *musicBuffer; sound_create_primary_buffer(sgpMusicTrack); if (!success) { sgpMusicTrack = NULL; } else { int bytestoread = SFileGetFileSize(sgpMusicTrack, 0); musicBuffer = (char *)MusicAllocPtr(bytestoread); SFileReadFile(sgpMusicTrack, musicBuffer, bytestoread, NULL, 0); musicRw = SDL_RWFromConstMem(musicBuffer, bytestoread); if (musicRw == NULL) { SDL_Log(SDL_GetError()); } music = Mix_LoadMUS_RW(musicRw, 1); // This function always returns null despite musicRw always having a pointer Mix_VolumeMusic(MIX_MAX_VOLUME - MIX_MAX_VOLUME * sglMusicVolume / VOLUME_MIN); Mix_PlayMusic(music, -1);// this will never play because there is no music loaded in the mixer. sgnMusicTrack = nTrack; /*CODE SAMPLE2----------------------------------------------------------------*/ #include <SDL.h> #include <SDL_image.h> #include <SDL_mixer.h> #include <stdio.h> #include <android/log.h> #include <iostream> #include <fstream> Mix_Music *gMusic = NULL; Mix_Chunk *gHigh = NULL; int main(int /*argc*/, char* /*argv*/[]) { bool success = true; SDL_Window *window; // Declare a pointer SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO); // Initialize SDL2 // Create an application window with the following settings: window = SDL_CreateWindow( "An SDL2 window", // window title SDL_WINDOWPOS_UNDEFINED, // initial x position SDL_WINDOWPOS_UNDEFINED, // initial y position 640, // width, in pixels 480, // height, in pixels SDL_WINDOW_OPENGL // flags - see below ); // Check that the window was successfully created if (window == NULL) { // In the case that the window could not be made... printf("Could not create window: %s\n", SDL_GetError()); return 1; } // The window is open: could enter program loop here (see SDL_PollEvent()) // Setup renderer SDL_Renderer *renderer = NULL; renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); // Set render color to red ( background will be rendered in this color ) SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255); // Clear winow SDL_RenderClear(renderer); if( Mix_OpenAudio( 44100, MIX_DEFAULT_FORMAT, 2, 2048 ) < 0 ) { __android_log_print(ANDROID_LOG_VERBOSE, "DEBUGSOUND", Mix_GetError(), 1); success = false; } //THIS FAILS gMusic = Mix_LoadMUS("/sdcard/music/test.wav");///// THIS FAILS if (gMusic == NULL) { //char * error [100] = {0}; //sprintf(error,Mix_GetError()) printf("Failed to load beat music! SDL_mixer Error: %s\n", Mix_GetError()); __android_log_print(ANDROID_LOG_VERBOSE, "DEBUGSOUND", Mix_GetError(), 1); success = false; } //THIS Succeeds! gHigh = Mix_LoadWAV( "/sdcard/music/test.wav" ); if( gHigh == NULL ) { printf( "Failed to load high sound effect! SDL_mixer Error: %s\n", Mix_GetError() ); __android_log_print(ANDROID_LOG_VERBOSE, "DEBUGSOUND", Mix_GetError(), 1); success = false; } // bouyatest // Creat a rect at pos ( 50, 50 ) that's 50 pixels wide and 50 pixels high. SDL_Rect r; r.x = 50; r.y = 50; r.w = 500; r.h = 500; // Set render color to blue ( rect will be rendered in this color ) SDL_SetRenderDrawColor( renderer, 0, 0, 255, 255 ); // Render image SDL_Surface *loadedImage = IMG_Load("res/hello.png"); SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, loadedImage); SDL_FreeSurface(loadedImage); SDL_RenderCopy(renderer, texture, NULL, &r); // Render the rect to the screen SDL_RenderPresent(renderer); bool quit = false; //Event handler SDL_Event e; //While application is running while( !quit ) { //Handle events on queue while( SDL_PollEvent( &e ) != 0 ) { //User requests quit if( e.type == SDL_QUIT ) { quit = true; } //Handle key press else if( e.type == SDL_MOUSEBUTTONDOWN ) { if( Mix_PlayingMusic() == 0 ) { //Play the music Mix_PlayChannel( -1, gHigh, 0 ); } } } } SDL_Delay(8000); // Pause execution for 3000 milliseconds, for example // Close and destroy the window SDL_DestroyWindow(window); // Clean up SDL_Quit(); return 0; }