We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 4754 - Android : Mix_LoadMUS_RW && Mix_LoadMUS always return null and fail to load music.
Summary: Android : Mix_LoadMUS_RW && Mix_LoadMUS always return null and fail to load m...
Status: NEW
Alias: None
Product: SDL_mixer
Classification: Unclassified
Component: misc (show other bugs)
Version: 2.0.4
Hardware: x86_64 Android (All)
: P2 normal
Assignee: Ryan C. Gordon
QA Contact: Sam Lantinga
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2019-08-06 05:46 UTC by Kostyarezkiy
Modified: 2020-10-07 07:12 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Kostyarezkiy 2019-08-06 05:46:52 UTC
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;
}
Comment 1 Sylvain 2019-08-22 06:59:11 UTC
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 ...
Comment 2 Sylvain 2020-10-07 07:12:37 UTC
Do you have more information ?