我刚开始做wince开发,想播放声音,但不知该怎没做,有一段代码放到这里,望大家帮我分析一下,做过wince/mobile
开发的更好,愿大家多提宝贵意见,谢谢!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Symbol;
using Symbol.Audio;
private Controller MyAudioController = null;
public enum AudioFrequency //枚举
{
Height = 1,
Middle = 2,
Low = 3
}
private void DoPlayAudio(AudioFrequency af)
{
int Duration = 200; // 时长
int Frequency = 400; // 频率
switch (af)
{
case AudioFrequency.Height:
Frequency = 2670;
Duration = 600;
break;
case AudioFrequency.Middle:
Frequency = 1900;
Duration = 400;
break;
case AudioFrequency.Low:
Frequency = 1500;
Duration = 300;
break;
default:
break;
}
try
{
this. MyAudioController.PlayAudio(Duration, Frequency); // play Default beep
}
catch(Exception es)
{
MessageBox.Show(es.Message);
}
}
然后再一个事件中调用一下DoPlayAudio()方法,
DoPlayAudio(AudioFrequency.Height);
运行时他会报错,是:NullReferenceException;
请问播放声音究竟应该怎么做,还有那个Controller类的MyAudioController 我要给它赋个什么值才可以,Controller类到底怎么用?(不好意思,问题有点多,但我现在确实刚开始做这个,望大家不吝赐教,谢谢!)
IGraphBuilder *pGraph;
/* 指向Filter Graph的指针 */
IMediaControl *pMediaControl;
/* 指向MediaControl对象的指针 */
/* 初始化,程序启动时执行一次即可 */
CoInitialize(NULL);
/**************************************************************************/
/* 以下程序每次播放MP3(或其他格式)文件时调用 */
/* 创建一个Filter Graph manager */
CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC, IID_IGraphBuilder, (void **)&pGraph);
pGraph->QueryInterface(IID_IMediaControl, (void **)&pMediaControl);
/* 连接播放指定文件所需的filter */
pGraph->RenderFile(L"\\ResidentFlash\\test.mp3", NULL);
/* 开始播放指定的文件 */
pMediaControl->Run();
/* 文件的播放在DirectShow中独立线程中进行,此处等待 */
::MessageBox(NULL, L"点击[OK]结束播放", L"Play MP3", MB_OK);
pMediaControl->Stop();
/* 停止播放 */
pMediaControl->Release();
/* 释放对象 */
pGraph->Release();
/**************************************************************************/
/* 程序结束时调用 */
CoUninitialize();