|
在做一个播放Mp3的程序(c#),功能很简单,就是普通的播放,停止,暂停,向前,向后,还有一个TrackBar可以进行进度的调节。
写了一个clsMusic类专门负责FMOD相关的操作。
平时操作都正常,但是进行了channel.stop操作后再次播放时就有问题。
问题如下:
1)音乐自行播放结束或者点击停止(channel,stop)后,调节TrackBar后出现错误。
这个错误总出现在下面代码的“MC00027”位置。
2)以很高的频率进行,停止,播放操作(就是点击完播放后立刻点击停止,再立刻点击播放.....)
会出现错误。
以上两种问题返回的错误为如下三种,其中头两种出现频率高,第三种频率低。
FMOD_ERR_INVALID_HANDLE
FMOD_ERR_INVALID_PARAM
FMOD_ERR_OUTPUT_DRIVERCALL
贴出部分代码如下,希望能得到您的指点,先谢谢了!
下面代码中,
FmodRun() 为线程函数,其他函数都是通过窗口线程进行调用的。
有几个结构和方法是负责处理错误的,不影响您的阅读,也不影响程序。
- /// <summary>
- /// 初始化函数
- /// </summary>
- public ErrorParam FmodInit()
- {
- uint version = 0;
- FMOD.RESULT result;
- /*
- Global Settings
- */
- result = FMOD.Factory.System_Create(ref system);
- ERRCHECK(result,ref ep, "MC00001", "FMOD0001");
- if (ep.isError) return ep;
- result = system.getVersion(ref version);
- ERRCHECK(result, ref ep, "MC00002", "FMOD0001");
- if (ep.isError) return ep;
- if (version < FMOD.VERSION.number)
- {
- //MessageBox.Show("Error! You are using an old version of FMOD " + version.ToString("X") + ". This program requires " + FMOD.VERSION.number.ToString("X") + ".");
- //Application.Exit();
- clsLanguage.SetErrorParam(ref ep, "MC00006", "FMOD0002", version.ToString("X") + "," + FMOD.VERSION.number.ToString("X"));
- return ep;
- }
- result = system.init(32, FMOD.INITFLAG.NORMAL, (IntPtr)null);
- ERRCHECK(result, ref ep, "MC00003", "FMOD0001");
- if (ep.isError) return ep;
- result = system.createSound(m_strMusicPath, (FMOD.MODE._2D | FMOD.MODE.HARDWARE | FMOD.MODE.CREATESTREAM), ref sound);
- //result = system.createSound(m_strMusicPath, FMOD.MODE.HARDWARE, ref sound);
- ERRCHECK(result, ref ep, "MC00004", "FMOD0001");
- if (ep.isError) return ep;
- result = sound.getLength(ref musiclength, FMOD.TIMEUNIT.MS);
- ERRCHECK(result, ref ep, "MC00021", "FMOD0001");
- if (ep.isError) return ep;
- //设置音乐条最大刻度
- mct.SetSoundTrackBarLength((int)musiclength);
- if (musicthread == null)
- {
- musicthread = new Thread(this.FmodRun);
- }
- isPlayMusic = true;
- musicthread.Start();
- clsLanguage.GetOKErrorParam(ref ep);
- return ep;
- }
- /// <summary>
- /// 线程执行函数
- /// </summary>
- private void FmodRun()
- {
- while (isPlayMusic)
- {
- uint ms = 0;
- if (channel != null)
- {
- //result = channel.isPlaying(ref playing);
- //if ((result != FMOD.RESULT.OK) && (result != FMOD.RESULT.ERR_INVALID_HANDLE))
- //{
- // ERRCHECK(result);
- //}
- //result = channel.getPaused(ref paused);
- //if ((result != FMOD.RESULT.OK) && (result != FMOD.RESULT.ERR_INVALID_HANDLE))
- //{
- // ERRCHECK(result);
- //}
- result = channel.getPosition(ref ms, FMOD.TIMEUNIT.MS);
- if ((result != FMOD.RESULT.OK) && (result != FMOD.RESULT.ERR_INVALID_HANDLE))
- {
- ERRCHECK(result, ref ep, "MC00011", "FMOD0001");
- clsNotify.Instance().SendMessage(ep);
- isPlayMusic = false;
- return;
- }
- if (!mct.isSoundCtrl)
- {
- mct.SetSoundInfo((int)ms);
- mct.SetSoundTrackValue((int)ms);
- }
-
- }
- //statusBar.Text = "Time " + (ms / 1000 / 60) + ":" + (ms / 1000 % 60) + ":" + (ms / 10 % 100) + "/" + (lenms / 1000 / 60) + ":" + (lenms / 1000 % 60) + ":" + (lenms / 10 % 100) + " : " + (paused ? "Paused " : playing ? "Playing" : "Stopped");
- if (system != null)
- {
- system.update();
- }
- Thread.Sleep(10);
- }
- }
- /// <summary>
- /// 设置音乐当前的位置,TrackBar调整数值后调用
- /// </summary>
- /// <param name="pos">要设置的位置</param>
- public ErrorParam setPosition(uint pos)
- {
- if (channel != null)
- {
- bool isplay = false;
- if (pos >= musiclength) pos = 0;
- result = channel.isPlaying(ref isplay);
- switch (result)
- {
- case FMOD.RESULT.OK:
- if (isplay)
- {
- result = channel.setPosition(pos, FMOD.TIMEUNIT.MS);
- if (result != FMOD.RESULT.OK)
- {
- ERRCHECK(result, ref ep, "MC00030", "FMOD0001");
- return ep;
- }
- }
- break;
- case FMOD.RESULT.ERR_INVALID_HANDLE:
- result = system.playSound(FMOD.CHANNELINDEX.FREE, sound, true, ref channel);
- if (result != FMOD.RESULT.OK)
- {
- ERRCHECK(result, ref ep, "MC00027", "FMOD0001");
- return ep;
- }
- result = channel.setPosition(pos, FMOD.TIMEUNIT.MS);
- if (result != FMOD.RESULT.OK)
- {
- ERRCHECK(result, ref ep, "MC00029", "FMOD0001");
- return ep;
- }
- break;
- default:
- ERRCHECK(result, ref ep, "MC00028", "FMOD0001");
- return ep;
- }
- }
- clsLanguage.GetOKErrorParam(ref ep);
- return ep;
- }
- /// <summary>
- /// 播放
- /// </summary>
- public ErrorParam Play()
- {
- if (channel != null)
- {//已经播放了
- bool paused = false;
- bool isplay = false;
- //获得播放/暂停状态
- result = channel.isPlaying(ref isplay);
- switch (result)
- {
- case FMOD.RESULT.OK:
- if (isplay)
- {//已经播放
- result = channel.getPaused(ref paused);
- if ((result != FMOD.RESULT.OK) && (result != FMOD.RESULT.ERR_INVALID_HANDLE))
- {
- ERRCHECK(result, ref ep, "MC00006", "FMOD0001");
- return ep;
- }
- if (paused)
- {//暂停
- result = channel.setPaused(false);
- if ((result != FMOD.RESULT.OK) && (result != FMOD.RESULT.ERR_INVALID_HANDLE))
- {
- ERRCHECK(result, ref ep, "MC00007", "FMOD0001");
- return ep;
- }
- clsLanguage.GetOKErrorParam(ref ep);
- return ep;
- }
- else
- {//播放中
- clsLanguage.GetOKErrorParam(ref ep);
- return ep;
- }
- }
- else
- {//还未播放
- result = system.playSound(FMOD.CHANNELINDEX.FREE, sound, true, ref channel);
- if (result != FMOD.RESULT.OK)
- {
- ERRCHECK(result, ref ep, "MC00008", "FMOD0001");
- return ep;
- }
-
- }
- break;
- case FMOD.RESULT.ERR_INVALID_HANDLE:
- result = system.playSound(FMOD.CHANNELINDEX.FREE, sound, true, ref channel);
- if (result != FMOD.RESULT.OK)
- {
- ERRCHECK(result, ref ep, "MC00026", "FMOD0001");
- return ep;
- }
- break;
- default:
- ERRCHECK(result, ref ep, "MC00009", "FMOD0001");
- return ep;
- }
- }
- else
- {//从未播放过
- result = system.playSound(FMOD.CHANNELINDEX.FREE, sound, true, ref channel);
- if (result != FMOD.RESULT.OK)
- {
- ERRCHECK(result, ref ep, "MC00010", "FMOD0001");
- return ep;
- }
- }
- int pos = mct.GetSoundTrackValue();
- if (pos == (int)musiclength) pos = 0;
- result = channel.setPosition((uint)pos, FMOD.TIMEUNIT.MS);
- if (result != FMOD.RESULT.OK)
- {
- ERRCHECK(result, ref ep, "MC00024", "FMOD0001");
- return ep;
- }
- result = channel.setPaused(false);
- if (result != FMOD.RESULT.OK)
- {
- ERRCHECK(result, ref ep, "MC00025", "FMOD0001");
- return ep;
- }
- clsLanguage.GetOKErrorParam(ref ep);
- return ep;
- }
- /// <summary>
- /// 停止播放
- /// </summary>
- /// <returns></returns>
- public ErrorParam Stop()
- {
- if (channel != null)
- {
- result = channel.stop();
- if ((result != FMOD.RESULT.OK) && (result != FMOD.RESULT.ERR_INVALID_HANDLE))
- {
- ERRCHECK(result, ref ep, "MC00023", "FMOD0001");
- return ep;
- }
- }
- clsLanguage.GetOKErrorParam(ref ep);
- return ep;
- }
复制代码 |
|