2016-05-05 14 views
1

問題があります。私はSDLを学び始めています。私は学校プロジェクトのために作っているこの「ゲーム」にいくつかの音声を加えたいと思っています。オーディオは私のプログラムでは再生されませんが、私が読んでダウンロードしたチュートリアルのコードにはオーディオが入っていて、再生しています。なぜ私のオーディオが再生されないのか、私は本当に混乱していますが、私が知っていることは間違っていません。SDL_Mixerオーディオが再生されない

  1. mp3ミキサーはそれに期待するだけのように、コード
  2. mp3は44100 Hzで果たしていると同じフォルダ内にあります。
  3. すべてのヘルプは大歓迎です

は、ここで出力が言うことを知っているのは素晴らしいだろうコード

#include <iostream> 
#include <fstream> 
#include <stdlib.h> 
#include "SDL/SDL.h" 
#include "SDL2/SDL.h" 
#include <SDL2/SDL_mixer.h> 


//The attributes of the screen 
const int SCREEN_WIDTH = 640; 
const int SCREEN_HEIGHT = 640; 
const int SCREEN_BPP = 32; 

//The surfaces that will be used 
Mix_Music *music = NULL; 
SDL_Surface *wall = NULL; 
SDL_Surface *path = NULL; 
SDL_Surface *breadcrumb = NULL; 
SDL_Surface *deadend = NULL; 
SDL_Surface *screen = NULL; 
SDL_Event event; 

using namespace std; 
//////////////////////////////////////////////////////////// 
SDL_Surface *load_image(string filename) 
{ 
    SDL_Surface* loadedImage = NULL;  //Temporary storage for the image that's loaded 
    SDL_Surface* optimizedImage = NULL; //The optimized image that will be used 
    loadedImage = SDL_LoadBMP(filename.c_str()); //Load the image 
    if(loadedImage != NULL) //If nothing went wrong in loading the image 
    { 
     optimizedImage = SDL_DisplayFormat(loadedImage);  //Create an optimized image 
     SDL_FreeSurface(loadedImage);  //Free the old image 
    } 
    return optimizedImage; //Return the optimized image 
} 
//////////////////////////////////////////////////////////// 
void apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination) 
{ 
    SDL_Rect offset; //Make a temporary rectangle to hold the offsets 
    offset.x = x; //Give the offsets to the rectangle 
    offset.y = y; 
    SDL_BlitSurface(source, NULL, destination, &offset); //Blit th 
} 
using namespace std; 

char MAZE[22][22]; 
//////////////////////////////////////////////////////////////////////// 
void Initialize() 
{ 
    int X,Y; 
    for (X=0;X<22;X++) 
     for (Y=0;Y<22;Y++) 
      MAZE[X][Y]='0'; 
    ifstream fin ("maze.txt"); 
    if (fin.is_open()) 
    { 
     for (X=1;X<21;X++) 
      for (Y=1;Y<21;Y++) 
       fin>>MAZE[X][Y]; 
     fin.close(); 
    } 
    else cout << "Unable to open maze.txt\n"; 
    return; 
} 
//////////////////////////////////////////////////////////////////////// 
void PrintMaze() 
{ 
    int gx,gy; 
    system("clear"); 
    for (int Y=1;Y<21;Y++) 
    { 
     for (int X=1;X<21;X++) 
     { 
      gx=32*(X-1); 
      gy=32*(Y-1); 
      cout<<MAZE[X][Y]; 
      switch (MAZE[X][Y]) 
      { 
       case '.': 
        apply_surface(gx, gy, wall, screen); 
       break; 
       case 'X': 
        apply_surface(gx, gy, path, screen); 
       break; 
       case '*': 
        apply_surface(gx, gy, breadcrumb, screen); 
       break; 
      } 
     }cout<<"\n"; 

    } 
    if(SDL_Flip(screen) == -1) return; 
    cout<<flush; 
    system("sleep .2"); 
    return; 
} 
//////////////////////////////////////////////////////////////////////// 
int Solve(int X, int Y) 
{ 
    if ((X==20)&&(Y==20)) 
    { 
     cout<<"("<<X<<","<<Y<<")"; 
     return 1; 
    } 
    if (MAZE[X][Y]!='X') return 0; 
    else 
    { 
     PrintMaze(); 
     MAZE[X][Y]='*'; 
     for (int A=(X+1);A>=(X-1);A--) 
      for (int B=(Y+1);B>=(Y-1);B--) 
       if (Solve(A,B)==1) 
       { 
        cout<<"("<<X<<","<<Y<<")"; 
        return 1; 
       } 
    } 
    MAZE[X][Y]='.'; 
    return 0; 
} 
//////////////////////////////////////////////////////////////////////// 
int main() 
{ 
    if(SDL_Init(SDL_INIT_EVERYTHING) == -1) //Initialize all SDL subsystems 
     return 1; 
    screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE); //Set up the screen 
    if(screen == NULL) //If there was an error in setting up the screen 
     return 1; 
    //Initialize SDL_mixer 
     if(Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048) < 0) 
     { 
        printf("SDL_mixer could not initialize! SDL_mixer Error: %s\n", Mix_GetError()); 
     } 
    SDL_WM_SetCaption("Dank Solver", NULL); //Set the window caption 
    wall = load_image("wall.bmp"); //Load the images 
    path = load_image("path.bmp"); 
    breadcrumb = load_image("breadcrumb.bmp"); 
    deadend = load_image("deadend.bmp"); 
    music = Mix_LoadMUS("mlg.mp3"); 
     if(music == NULL) 
    { 
     cout<<"Music Error"; 
     return 0; 
    } 

    Initialize(); 
    Mix_PlayMusic (music, -1); 
    PrintMaze(); 
    if (Solve(1,1)==0) cout<<"No Solution\n"; 
    else cout<<"Solution Found\n"; 
    SDL_FreeSurface(wall); 
    SDL_FreeSurface(path); 
    SDL_FreeSurface(breadcrumb); 
    SDL_FreeSurface(deadend); 
    Mix_FreeMusic (music); 
    Mix_CloseAudio(); 
    SDL_Quit();   //Quit SDL 
    return 0; 
} 
+0

wavファイルで試しましたか? – GRiMe2D

+0

はい、それも無駄に働いた。 –

+0

私はMP3再生も無音になっています。 WAV、MOD、IT、S3Mの音楽作品は、OGGやFLACをMP3に向けて試してみませんでした。エラーコードなし - SDL_mixerに関する限り、音楽が再生されています(音楽タイプMUS_MP3、音量128、一時停止またはフェーディングなし)。また、Windows 7(SP1、64ビット)では失敗しますが、Linuxでは使用できません。音楽タイプはMUS_MP3ではなくMUS_MP3_MADとして報告されます。私はそれがWindowsで使用されている特定のデコーダの問題かもしれないと思っています - 私はsmpeg2.dllを信じていますが、まだ調査中です。しかし、おそらくOGGに変換するのが簡単な回避策です。 – Steve314

答えて

0

です。それは「音楽エラー」と言う場合 、あなたはまた、

printf("Music Error %s",Mix_GetError()); 

は、一般的にMP3を再生するために失敗し、話す()Mix_GetErrorを印刷する必要がありますがmp3のサポートなしで構築された行方不明のコーデック/ミキサーの結果です。 ファイルの再生中にエラーがないか確認することもできます。 文書からコピーして貼り付け:

if(Mix_PlayMusic(music, -1)==-1) { 
    printf("Mix_PlayMusic: %s\n", Mix_GetError()); 
    // well, there's no music, but most games don't break without music... 
} 
関連する問題