【3D技术宅公社】XR数字艺术论坛  XR技术讨论 XR互动电影 定格动画

 找回密码
 立即注册

QQ登录

只需一步,快速开始

调查问卷
论坛即将给大家带来全新的技术服务,面向三围图形学、游戏、动画的全新服务论坛升级为UTF8版本后,中文用户名和用户密码中有中文的都无法登陆,请发邮件到324007255(at)QQ.com联系手动修改密码

3D技术论坛将以计算机图形学为核心,面向教育 推出国内的三维教育引擎该项目在持续研发当中,感谢大家的关注。

查看: 3719|回复: 11

如何使背景音乐循环播放

[复制链接]
发表于 2009-5-19 07:16:41 | 显示全部楼层 |阅读模式
如题:给个介绍...最好给出具体代码,解析下:
发表于 2009-5-19 10:01:09 | 显示全部楼层

??

没有前提吗??

比如说 是flash里面 还是DX里面的声音循环?

 楼主| 发表于 2009-5-19 12:50:01 | 显示全部楼层
DX
发表于 2009-5-19 15:08:54 | 显示全部楼层

 

g_pSegment->SetRepeats(DMUS_SEG_REPEAT_INFINITE);

 楼主| 发表于 2009-5-19 18:16:59 | 显示全部楼层

要不我直接给你们代码,你们帮我改下,让它可以实现音乐循环....

#include <windows.h>
#include <stdio.h>
#include <dshow.h>
#include "resource.h"

#pragma comment(lib, "dxguid.lib")
#pragma comment(lib, "strmiids.lib")

#pragma warning(disable : 4996)

#define Safe_Release(p) if((p)) (p)->Release();

// window handles, class.
HWND g_hwnd;
char g_class_name[] = "MP3PlayClass";

// DirectShows components
IGraphBuilder*  g_graph_builder = NULL;
IMediaControl*  g_media_control = NULL;
IMediaEvent*    g_media_event   = NULL;

//--------------------------------------------------------------------------------
// Play mp3 which specified by filename.
//--------------------------------------------------------------------------------
BOOL Play_MP3(char* filename)
{
    // convert filename to wide-character string
    WCHAR w_filename[MAX_PATH] = {0};

    mbstowcs(w_filename, filename, MAX_PATH);

    // render the file
    g_graph_builder->RenderFile(w_filename, NULL);

    // play the file, switches the entire filter graph into a running state.
    g_media_control->Run();

    return TRUE;
}

//--------------------------------------------------------------------------------
// Window procedure.
//--------------------------------------------------------------------------------
long WINAPI Window_Proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    }

    return (long) DefWindowProc(hwnd, msg, wParam, lParam);
}

