| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2105 人关注过本帖
标题:中断音乐播放
只看楼主 加入收藏
yu965634383
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:119
专家分:195
注 册:2017-9-1
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:6 
中断音乐播放
void MciFunction (char *FilePath)
{
    FILE *fp = fopen ("MusicName.txt" , "r");
    if (!fp)
        return ;
    char FileName[260];
    char cmd[260];
    MCIDEVICEID dev = 0;
    MCI_OPEN_PARMSA open;
    DWORD n;

    MCI_PLAY_PARMS play;
    MCI_BREAK_PARMS br;
    play.dwCallback = NULL;
    play.dwFrom = 0;
    br.nVirtKey = 0x61;
    br.dwCallback = NULL;

    fgets (FileName , 260 , fp);
    FileName[strlen (FileName) - 1] = '\0';
    sprintf (cmd , "%s%s" , FilePath , FileName);
    open.lpstrDeviceType = NULL;
    open.lpstrElementName = cmd;
//    printf ("%s" , cmd);
    n = mciSendCommandA (NULL , MCI_OPEN ,
        MCI_OPEN_ELEMENT | MCI_WAIT | MCI_OPEN_SHAREABLE , (DWORD)(PVOID)&open);
    if (n == 0)
    {
        dev = open.wDeviceID;
    }
    else
    {
        puts ("失败");
        return;
    }
        
    n=mciSendCommand (dev , MCI_BREAK , MCI_BREAK_KEY ,(DWORD)(PVOID)&br);
//    printf ("%ld" , n);
    mciSendCommand (dev , MCI_PLAY , MCI_WAIT  , (DWORD)(PVOID)&play );   
}
我将“1”键设为中断音乐播放的按键,但为什么运行时要按两次音乐才中断啊?
搜索更多相关主题的帖子: 中断 音乐 open dev NULL 
2017-10-22 03:30
吹水佬
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:451
帖 子:10609
专家分:43210
注 册:2014-5-20
收藏
得分:20 
我的测试也一样,要按2下。
2017-10-22 16:58
yu965634383
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:119
专家分:195
注 册:2017-9-1
收藏
得分:0 
为啥会这样呢,按“1”键将wait功能中断后,如果不再按一次的话,wait的功能在整个程序中都是处于中断状态。如果在播放一首歌曲,我想换下一首的时候,按了“1”键,停止了歌曲,但是后面的歌曲也一个也播放不了了。
我想实现的功能是:播放一个文件夹内的所有MP3文件,在播放的过程中,我可以通过一个按键来切换下一首歌曲。如果在我不切换歌曲的情况下,播放完一首歌曲之后,它会自动播放下一首。
有没有什么办法实现这个功能啊?
我写了下面的代码,改来改去的,但是就是不行。难受……
求指教、求指点……
#define _CRT_SECURE_NO_WARNINGS
# include <stdio.h>
# include <conio.h>
# include <windows.h>
# include <stdlib.h>
# include <string.h>
# include <io.h>
# include <errno.h>
# pragma comment (lib , "WINMM.LIB")

void GetFileName (char *FilePath , char * FileNameExtension)
{
    FILE * fp = fopen ("MusicName.txt" , "w+");
    if (!fp)
        return ;
    _finddata_t Info;
    char cmd[MAX_PATH];
    sprintf (cmd , "%s%s" , FilePath , FileNameExtension);
    long hFind;
    hFind = _findfirst (cmd , &Info);
    fputs (Info.name , fp);
    if (hFind == -1)
    {
        puts ("失败");
        return ;
    }
    while (!_findnext (hFind , &Info))
    {
        fputs ("\n" , fp);
        puts (Info.name);
        fputs (Info.name , fp);
    }
    fputs ("\n" , fp);
    fclose (fp);
    _findclose (hFind);
}

