| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 527 人关注过本帖
标题:如何给程序增加音乐啊??
只看楼主 加入收藏
谢谆本
Rank: 2
来 自:湖南娄底
等 级:论坛游民
帖 子:55
专家分:49
注 册:2010-6-21
结帖率:66.67%
收藏
已结贴  问题点数:10 回复次数:3 
如何给程序增加音乐啊??
我做了个俄罗斯方块,个人想给它加点音乐希望各位大哥大姐大侠们指点一下下。。。。。谢谢了。。。。
搜索更多相关主题的帖子: 音乐 
2010-10-18 09:33
hahayezhe
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:湖南张家界
等 级:贵宾
威 望:24
帖 子:1386
专家分:6999
注 册:2010-3-8
收藏
得分:5 
playsound
bool LoadFile(LPCTSTR sFile, LPVOID & outData){   
        struct _stat buf;//struct _stat 获取普通文件
        if(_stat(sFile,&buf))
            return false;
        outData = new char[buf.st_size];
        FILE *fp = NULL;
        fopen_s(&fp,sFile,"rb");//以二进制形式打开文件
        fread(outData,buf.st_size,1,fp);
        fclose(fp);
        return true;
    }
    BOOL GetWaveData(void * pRes, WAVEFORMATEX * & pWaveHeader, void * & pbWaveData, DWORD & cbWaveSize){
        pWaveHeader = 0;
        pbWaveData = 0;
        cbWaveSize = 0;
        DWORD *pdw = (DWORD*)pRes;
        DWORD dwLength = *pdw++;
        DWORD dwRiff = *pdw++;
        DWORD dwType = *pdw++;
        if(dwRiff!=mmioFOURCC('R','I','F','F'))
            return false;
        if(dwType!=mmioFOURCC('W','A','V','E'))
            return false;
        DWORD*pdwEnd = (DWORD*)((BYTE*)pdw+dwLength-4);
        while(pdw<pdwEnd){
            dwType = *pdw++;
            dwLength = *pdw++;

            switch( dwType )
            {
            case mmioFOURCC('f', 'm', 't', ' '):
                if( !pWaveHeader )
                {
                    if( dwLength < sizeof(WAVEFORMAT) )
                        return false;      // not a WAV

                    pWaveHeader = (WAVEFORMATEX *)pdw;

                    if( pbWaveData && cbWaveSize )
                        return true;
                }
                break;

            case mmioFOURCC('d', 'a', 't', 'a'):
                pbWaveData = LPVOID(pdw);
                cbWaveSize = dwLength;

                if( pWaveHeader )
                    return true;
                break;
            }
            pdw = (DWORD *)((BYTE *)pdw + ((dwLength+1)&~1));
        }
        return false;
    }
    BOOL MyCreateSoundBuffer(WAVEFORMATEX * pcmwf){
        DSBUFFERDESC dsbdesc;

        // Set up DSBUFFERDESC structure.
        memset(&dsbdesc, 0, sizeof(DSBUFFERDESC)); // Zero it out.
        dsbdesc.dwSize = sizeof(DSBUFFERDESC);
        // Need no controls (pan, volume, frequency).
        dsbdesc.dwFlags = DSBCAPS_STATIC;        // assumes that the sound is played often
        dsbdesc.dwBufferBytes = m_dwTheSound;
        dsbdesc.lpwfxFormat = pcmwf;    // Create buffer.
        HRESULT hRes;
        hRes = m_ds->CreateSoundBuffer(&dsbdesc, &m_pDsb, 0);
        if (FAILED(hRes))
        {
            TRACE("MyCreateSoundBuffer FAILED!");
            return FALSE;
        }

        return TRUE;
    }
    BOOL SetSoundData(void * pSoundData, DWORD dwSoundSize){
        LPVOID lpvPtr1;
        DWORD dwBytes1;
        // Obtain write pointer.
        HRESULT hr = m_pDsb->Lock(0, 0, &lpvPtr1, &dwBytes1, 0, 0, DSBLOCK_ENTIREBUFFER);
        // If DSERR_BUFFERLOST is returned, restore and retry lock.
        if(DSERR_BUFFERLOST == hr)
        {
            m_pDsb->Restore();
            hr = m_pDsb->Lock(0, 0, &lpvPtr1, &dwBytes1, 0, 0, DSBLOCK_ENTIREBUFFER);
        }
        if(DS_OK == hr)
        {
            // Write to pointers.
            CopyMemory(lpvPtr1, pSoundData, dwBytes1);
            // Release the data back to DirectSound.
            hr = m_pDsb->Unlock(lpvPtr1, dwBytes1, 0, 0);
            if(DS_OK == hr)
                return TRUE;
        }
        // Lock, Unlock, or Restore failed.
        return FALSE;
    }
    void Play(){
        //TRACE("play");
        m_pDsb->SetCurrentPosition(0);
        if( DSERR_BUFFERLOST == m_pDsb->Play(0, 0, 0) )
        {
            // another application had stolen our buffer
            // Note that a "Restore()" is not enough, because
            // the sound data is invalid after Restore().
            SetSoundData(m_pTheSound, m_dwTheSound);

            // Try playing again
            m_pDsb->Play(0, 0, 0);
        }
    }
    void Stop(){
        m_pDsb->Stop();
    }
2010-10-18 10:02
hahayezhe
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:湖南张家界
等 级:贵宾
威 望:24
帖 子:1386
专家分:6999
注 册:2010-3-8
收藏
得分:0 
在SDK中用playsound  Windows设计里第一章的例子就说了

第二种是Direct3D编程 你可以去找相关例子IDirectSoundBuffer8
2010-10-18 10:05
zhooyu
Rank: 2
等 级:论坛游民
帖 子:34
专家分:12
注 册:2010-9-12
收藏
得分:5 
用mciSendString函数
//////////////////////////////////////////////////////////////////////////////////
#pragma comment(lib,"winmm.lib")

char szCommand[20];
//加载音乐,音乐文件名:bgMusic.mp3
sprintf(szCommand,"Open %s Alias bgMusic","bgMusic.mp3");
mciSendString(szCommand,NULL,0,0);
//播放音乐
sprintf(szCommand,"Play bgMusic From 0 repeat");
mciSendString(szCommand,NULL,0,0);
2010-10-18 21:55
快速回复:如何给程序增加音乐啊??
数据加载中...
 
   



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

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