//--------------------------------------------------------------------------------
// Main function, routine entry.
//--------------------------------------------------------------------------------
int WINAPI WinMain(HINSTANCE inst, HINSTANCE, LPSTR cmd_line, int cmd_show)
{
    WNDCLASS            win_class;
    MSG                 msg;   

    // create window class and register it
    win_class.style         = CS_HREDRAW | CS_VREDRAW;
    win_class.lpfnWndProc   = Window_Proc;
    win_class.cbClsExtra    = 0;
    win_class.cbWndExtra    = DLGWINDOWEXTRA;
    win_class.hInstance     = inst;
    win_class.hIcon         = LoadIcon(inst, IDI_APPLICATION);
    win_class.hCursor       = LoadCursor(NULL, IDC_ARROW);
    win_class.hbrBackground = (HBRUSH) (COLOR_BTNFACE + 1);
    win_class.lpszMenuName  = NULL;
    win_class.lpszClassName = g_class_name;   

    if(! RegisterClass(&win_class))
        return FALSE;

    // create the main window
    g_hwnd = CreateDialog(inst, MAKEINTRESOURCE(IDD_MP3PLAY), 0, NULL);

    ShowWindow(g_hwnd, cmd_show);
    UpdateWindow(g_hwnd);

    // initialize COM
    //
    // initialize the COM library on the current thread and identifies the concurrency model as single-thread
    // apartment (STA).
    CoInitialize(0);

    // create the DirectMusic performance object
    //
    // creates a single uninitialized object of the class associated with a specified CLSID.
    if(FAILED(CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder,
                               (void**)&g_graph_builder)))
    {
        MessageBox(NULL, "Unable to create DirectShow Graph Builder object.", "Error", MB_OK);
        return FALSE;
    }

    // Query for the media control and event objects
    g_graph_builder->QueryInterface(IID_IMediaControl, (void**)&g_media_control);
    g_graph_builder->QueryInterface(IID_IMediaEvent, (void**)&g_media_event);
   
    // play mp3
    Play_MP3("escape.mp3");
   
    // start message pump, waiting for signal to quit.
    ZeroMemory(&msg, sizeof(MSG));

    while(msg.message != WM_QUIT)
    {
        if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);           
        }            

        // get th status of the song, it if is done, exit program.

        long event_code, param1, param2;

        // retrieves the next notification event
        if(SUCCEEDED(g_media_event->GetEvent(&event_code, &param1, &param2, 1)))
        {
            if(event_code == EC_COMPLETE)
            {
                // frees resources associated with the parameters of an events.
                g_media_event->FreeEventParams(event_code, param1, param2);

                break;
            }               
        }

        // frees resources associated with the parameters of an events.
        g_media_event->FreeEventParams(event_code, param1, param2);
    }

    // stop music and relaese DirectShow objects

    // switches all filters in the filter graph to a stopped state.
    g_media_control->Stop();

    g_media_event->Release();
    g_media_event = NULL;

    g_media_control->Release();
    g_media_control = NULL;

    g_graph_builder->Release();
    g_graph_builder = NULL;

    UnregisterClass(g_class_name, inst);

    // release COM system
    //
    // Closes the COM library on the current thread, unloads all DLLs loaded by the thread, frees any other
    // resources that the thread maintains, and forces all RPC connections on the thread to close.
    CoUninitialize();
   
    return (int) msg.wParam;
}

发表于 2009-5-19 19:11:48 | 显示全部楼层
我晕 谁有空帮你该代码啊~~~~~~
 楼主| 发表于 2009-5-19 21:46:37 | 显示全部楼层

问个问题哈:我把循环弄在一个函数中,

void CMusic::Render()
{
 //m_pSegment->SetRepeats(DMUS_SEG_REPEAT_INFINITE);           //To loop the sound forever, pass in DMUS_SEG_REPEAT_INFINITE
 //Play the loaded sound
 m_pPerformance->laySegmentEx(m_pSegment, NULL, NULL, 0, 0, NULL, NULL, NULL);
}

但是在主函数那边输入music->Render();的时候会出错,会提示中断?不知道这个是什么原因?

可以解答下吗?

 楼主| 发表于 2009-5-19 21:48:47 | 显示全部楼层

MyGame.exe 中的 0x00438d59 处未处理的异常: 0xC0000005: 读取位置 0xcdcdcdcd 时发生访问冲突

 

发表于 2009-5-20 09:10:28 | 显示全部楼层

你把音乐播放放在循环里面了???

报错原因暂且不说

你知道程序循环的运行机制吗??

你看见过光盘卡盘死读或者磁带卡带吗?这样能发声吗~!!!

 楼主| 发表于 2009-5-20 12:18:18 | 显示全部楼层

初学者遇到问题正常....麻烦你可以解答下……

发表于 2009-5-20 19:05:57 | 显示全部楼层
你的问题也太频繁了~~
解答一下哪个?

 楼主| 发表于 2009-5-20 19:27:08 | 显示全部楼层

算了,求人不如求己……

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|3D数字艺术论坛 ( 沪ICP备14023054号 )

GMT+8, 2025-6-24 14:53

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表