void MciFunction (char *FilePath , char *FileName)
{
   
//    char FileName[260];
    char cmd[260];
   
    MCIDEVICEID dev = 0;
    MCI_OPEN_PARMSA open;
    DWORD n;

    MCI_PLAY_PARMS play;
    MCI_BREAK_PARMS br;

//    while (fgets (FileName , 260 , fp) != NULL)
//    {
    open.lpstrAlias = "mysong";
    play.dwCallback = NULL;
    play.dwFrom = 0;
    br.nVirtKey = VK_DOWN;
    br.dwCallback = NULL;

   
    FileName[strlen (FileName) - 1] = '\0';
    sprintf (cmd , "%s%s" , FilePath , FileName);
    open.lpstrDeviceType = NULL;
    open.lpstrElementName = cmd;
    printf ("%s\n" , cmd);
    n = mciSendCommandA (NULL , MCI_OPEN ,
        MCI_OPEN_ELEMENT | MCI_WAIT | MCI_OPEN_SHAREABLE , (DWORD)(PVOID)&open);
    if (n == 0)
    {
        dev = open.wDeviceID;
    //    printf ("%d\n" , open.dwCallback);
    }
    else
    {
        puts ("失败");
        return;
    }
//    MCI_STATUS_PARMS status;
//    status.dwItem = MCI_STATUS_POSITION;
//    printf ("%d\n", dev);   

    n=mciSendCommand (dev , MCI_BREAK , MCI_BREAK_KEY ,(DWORD)(PVOID)&br);

    mciSendCommand (dev , MCI_PLAY , MCI_WAIT , (DWORD)(PVOID)&play );

//    mciSendCommand (dev , MCI_STATUS , MCI_STATUS_ITEM , (DWORD)(PVOID)&status);
//    printf ("%d\n" , status.dwReturn);
   
    MCI_GENERIC_PARMS close;
    close.dwCallback = NULL;
    mciSendCommand (dev , MCI_CLOSE , 0 , (DWORD)(PVOID)&close);
    //}
    return ;
}

int main (void)
{
    char FilePath[MAX_PATH] = "C:\\Users\\yutang\\Desktop\\music\\";
    char FileNameExtension[32] = "*.mp3";
//    gets (FilePath);
//    gets (FileNameExtension);
    char FileName[260];
    FILE *fp = fopen ("MusicName.txt" , "r");
    if (!fp)
        return 0;
    GetFileName (FilePath , FileNameExtension);
    while (fgets (FileName , 260 , fp) != 0)
    {
        MciFunction (FilePath , FileName);
    }
    fclose (fp);
    return 0;
}

[此贴子已经被作者于2017-10-22 18:53编辑过]


菜鸟一枚,求各位大神多多关照。
2017-10-22 18:48
吹水佬
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:451
帖 子:10609
专家分:43210
注 册:2014-5-20
收藏
得分:0 
如果在自己设计的窗口播放,可试试用 MCI_NOTIFY、MCI_BREAK_HWND 等相关参数,在窗口过程处理 MM_MCINOTIFY 消息。
或者自己捕捉处理键盘动作。如:
#include <stdio.h>
#include <windows.h>

void _prnerr(MCIERROR err)
{
    char errBuf[1024];
    mciGetErrorString(err, errBuf, sizeof(errBuf));
    printf("error: %d  %s\n", err, errBuf);
}

int main(int argc, char *argv[])
{
    char FileName[] = "test.wav";
    MCIERROR err;
    MCIDEVICEID dev = 0;
    MCI_OPEN_PARMS open;
    open.lpstrDeviceType = "waveaudio";
    open.lpstrElementName = FileName;
    err = mciSendCommandA(0, MCI_OPEN, MCI_OPEN_TYPE|MCI_OPEN_ELEMENT, (DWORD)(LPVOID)&open);
    if (err != 0)
    {
        _prnerr(err);
        return 0;
    }
    dev = open.wDeviceID;
   
    MCI_STATUS_PARMS sp;
    sp.dwItem = MCI_STATUS_LENGTH;
    mciSendCommandA(dev, MCI_STATUS, MCI_STATUS_ITEM, (DWORD)(LPVOID)&sp);
    DWORD len = sp.dwReturn;

    MCI_PLAY_PARMS play;
    err = mciSendCommandA(dev, MCI_PLAY, 0, (DWORD)(LPVOID)&play);
    if (err != 0)
        _prnerr(err);

    DWORD pos = 0, pc;
    sp.dwItem = MCI_STATUS_POSITION;
    printf("按↓键中止\n");
    while (!GetAsyncKeyState(VK_DOWN) && pos<len)
    {
        mciSendCommandA(dev, MCI_STATUS, MCI_STATUS_ITEM, (DWORD)(LPVOID)&sp);
        pos = sp.dwReturn;
        pc = 100 * pos / len;
        if (pc%10 == 0)
            printf("   %d%%\r", pc);
    }
    mciSendCommandA(dev, MCI_CLOSE, 0, 0);
    return 0;
}


[此贴子已经被作者于2017-10-22 20:26编辑过]

2017-10-22 20:19
yu965634383
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:119
专家分:195
注 册:2017-9-1
收藏
得分:0 
好的, 我在去查查看看这方面的知识

菜鸟一枚,求各位大神多多关照。
2017-10-22 21:21
yu965634383
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:119
专家分:195
注 册:2017-9-1
收藏
得分:0 
#define _CRT_SECURE_NO_WARNINGS
# include <stdio.h>
# include <conio.h>
# include <windows.h>
# include <stdlib.h>
# include <string.h>
# include <io.h>
# include <errno.h>
# pragma comment (lib , "WINMM.LIB")

