简体   繁体   English

Mix_PlayMusic() 不播放音乐

[英]Mix_PlayMusic() not playing music

I have this program which used to load mp3 file from cmd argument but when i use the Mix_PlayMusic the audio isn't playing:我有这个程序用于从 cmd 参数加载 mp3 文件但是当我使用 Mix_PlayMusic 时音频没有播放:

#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <stdbool.h>

#include <SDL2/SDL.h>
#include <SDL2/SDL_mixer.h>

Mix_Music *mp3 = NULL;

bool loadmedia(char *filepath);
void play();

int main(int argc, char *argv[]){

    bool success;   

   if (SDL_Init(SDL_INIT_AUDIO) < 0)
    {
        printf("Couldn't initialize SDL: %s\n", SDL_GetError());
        success = false;
    }


    if(Mix_OpenAudio(44100,MIX_DEFAULT_FORMAT,2,2048)<0){
        printf("SDL_mixer could not be initialized %s\n",Mix_GetError());
        success = false;
    }

    char *file = argv[1];

    bool status= loadmedia(file);

   
}


bool loadmedia(char *file_path){
    bool success = true;

    printf("%s is path\n",file_path);

    mp3 = Mix_LoadMUS(file_path);

    if(mp3==NULL){
        printf("media load failed\n");
        success = false;
    }
    else{
                    printf("media loaded succefully\n");
                    Mix_PlayMusic(mp3,-1);

                    play();
        
    }

    return success;

}

void play(){

    Mix_FreeMusic(mp3);
}

i use sdl2 and i used both wav and mp3 files but nothing is working.我使用 sdl2,同时使用 wav 和 mp3 文件,但没有任何效果。

so the problem was SDL_Delay() wasn't added to program which the file has not time to be played adding SDL_Delay() fixed the problem.所以问题是SDL_Delay()没有添加到文件没有时间播放的程序中,添加SDL_Delay()解决了这个问题。

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

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