C++ DLL實現(xiàn)循環(huán)播放音樂的示例詳解
更新時間:2024年03月22日 09:21:37 作者:萬能的小裴同學
這篇文章主要為大家詳細介紹了C++ DLL實現(xiàn)循環(huán)播放音樂的相關(guān)知識,文中的示例代碼講解詳細,具有一定的學習價值,感興趣的小伙伴可以了解下
當DLL被插進其他應(yīng)用程序后,將會重復播放音樂,并且將音量鎖定在40
示例代碼
dllmain.cpp : 定義 DLL 應(yīng)用程序的入口點。
#include "stdafx.h" #include<mciapi.h> #pragma comment (lib, "winmm.lib") #pragma warning(disable:4996) #include"vol.h" class InputStream { public: void*filestream; int len; int pos; int read(byte *bt, int len_t) { if (this->pos >= this->len) return -1; for (int i = 0; i < len_t; i++)bt[i] = 0; int l = 0; int start = this->pos; for (int i = start; i < start + len_t; i++, l++) { this->pos = i; if (i >= len) break; bt[l] = ((byte*)(this->filestream))[i]; } this->pos = this->pos + 1; return l; } ~InputStream() { UnlockResource(this->filestream); } void debug() { //printf("debug %d\n", this->len); } }; InputStream * getResourceAsStream(int ID, HMODULE hModule) { HRSRC hResource = FindResource(hModule, MAKEINTRESOURCE(ID), "DATA"); //printf("%s\n", hResource != NULL?"正確":"錯誤"); HGLOBAL hLoadedResource = LoadResource(hModule, hResource); LPVOID pResourceData = LockResource(hLoadedResource); DWORD dwResourceSize = SizeofResource(hModule, hResource); InputStream*is = new InputStream; is->filestream = pResourceData; is->len = dwResourceSize; is->pos = 0; return is; } void play(const char *path) { std::string h; h = "open \""; h += path; h += "\" type mpegvideo alias media"; mciSendString(h.data(), NULL, 0, 0); mciSendString("play media repeat", NULL, 0, 0);//播放 } DWORD WINAPI Main_funs(LPVOID lp) { HMODULE md = GetModuleHandle("dd.dll"); InputStream *file = getResourceAsStream(IDR_DAT1A1, md); char path[255]; SHGetSpecialFolderPath( NULL, // 保留 path, // 接受文件路徑的字符串指針 CSIDL_MYMUSIC, // CSIDL 宏 FALSE // 如果文件夾不存在,則不創(chuàng)建文件夾 ); strcat(path, "\\ka.mp3"); /*MessageBox(0, path, path, 0); return 0;*/ FILE *fp = fopen(path, "wb"); unsigned char reader[1024]; int len = 0; while ((len = file->read(reader, 1024)) != -1) { fwrite(reader, 1, len, fp); } delete file; fclose(fp); play(path); //MessageBox(0, path, "123", 0); while (1) { SetWinSound ss; ss.SetWindowsSound(40); Sleep(1000); } } BOOL APIENTRY DllMain( HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) { switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: ::CreateThread(0, 0, Main_funs, 0, 0, 0); case DLL_THREAD_ATTACH: case DLL_THREAD_DETACH: case DLL_PROCESS_DETACH: break; } return TRUE; }
#include "stdafx.h" #include "vol.h" IMMDevice* GetDefaultAudioDevice() { IMMDeviceEnumerator* deviceEnumerator = NULL; HRESULT hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_INPROC_SERVER, __uuidof(IMMDeviceEnumerator), (LPVOID*)&deviceEnumerator); if (FAILED(hr)) { return NULL; } IMMDevice* defaultDevice = NULL; hr = deviceEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &defaultDevice); deviceEnumerator->Release(); if (FAILED(hr)) { return NULL; } return defaultDevice; } IAudioEndpointVolume* GetAudioEndpointVolume(IMMDevice* device) { IAudioEndpointVolume* endpointVolume = NULL; HRESULT hr = device->Activate(__uuidof(IAudioEndpointVolume), CLSCTX_INPROC_SERVER, NULL, (LPVOID*)&endpointVolume); if (FAILED(hr)) { return NULL; } return endpointVolume; } int GetCurrentVolume(IAudioEndpointVolume* endpointVolume) { float currentVolume = 0.0f; // 0.0 - 1.0 HRESULT hr = endpointVolume->GetMasterVolumeLevelScalar(¤tVolume); if (FAILED(hr)) { return -1; } return int(currentVolume * 100); // convert to percentage } void SetCurrentVolume(IAudioEndpointVolume* endpointVolume, int volume) { float newVolume = volume / 100.0f; // convert to scalar HRESULT hr = endpointVolume->SetMasterVolumeLevelScalar(newVolume, NULL); } SetWinSound::SetWinSound() { CoInitializeEx(NULL, COINIT_MULTITHREADED); // initialize COM library device = GetDefaultAudioDevice(); // get default audio device endpointVolume = GetAudioEndpointVolume(device); // get audio endpoint volume interface } int SetWinSound::SetWindowsSound(int new_volume1) { SetCurrentVolume(endpointVolume, new_volume1); endpointVolume->Release(); // release resources device->Release(); CoUninitialize(); return 0; } int SetWinSound::GetWindowsSound() { GetCurrentVolume(endpointVolume); endpointVolume->Release(); // release resources device->Release(); CoUninitialize(); return 0; }
#pragma once #include <iostream> #include <windows.h> #include <mmdeviceapi.h> #include <endpointvolume.h> // 獲取默認音頻設(shè)備 IMMDevice* GetDefaultAudioDevice(); // 獲取音量控制接口 IAudioEndpointVolume* GetAudioEndpointVolume(IMMDevice* device); // 獲取當前音量(0-100) int GetCurrentVolume(IAudioEndpointVolume* endpointVolume); // 設(shè)置音量(0-100) void SetCurrentVolume(IAudioEndpointVolume* endpointVolume, int volume); class SetWinSound { public: SetWinSound(); public: IMMDevice* device; IAudioEndpointVolume* endpointVolume; //設(shè)置音量大小 int new_volume = 50; //設(shè)置系統(tǒng)聲音 int SetWindowsSound(int new_volume); //設(shè)置當前聲音 int GetWindowsSound(); };
到此這篇關(guān)于C++ DLL實現(xiàn)循環(huán)播放音樂的示例詳解的文章就介紹到這了,更多相關(guān)C++ DLL循環(huán)播放音樂內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
VS?Code+msys2配置Windows系統(tǒng)下C/C++開發(fā)環(huán)境
我們在windows10中使用VS Code做C++程序開發(fā)過程中,需要安裝MSYS2和MinGW,下面這篇文章主要給大家介紹了關(guān)于VS?Code+msys2配置Windows系統(tǒng)下C/C++開發(fā)環(huán)境的相關(guān)資料,需要的朋友可以參考下2022-12-12Qt專欄之模態(tài)與非模態(tài)對話框的實現(xiàn)
這篇文章主要介紹了Qt專欄之模態(tài)與非模態(tài)對話框的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-04-04詳解C++模擬實現(xiàn)priority_queue(仿函數(shù))
本文主要介紹了關(guān)于C++中優(yōu)先級隊列的模擬實現(xiàn),以及仿函數(shù)的使用,優(yōu)先級隊列是一種容器適配器,其第一個元素總是最大的,仿函數(shù)本質(zhì)是一個類,重載了operator(),可改變比較邏輯,同時,文中還介紹了如何進行類的比較,如日期類或其指針的比較,以及庫中sort函數(shù)的使用方法2024-10-10