void Error (MCIERROR err)
{
    char buf[260];
    mciGetErrorStringA (err , buf , sizeof (buf));
    printf ("\t%d\t%s\n " , err , buf);
}

 void SetDown (void)
   {
      // Simulate a key press
         keybd_event( VK_DOWN,
                      0x5000,
                      KEYEVENTF_EXTENDEDKEY,
                      0 );

      // Simulate a key release
          keybd_event( VK_DOWN,
                      0x5000,
                      KEYEVENTF_KEYUP,
                      0);
 }

void GetFileName (char *FilePath , char * FileNameExtension)
{
    FILE * fp = fopen ("MusicName.txt" , "w+");
    if (!fp)
        return ;
    _finddata_t Info;
    char cmd[MAX_PATH];
    sprintf (cmd , "%s%s" , FilePath , FileNameExtension);
    long hFind;
    hFind = _findfirst (cmd , &Info);
    fputs (Info.name , fp);
    if (hFind == -1)
    {
        puts ("失败");
        return ;
    }
    while (!_findnext (hFind , &Info))
    {
        fputs ("\n" , fp);
        puts (Info.name);
        fputs (Info.name , fp);
    }
    fputs ("\n" , fp);
    fclose (fp);
    _findclose (hFind);
}

void MciFunction (char *FilePath , char *FileName)
{
   
//    char FileName[260];
    char cmd[260];
   
    MCIDEVICEID dev = 0;
    MCI_OPEN_PARMSA open;
    DWORD err;

    MCI_PLAY_PARMS play;
    MCI_BREAK_PARMS br;

//    while (fgets (FileName , 260 , fp) != NULL)
//    {
//    open.lpstrAlias = "mysong";
    play.dwCallback = NULL;
    play.dwFrom = 0;
    br.nVirtKey = VK_DOWN;
    br.hwndBreak = NULL;
    br.dwCallback = NULL;
     
   
    FileName[strlen (FileName) - 1] = '\0';
    sprintf (cmd , "%s%s" , FilePath , FileName);
    open.lpstrDeviceType = NULL;
    open.lpstrElementName = cmd;
    printf ("%s\n" , cmd);
    err = mciSendCommandA (NULL , MCI_OPEN ,
        MCI_OPEN_ELEMENT | MCI_WAIT | MCI_OPEN_SHAREABLE , (DWORD)(PVOID)&open);
    if (err)
    {
        Error (err);
        return;
    }
    dev = open.wDeviceID;
//    MCI_STATUS_PARMS status;
//    status.dwItem = MCI_STATUS_POSITION;

    err = mciSendCommand (dev , MCI_BREAK , MCI_BREAK_KEY ,(DWORD)(PVOID)&br);
    if (err)
    {
        Error (err);
        return;
    }
    err = mciSendCommand (dev , MCI_PLAY , MCI_WAIT , (DWORD)(PVOID)&play );
    SetDown ();
    Sleep (1000);
    if (err)
    {
        Error (err);
        return;
    }
//    mciSendCommand (dev , MCI_STATUS , MCI_STATUS_ITEM , (DWORD)(PVOID)&status);
//    printf ("%d\n" , status.dwReturn);
   
    MCI_GENERIC_PARMS close;
    close.dwCallback = NULL;
    mciSendCommand (dev , MCI_CLOSE , 0 , (DWORD)(PVOID)&close);
    //}
}

int main (void)
{
    char FilePath[MAX_PATH] = "C:\\Users\\yutang\\Desktop\\music\\";
    char FileNameExtension[32] = "*.mp3";
//    gets (FilePath);
//    gets (FileNameExtension);
    char FileName[260];
    FILE *fp = fopen ("MusicName.txt" , "r");
    if (!fp)
        return 0;
    GetFileName (FilePath , FileNameExtension);
    while (fgets (FileName , 260 , fp) != 0)
    {
        MciFunction (FilePath , FileName);
    }
    fclose (fp);
    return 0;
}
我加了一个函数,void SetDown (void),有那么一点意思了,但还是有些问题。总是隔一首播放一首,不知道是怎么回事。
MCI_BREAK_HWND 这个句柄是怎么来的,怎么获取这个参数啊 。
我在上面查了下,HWND  hwndBreak 只是介绍了这个成员代表了什么,但是怎么获取,怎么用就不知道了。
图片附件: 游客没有浏览图片的权限,请 登录注册


[此贴子已经被作者于2017-10-23 03:04编辑过]


菜鸟一枚,求各位大神多多关照。
2017-10-23 02:53
yu965634383
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:119
专家分:195
注 册:2017-9-1
收藏
得分:0 


