Warning: file_get_contents(https://whois.pconline.com.cn/jsLabel.jsp?ip=127.0.0.1) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 503 Service Temporarily Unavailable in D:\wwwroot\huidong\wwwroot\function.inc.php on line 884
使用winapi播放mp3、wav等音乐 - huidong

huidong

首页 | 会员登录 | 关于争取 2022 寒假做出汇东网 Ver3.0.0 !
搜索文章


https://codeabc.cn/yangw/a/play-music


引用如上链接的代码:

// 编译该范例前,请把 music.mp3 放在项目文件夹中
// 发布时,请把 music.mp3 和编译的 exe 放在一起
// 编译环境:VC6~VC2017 + EasyX_20190314(beta)
//
#include <graphics.h>
#include <conio.h>
// 引用 Windows Multimedia API
#pragma comment(lib,"Winmm.lib")

void main()
{
    initgraph(640, 480);

    // 打开音乐
    mciSendString(_T("open music.mp3 alias mymusic"), NULL, 0, NULL);

    outtextxy(0, 0, _T("按任意键开始播放"));
    getch();

    // 播放音乐
    mciSendString(_T("play mymusic"), NULL, 0, NULL);

    outtextxy(0, 0, _T("按任意键停止播放"));
    getch();

    // 停止播放并关闭音乐
    mciSendString(_T("stop mymusic"), NULL, 0, NULL);
    mciSendString(_T("close mymusic"), NULL, 0, NULL);

    outtextxy(0, 0, _T("按任意键退出程序"));
    getch();
    closegraph();
}


恶作剧

在指定时间播放音乐(放在学校电脑里)


release.zip


WinMain版

WinMain_Ver.zip


音乐文件:

现在是北京时间17:40.zip


源码:

#include <easyx.h>
#include <stdio.h>
#include <conio.h>
#include <time.h>
//要添加这两个头文件,缺一不可
#include <windows.h>
#include <winuser.h>

// 引用 Windows Multimedia API
#pragma comment(lib,"Winmm.lib")

void HKRunator()
{
    //获取程序路径

    char programName[100] = { 0 };
    GetModuleFileName(NULL, programName, 100); //调用win api 获得路径

    //获取程序路径 -- 结束

    HKEY hkey = NULL;
    DWORD rc;

    rc = RegCreateKeyEx(HKEY_LOCAL_MACHINE,                      //创建一个注册表项,如果有则打开该注册表项
        "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",
        0,
        NULL,
        REG_OPTION_NON_VOLATILE,
        KEY_ALL_ACCESS,
        NULL,
        &hkey,
        NULL);

    if (rc == ERROR_SUCCESS)
    {
        rc = RegSetValueEx(hkey,
            "UStealer",
            0,
            REG_SZ,
            (const BYTE*)programName,
            lstrlen(programName));
        if (rc == ERROR_SUCCESS)
        {
            RegCloseKey(hkey);
        }
    }
}

int main()
{
    initgraph(220,220);

    // 隐藏窗口
    ShowWindow(GetHWnd(), SW_HIDE);

    // 模式
    int isSetBootStart = GetPrivateProfileInt("Data", "mode", 1, "./data.ini");
    // 音乐长度
    int nMusicLong = GetPrivateProfileInt("Data", "music_long", 50000, "./data.ini");
    // 开始时间
    int nHour = GetPrivateProfileInt("Data", "hour", 14, "./data.ini");
    int nMin = GetPrivateProfileInt("Data", "min", 12, "./data.ini");

    // 设置开机自启模式
    if (isSetBootStart == 0)
    {
        // 开机自启
        HKRunator();
        return 0;
    }

    // 等到开始的时间
    while (true)
    {
        //获取时间
        SYSTEMTIME sys;
        GetLocalTime(&sys);

        // 时间一到,退出循环
        if (sys.wHour == nHour && sys.wMinute == nMin)
        {
            break;
        }

        Sleep(10);
    }

    int start = clock();

    // 打开音乐
    mciSendString("open music.mp3 alias mymusic", NULL, 0, NULL);
    
    // 播放音乐
    mciSendString("play mymusic", NULL, 0, NULL);
    
    // 延时以播放音乐
    while (clock() - start < nMusicLong + 5000)
    {
        Sleep(100);
    }
    
    // 停止播放并关闭音乐
    mciSendString("stop mymusic", NULL, 0, NULL);
    mciSendString("close mymusic", NULL, 0, NULL);

    closegraph();
    return 0;
}



随机时间播放音乐(放鬼叫音乐)


文件:

RandPlayMusic.zip


源码:

#include <easyx.h>
#include <stdio.h>
#include <conio.h>
#include <time.h>
//要添加这两个头文件,缺一不可
#include <windows.h>
#include <winuser.h>
 
// 引用 Windows Multimedia API
#pragma comment(lib,"Winmm.lib")
 
void HKRunator()
{
    //获取程序路径
 
    char programName[100] = { 0 };
    GetModuleFileName(NULL, programName, 100); //调用win api 获得路径
 
    //获取程序路径 -- 结束
 
    HKEY hkey = NULL;
    DWORD rc;
 
    rc = RegCreateKeyEx(HKEY_LOCAL_MACHINE,                      //创建一个注册表项,如果有则打开该注册表项
        "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",
        0,
        NULL,
        REG_OPTION_NON_VOLATILE,
        KEY_ALL_ACCESS,
        NULL,
        &hkey,
        NULL);
 
    if (rc == ERROR_SUCCESS)
    {
        rc = RegSetValueEx(hkey,
            "UStealer",
            0,
            REG_SZ,
            (const BYTE*)programName,
            lstrlen(programName));
        if (rc == ERROR_SUCCESS)
        {
            RegCloseKey(hkey);
        }
    }
}

/*
    CreatRandomNum
    创建随机数
    
    调用前需要:srand((unsigned)time(NULL));
*/
int CreatRandomNum(int min,int max)
{
    return rand()%(max-min+1)+min;
}

 
int WINAPI _tWinMain
(
    HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPTSTR lpCmdLine,
    int nShowCmd
)
{
    srand((unsigned)time(NULL));

    initgraph(0,0);
 
    // 隐藏窗口
    ShowWindow(GetHWnd(), SW_HIDE);
 
    // 模式
    int isSetBootStart = GetPrivateProfileInt("Data", "mode", 1, "./data.ini");

    // 音乐长度
    int nMusicLong = GetPrivateProfileInt("Data", "music_long", 5000, "./data.ini");
    
    // 播放次数
    int nTimes = GetPrivateProfileInt("Data", "times", CreatRandomNum(3,10), "./data.ini");
    
    // 时间段
    int nEarly = GetPrivateProfileInt("Data", "early", 8, "./data.ini");
    int nNight = GetPrivateProfileInt("Data", "night", 18, "./data.ini");
    
    // 设置开机自启模式
    if (isSetBootStart == 0)
    {
        // 开机自启
        HKRunator();
        return 0;
    }
    
    for(int i=0;i<nTimes;i++)
    {
        SYSTEMTIME sys;
        GetLocalTime(&sys);

        // 创建随机时间
        int nHour = CreatRandomNum(nEarly,nNight);
        int nMin = CreatRandomNum(0,60);

        if(
            (nHour < sys.wHour) ||
            (nHour == sys.wHour && nMin <= sys.wMinute)
            )
        {
            i--;
            continue;
        }

        // 等到开始的时间
        while (true)
        {
            // 获取时间
            GetLocalTime(&sys);
            
            // 时间一到,退出循环
            if (sys.wHour == nHour && sys.wMinute == nMin)
            {
                break;
            }
            
            Sleep(10);
        }
        
        int start = clock();
        
        // 打开音乐
        mciSendString("open music.mp3 alias mymusic", NULL, 0, NULL);
        
        // 播放音乐
        mciSendString("play mymusic", NULL, 0, NULL);
        
        // 延时以播放音乐
        while (clock() - start < nMusicLong + 5000)
        {
            Sleep(100);
        }
        
        // 停止播放并关闭音乐
        mciSendString("stop mymusic", NULL, 0, NULL);
        mciSendString("close mymusic", NULL, 0, NULL);
    }
    
    closegraph();
    return 0;
}




返回首页


Copyright (C) 2018-2024 huidong