• VC播放mp3的方法


    1、使用msi库

        #include <mmsystem.h>  
        #pragma comment(lib,"winmm.lib") 
    
        .......
    
        //打开文件
        MCI_OPEN_PARMS mciOpen;  
        mciOpen.lpstrDeviceType = _T("mpegvideo");  
        mciOpen.lpstrElementName = _T("c://1.mp3");  
        MCIERROR mciError = mciSendCommand(0, MCI_OPEN, MCI_OPEN_TYPE | MCI_OPEN_ELEMENT, (DWORD)&mciOpen);  
        if(mciError)  
        {  
            TCHAR buf[128]={0};  
            mciGetErrorString(mciError, buf, 128);  
            printf("%s/n",buf);  
        }  
    
        //播放(异步)
        UINT DeviceID = mciOpen.wDeviceID ;  
        MCI_PLAY_PARMS mciPlay;  
        mciError = mciSendCommand(DeviceID, MCI_PLAY, 0 , (DWORD)&mciPlay);
        if(mciError)  
        {  
            printf("send MCI_PLAY command failed/n");  
        }  
    
        //暂停
        //mciSendCommand(DeviceID, MCI_PAUSE, 0 , (DWORD)&mciPlay);
    
        //停止
        mciSendCommand(DeviceID, MCI_STOP, NULL , NULL);
        mciSendCommand(DeviceID, MCI_SEEK, MCI_SEEK_TO_START , NULL);
    
        //关闭
        //mciSendCommand(DeviceID, MCI_CLOSE, 0 , (DWORD)&mciPlay);
        
        //获得播放进度,仅为示例代码
         //MCI_STATUS_PARMS mciStatusParms;
         //mciStatusParms.dwItem=MCI_STATUS_POSITION;    
         //mciSendCommand(m_wDeviceID,MCI_STATUS,MCI_WAIT|MCI_STATUS_ITEM,(DWORD)(LPVOID)&mciStatusParms);//关键,取得位置
         //int m_dPosition=mciStatusParms.dwReturn;
    
         //获得整个音频的时间长度,仅为示例代码
         //UINT DeviceID = mciOpen.wDeviceID ;
         //mciStatusParms.dwItem=MCI_STATUS_LENGTH; 
         //mciSendCommand(DeviceID,MCI_STATUS,MCI_WAIT|MCI_STATUS_ITEM,(DWORD)(LPVOID)&mciStatusParms);//关键,取得长度
        // m_dLength=mciStatusParms.dwReturn;
    
         //在指定位置播放,仅为示例代码
         //MCI_PLAY_PARMS mciPlay;
         //mciPlay.dwFrom=m_dPosition;
         //mciError = mciSendCommand(m_wDeviceID,MCI_PLAY,MCI_FROM,(DWORD)&mciPlay);
    
         //控制音量,仅为示例代码
         //MCI_DGV_SETAUDIO_PARMS mciSetAudioPara; 
         //mciSetAudioPara.dwItem = MCI_DGV_SETAUDIO_VOLUME; 
         //mciSetAudioPara.dwValue = atoi(m_Volume.GetBuffer(m_Volume.GetLength()));//here is the value of the Volume(0--1000) 
         //mciSendCommand(m_wDeviceID, MCI_SETAUDIO, MCI_DGV_SETAUDIO_VALUE | MCI_DGV_SETAUDIO_ITEM, (DWORD)(LPVOID)&mciSetAudioPara); 

    2、使用vfw库

    #include <vfw.h>
        #pragma comment(lib,"vfw32.lib")
    
        .......
    
        //打开文件
        CString filename = _T("c://1.mp3");
        HWND hwnd = MCIWndCreate(m_hWnd,NULL,MCIWNDF_NOPLAYBAR,filename);
        ::ShowWindow(hwnd,SW_HIDE);
    
        //设置音量
        MCIWndSetVolume(hwnd,1000);
    
        //播放(异步)
        MCIWndPlay(hwnd);
    
        //关闭
        //MCIWndClose(hwnd);

    3、使用DirectShow

        #include   <dshow.h>
        #pragma comment(lib,"strmiids.lib")
    
        .......
    
        IGraphBuilder*   pGBuilder;  
        IMediaControl*   pMControl;  
        IMediaPosition*   pMPos;  
        IMediaEvent*   pMEvent; 
    
        CoInitialize(NULL);   //初始化COM  
        CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC, IID_IGraphBuilder,(void**)&pGBuilder);  
        pGBuilder->QueryInterface(IID_IMediaControl,(void**)&pMControl);  
        pGBuilder->QueryInterface(IID_IMediaEvent,(void**)&pMEvent);  
        pGBuilder->QueryInterface(IID_IMediaPosition,(void**)&pMPos);
    
        pGBuilder->RenderFile(L"C://1.mp3",   NULL);   //调入文件 
    
        //播放MP3文件 
        pMPos->put_CurrentPosition(0);   //移动到文件头 
        pMControl->Run();   //播放(异步) 
    
        // 停止播放和释放对象 
        /*pMControl->Stop();   //停止播放 
        pMControl->Release();
        pMControl=NULL;
        pMEvent->Release();
        pMEvent=NULL;
        pMPos->Release();
        pMPos=NULL;
        pGBuilder->Release();
        pGBuilder=NULL;
        CoUninitialize();   //释放COM  */
  • 相关阅读:
    groovy main method is use static main(args) //ok
    undefined reference to
    CuTest: C Unit Testing Framework
    screen to tmux: A Humble Quickstart Guide « My Humble Corner
    main,tmain,winmain()等函数——UNICODE sensensen 博客园
    Adding Unit Tests to a C Project NetBeans IDE 6.9 Tutorial
    罗马转数字
    About Luvit
    KISSY Keep It Simple & Stupid, Short & Sweet, Slim & Sexy...
    Create a CSV file
  • 原文地址:https://www.cnblogs.com/milanleon/p/5626829.html
Copyright © 2020-2023  润新知