- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
好了,问题来了。我开始学习 SDL,并想为我为学校项目制作的这个“游戏”添加一些音频。音频无法从我的程序中播放,但我阅读和下载的教程代码中包含音频并可以播放。我真的很困惑为什么我的音频无法播放,但我知道这是正确的。
非常感谢任何帮助,这是代码
#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;
}
最佳答案
如果知道输出内容会很棒。如果它说“音乐错误”那么你也应该打印 Mix_GetError()
printf("Music Error %s",Mix_GetError());
一般来说,无法播放 MP3 是因为缺少在不支持 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...
}
关于c++ - SDL_Mixer 音频不播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37057745/
我正在浏览 this tutorial使用 SDL_mixer 在音频上一切正常。 在那之后,我决定通过探索进行实验、玩耍和学习,所以我制作了一个非常相似的程序,有两个声道,让我播放一些视频游戏音乐,
我正在尝试获取一些开源C ++代码以在xcode中进行编译。它使用SDL库。我为OS X安装了downloaded SDL 2.0并安装了它,并且一切正常-但下载的文件似乎并未包含SDL_mixer.
基本信息: 编程语言 - C++ 平台 - Windows 音频格式 - wav 和 mid 我最近完成了一款游戏,并且正在四处寻找将其上传到文件托管站点的最佳方式。我最终决定使用 7zip 的自解压
如果某个点在范围内,我的程序需要停止播放声音,因为我有一个带有函数 run() 的类,它在单独的线程上运行并使用 SDL.hgetPos())) { print
好了,问题来了。我开始学习 SDL,并想为我为学校项目制作的这个“游戏”添加一些音频。音频无法从我的程序中播放,但我阅读和下载的教程代码中包含音频并可以播放。我真的很困惑为什么我的音频无法播放,但我知
我阅读并尝试了该主题的所有其他帖子,但没有任何帮助。当我尝试使用 Mix_PlayChannel() 播放音乐时,我没有收到错误消息,也没有听到任何声音!我现在尝试了几个小时,但没有任何帮助。程序愉快
我想知道 SDL_Mixer 库中是否有跳转到 wav 文件中特定位置的功能。我发现有一个名为 Mix_SetMusicPosition 的函数,但它不适用于 .wav 文件,也不允许您选择 chan
我遇到了一个奇怪的问题。我想在我的游戏中加入声音。这是一个有两个阶段的游戏。角色扮演游戏之一和战斗之一。在角色扮演游戏中,您可以在踩到怪物时发动战斗。在我的代码中,我使用了 3 次 SDL_mixer
嗨,我正在尝试在这里触发多个音乐播放,这里有一些代码。 Mix_Music *mix_list[MUSIC_COUNT] ; //init music with SDL int
我正在尝试获取一些开源 C++ 代码以在 xcode 中进行编译。它使用 SDL 库。我downloaded适用于 OS X 的 SDL 2.0 并安装了它,一切正常 - 但下载的文件似乎不包含文件
我有一个程序可以使用 SDL_mixer 库 ( http://www.libsdl.org/projects/SDL_mixer/ ) 播放一些声音。当我只使用 Mix_OpenAudio 打开与默
我有 4 个声音。我需要播放声音1,当它结束时,自动播放声音2;当声音 2 结束时,自动播放声音 3。声音 3 结束时,播放声音 4....我正在使用 SDL Mixer 2.0,没有 SDL Sou
我在使用 SDL_Mixer 时遇到了问题(我缺乏经验)。 block 和音乐播放得很好(使用 Mix_PlayChannel 和 Mix_PlayMusic),同时播放两个不同的 block 不是问
我正在使用 SDL 和 SDL_mixer 库,在编译时遇到以下错误: .... game.cpp:(.text+0x88f): undefined reference to `Mix_OpenAud
我想写一个简单的控制台音乐播放器,它支持 .mp3 和 .wav 音频格式。使用 SDL_mixer 播放 .wav 文件会降低声音质量,但使用 mp3 文件则不会。当 .wav 文件开始播放时,会发
我使用 SDL2_Mixer 在 Linux Ubuntu 16.04 上播放声音。 这是我的类(class)代码。 bool SoundPlayer::init() { if (SDL_In
我正在使用 SDL_Mixer 在我的 C++ 游戏中实现声音,但在 the manual 中找不到函数那会给我当前正在播放的 Mix_Music。我想我需要它来了解我是否可以释放 Mix_Music
目前SDL_Mixer有两种声音资源:chunk和music。 除了 API 和支持的格式限制外,是否有任何理由不将音乐作为 SDL_Chunk 和 channel 加载和播放? (内存、速度等) 最
我正在制作一个简单的游戏,当玩家接近正在玩的关卡结束时,它的音频速度应该会增加。所以现在我想知道是否有办法使用 SDL_Mixer 来做到这一点。如果 SDL_Mixer 不是要走的路,请告诉我如何在
我有一个使用 GLFW 进行窗口创建和输入的游戏。是否可以在没有 SDL 的情况下将 SDL_mixer 用于音频? 最佳答案 不,你不能。 因为它依赖于 SDL header 。在 SDL_mixe
我是一名优秀的程序员,十分优秀!