linux下c++实现音乐播放软件

linux下基于SFML实现音乐播放

最近想在linux下实现一个简单的音乐播放软件,通过学习找到一个SFML库可用。基于SFML库实现了音乐的播放、暂停、停止、音量调整、音乐切换等功能

1、安装smfl

bash 复制代码
sudo apt-get install libsfml-dev

2、在cmakelists.txt中添加引用,因为只用到了音频功能,所以只依赖audio库即可

bash 复制代码
find_package(SFML 2.5 REQUIRED COMPONENTS audio)
link_directories(${SFML_LIBRARY_DIR})
target_link_libraries( music_node
  sfml-audio
)

3、完整运行代码

cpp 复制代码
#include <iostream>
#include <SFML/Audio.hpp>
#include <string>
#include <dirent.h>
#include <vector>


using namespace std;
class MusicPlayer
{
private:
    /* data */
    // 加载音乐文件
    sf::Music m_cMusic;
    vector<string> m_vMusic_files;
    int m_iCurrent_index;
    bool m_bPause_flag;
public:
    MusicPlayer(/* args */){m_iCurrent_index=0;m_bPause_flag=true;}
    ~MusicPlayer(){}
    //更换音乐文件路径,获取文件路径下所有文件
    void switch_music_folder(const std::string& path)
    {
        m_vMusic_files.clear();
        DIR *dir;
        struct dirent *ent;

        dir = opendir(path.c_str());
        if (dir != NULL) {
            while ((ent = readdir(dir)) != NULL) {
                string file_name = ent->d_name;
            
                if (file_name=="."||  file_name=="..")
                {
                    continue;
                }
                std::cout << file_name << std::endl;
                m_vMusic_files.emplace_back(path+"/"+file_name);
            }
            closedir(dir);
        } else {
            std::cerr << "无法打开目录" << path << std::endl;
        }

    }
    void play_music()
    {
        if (m_vMusic_files.empty())
            return;
        if (!m_cMusic.openFromFile(m_vMusic_files[m_iCurrent_index]))
        {
            std::cout << "无法加载音乐文件" << std::endl;
        }
        ROS_INFO("当前音乐:%s",m_vMusic_files[m_iCurrent_index].c_str());
        m_cMusic.play();
    }
    void stop_music()
    {
        m_cMusic.stop();
    }
    void pause_music()
    {
        m_bPause_flag = true;
        m_cMusic.pause();
    }
    void start_music()
    {
        if (m_cMusic.getStatus()==sf::Music::Paused)
        {
            m_cMusic.play();
        }
        else if (m_bPause_flag)
        {
            play_music();
        }
    }
    bool get_state()
    {
        return !m_bPause_flag;
    }
    void adjust_volume(int volume)
    {
        // 音量范围0-100
        if (volume<0)
            volume=0;
        if (volume>100)
            volume=100;
        ROS_INFO("当前音量:%d",volume);
        m_cMusic.setVolume(volume);
        
    }
    void switch_to_next_music(bool next=true)
    {
        if (m_bPause_flag)
        {
            m_bPause_flag = false;
        }
        if (next)
        {
            m_iCurrent_index+=1;
            if (m_iCurrent_index > m_vMusic_files.size())
            {
                // 如果最后一首,则从头开始
                m_iCurrent_index = 0;
            }
        }
        else
        {
            m_iCurrent_index-=1;
            if (m_iCurrent_index<0)
            {
                m_iCurrent_index = m_vMusic_files.size()-1;
            }
        }
        play_music();

    }
};
相关推荐
程序员南飞1 小时前
ps aux | grep smart_webrtc这条指令代表什么意思
java·linux·ubuntu·webrtc
StrokeAce2 小时前
linux桌面软件(wps)内嵌到主窗口后的关闭问题
linux·c++·qt·wps·窗口内嵌
家有狸花5 小时前
VSCODE驯服日记(三):配置C++环境
c++·ide·vscode
dengqingrui1235 小时前
【树形DP】AT_dp_p Independent Set 题解
c++·学习·算法·深度优先·图论·dp
C++忠实粉丝5 小时前
前缀和(8)_矩阵区域和
数据结构·c++·线性代数·算法·矩阵
热爱嵌入式的小许5 小时前
Linux基础项目开发1:量产工具——显示系统
linux·运维·服务器·韦东山量产工具
ZZZ_O^O6 小时前
二分查找算法——寻找旋转排序数组中的最小值&点名
数据结构·c++·学习·算法·二叉树
小飞猪Jay8 小时前
C++面试速通宝典——13
jvm·c++·面试
rjszcb9 小时前
一文说完c++全部基础知识,IO流(二)
c++
韩楚风9 小时前
【linux 多进程并发】linux进程状态与生命周期各阶段转换,进程状态查看分析,助力高性能优化
linux·服务器·性能优化·架构·gnu