#define _CRT_SECURE_NO_WARNINGS
# include <stdio.h>
# include <conio.h>
# include <windows.h>
# include <stdlib.h>
# include <string.h>
# include <io.h>
# include <errno.h>
# pragma comment (lib , "WINMM.LIB")

void KeyboardEvent (int VirKey)//这个函数不能用
{
    HANDLE hIn = GetStdHandle (STD_INPUT_HANDLE);
    DWORD UL;
    INPUT_RECORD inrec;
    ReadConsoleInput (hIn , &inrec , 1 , &UL);
    if (inrec.EventType == KEY_EVENT)
    {
        if (inrec.Event.KeyEvent.wVirtualKeyCode == VirKey
            )
            BlockInput (1);//这个函数一旦运行,鼠标,键盘都被锁定,无法控制。
        else
            BlockInput (0);
    }
}

void Error (MCIERROR err)
{
    char buf[260];
    mciGetErrorStringA (err , buf , sizeof (buf));
    printf ("\t%d\t%s\n " , err , buf);
}

 void SetDown (void)
{

    // Simulate a key press
        keybd_event( VK_DOWN,
                    0x5000,
                    KEYEVENTF_EXTENDEDKEY|0,
                    0 );

    // Simulate a key release
        keybd_event( VK_DOWN,
                    0x5000,
                    KEYEVENTF_EXTENDEDKEY|KEYEVENTF_KEYUP,
                    0);
}

void GetFileName (char *FilePath , char * FileNameExtension)
{
    FILE * fp = fopen ("MusicName.txt" , "w+");
    if (!fp)
        return ;
    _finddata_t Info;
    char cmd[MAX_PATH];
    sprintf (cmd , "%s%s" , FilePath , FileNameExtension);
    long hFind;
    hFind = _findfirst (cmd , &Info);
    fputs (Info.name , fp);
    if (hFind == -1)
    {
        puts ("失败");
        return ;
    }
    while (!_findnext (hFind , &Info))
    {
        fputs ("\n" , fp);
        puts (Info.name);
        fputs (Info.name , fp);
    }
    fputs ("\n" , fp);
    fclose (fp);
    _findclose (hFind);
}

void MciFunction (char *FilePath , char *FileName)
{
   
    char cmd[260];
   
    MCIDEVICEID dev = 0;
    MCI_OPEN_PARMSA open;
    DWORD err;

    MCI_PLAY_PARMS play;
    MCI_BREAK_PARMS br;


    play.dwCallback = NULL;
    play.dwFrom = 0;
    br.nVirtKey = VK_DOWN;
    br.hwndBreak = NULL;
    br.dwCallback = NULL;
     
   
    FileName[strlen (FileName) - 1] = '\0';
    sprintf (cmd , "%s%s" , FilePath , FileName);
    open.lpstrDeviceType = NULL;
    open.lpstrElementName = cmd;
    printf ("%s\n" , cmd);
    err = mciSendCommandA (NULL , MCI_OPEN ,
        MCI_OPEN_ELEMENT | MCI_WAIT | MCI_OPEN_SHAREABLE , (DWORD)(PVOID)&open);
    if (err)
    {
        Error (err);
        return;
    }
    dev = open.wDeviceID;
    int i = 2;
    while (i)
    {
        SetDown ();
        i--;
//        printf ("2\n");
   
    err = mciSendCommand (dev , MCI_BREAK , MCI_BREAK_KEY ,(DWORD)(PVOID)&br);
   
    if (err)
    {
        Error (err);
        return;
    }
    short n = 0;
    n=GetKeyState (br.nVirtKey);//这个函数用来检测按键是否按下抬起
//    printf ("%d\n" , n);
   
    err = mciSendCommand (dev , MCI_PLAY , MCI_WAIT , (DWORD)(PVOID)&play );
   
    }

    if (err)
    {
        Error (err);
        return;
    }
   
    MCI_GENERIC_PARMS close;
    close.dwCallback = NULL;
    mciSendCommand (dev , MCI_CLOSE , 0 , (DWORD)(PVOID)&close);
   
}

int main (void)
{
    char FilePath[MAX_PATH] = "C:\\Users\\yutang\\Desktop\\music\\";
    char FileNameExtension[32] = "*.mp3";
//    gets (FilePath);
//    gets (FileNameExtension);
    char FileName[260];
    FILE *fp = fopen ("MusicName.txt" , "r");
    if (!fp)
        return 0;
    GetFileName (FilePath , FileNameExtension);
    while (fgets (FileName , 260 , fp) != 0)
    {
        MciFunction (FilePath , FileName);
    }
    fclose (fp);
    return 0;
}
终于是弄好了,但是这个keybd_event (); 函数还是有点不太明白

菜鸟一枚,求各位大神多多关照。
2017-10-23 13:02
快速回复:中断音乐播放
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.043270 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved