请帮我使用c++语言设计一个简单的命令行音乐播放器,支持播放、暂停、停止、上一曲、下一曲、调整音量等功能。使用第三方库(如 SFML或 SDL) 来实现音频播放。
时间: 2024-01-27 19:06:22 浏览: 117
基于c++ qt实现的一个简单音乐播放器源码, 支持添加音乐目录, 调整音量
好的,以下是一个基于 SDL 库的简单命令行音乐播放器的示例代码。该程序支持播放、暂停、停止、上一曲、下一曲、调整音量等功能。
```c++
#include <iostream>
#include <string>
#include <SDL.h>
using namespace std;
// 常量定义
const int VOLUME_INCREMENT = 10; // 调整音量时的增量
const string MUSIC_DIRECTORY = "./music/"; // 音乐文件所在的目录
const string MUSIC_LIST[] = { "music1.mp3", "music2.mp3", "music3.mp3" }; // 音乐文件列表
// 全局变量定义
int currentMusicIndex = 0; // 当前播放的音乐文件在列表中的索引
int volume = 100; // 当前音量
// 函数声明
void playMusic(SDL_AudioDeviceID audioDevice, const string& musicFilename);
void pauseMusic(SDL_AudioDeviceID audioDevice);
void stopMusic(SDL_AudioDeviceID audioDevice);
void adjustVolume(SDL_AudioDeviceID audioDevice, int volumeIncrement);
void playNextMusic(SDL_AudioDeviceID audioDevice);
void playPreviousMusic(SDL_AudioDeviceID audioDevice);
int main(int argc, char* argv[]) {
// 初始化 SDL
if (SDL_Init(SDL_INIT_AUDIO) != 0) {
cerr << "Failed to initialize SDL: " << SDL_GetError() << endl;
return 1;
}
// 打开音频设备
SDL_AudioSpec desiredSpec, obtainedSpec;
SDL_zero(desiredSpec);
desiredSpec.freq = 44100;
desiredSpec.format = AUDIO_S16SYS;
desiredSpec.channels = 2;
desiredSpec.samples = 2048;
desiredSpec.callback = NULL;
SDL_AudioDeviceID audioDevice = SDL_OpenAudioDevice(NULL, 0, &desiredSpec, &obtainedSpec, 0);
if (audioDevice == 0) {
cerr << "Failed to open audio device: " << SDL_GetError() << endl;
SDL_Quit();
return 1;
}
// 播放第一首音乐
playMusic(audioDevice, MUSIC_DIRECTORY + MUSIC_LIST[currentMusicIndex]);
// 处理命令行输入
string command;
while (true) {
cout << "Enter a command (play/pause/stop/volumeup/volumedown/next/previous): ";
cin >> command;
if (command == "play") {
playMusic(audioDevice, MUSIC_DIRECTORY + MUSIC_LIST[currentMusicIndex]);
} else if (command == "pause") {
pauseMusic(audioDevice);
} else if (command == "stop") {
stopMusic(audioDevice);
} else if (command == "volumeup") {
adjustVolume(audioDevice, VOLUME_INCREMENT);
} else if (command == "volumedown") {
adjustVolume(audioDevice, -VOLUME_INCREMENT);
} else if (command == "next") {
playNextMusic(audioDevice);
} else if (command == "previous") {
playPreviousMusic(audioDevice);
} else {
cout << "Invalid command. Try again." << endl;
}
}
// 关闭音频设备和 SDL
SDL_CloseAudioDevice(audioDevice);
SDL_Quit();
return 0;
}
// 播放音乐
void playMusic(SDL_AudioDeviceID audioDevice, const string& musicFilename) {
SDL_ClearQueuedAudio(audioDevice);
SDL_AudioSpec wavSpec;
Uint32 wavLength;
Uint8* wavBuffer;
if (SDL_LoadWAV(musicFilename.c_str(), &wavSpec, &wavBuffer, &wavLength) == NULL) {
cerr << "Failed to load music file: " << SDL_GetError() << endl;
return;
}
SDL_QueueAudio(audioDevice, wavBuffer, wavLength);
SDL_FreeWAV(wavBuffer);
SDL_PauseAudioDevice(audioDevice, 0);
cout << "Playing " << musicFilename << endl;
}
// 暂停音乐
void pauseMusic(SDL_AudioDeviceID audioDevice) {
SDL_PauseAudioDevice(audioDevice, 1);
cout << "Music paused" << endl;
}
// 停止音乐
void stopMusic(SDL_AudioDeviceID audioDevice) {
SDL_ClearQueuedAudio(audioDevice);
SDL_PauseAudioDevice(audioDevice, 1);
cout << "Music stopped" << endl;
}
// 调整音量
void adjustVolume(SDL_AudioDeviceID audioDevice, int volumeIncrement) {
volume += volumeIncrement;
if (volume > 128) {
volume = 128;
} else if (volume < 0) {
volume = 0;
}
SDL_Volume(audioDevice, volume / 128.0f);
cout << "Volume adjusted to " << volume << endl;
}
// 播放下一首音乐
void playNextMusic(SDL_AudioDeviceID audioDevice) {
currentMusicIndex = (currentMusicIndex + 1) % (sizeof(MUSIC_LIST) / sizeof(MUSIC_LIST[0]));
stopMusic(audioDevice);
playMusic(audioDevice, MUSIC_DIRECTORY + MUSIC_LIST[currentMusicIndex]);
}
// 播放上一首音乐
void playPreviousMusic(SDL_AudioDeviceID audioDevice) {
currentMusicIndex--;
if (currentMusicIndex < 0) {
currentMusicIndex = sizeof(MUSIC_LIST) / sizeof(MUSIC_LIST[0]) - 1;
}
stopMusic(audioDevice);
playMusic(audioDevice, MUSIC_DIRECTORY + MUSIC_LIST[currentMusicIndex]);
}
```
注意:该示例程序需要在包含 SDL 库的环境中编译运行。具体的编译方法和配置方式可以参考 SDL 官方文档。
阅读全文