首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

小弟我又来了。mciSendString的有关问题

2013-08-09 
我又来了。。。mciSendString的问题上次已经解决了mciSendString播放的问题,采用的是在线程中播放。现在有一个

我又来了。。。mciSendString的问题
上次已经解决了mciSendString播放的问题,采用的是在线程中播放。

现在有一个问题,我写了一个自动播放的方法,有多个key音,然后逐个播放。发现,当播放了前面一部分,大概百来个key音(不固定)后,后面的音乐就没声音了。

不知道是什么原因呢。。。求大大们帮忙!
播放的线程函数是这样的


unsigned _stdcall thread_play(LPVOID lpParam)
{
char sz_command[126] = {0};
char sz_cur_play[32] = {0};


wsprintfA(sz_command, "open "%s\\key\\%c.mp3" alias key_%c", sz_file_path, (WORD)lpParam, (WORD)lpParam);
mciSendString(sz_command, 0, 0, 0 );

wsprintfA(sz_cur_play, "key_%c", (WORD)lpParam);

wsprintfA(sz_command, "play %s", sz_cur_play);
mciSendString(sz_command, 0, 0, 0);

return 0;
}

[解决办法]
仅供参考:
VERSION 5.00
Begin VB.Form Form1
   Caption         =   "Form1"
   ClientHeight    =   1590
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   5190
   LinkTopic       =   "Form1"
   ScaleHeight     =   1590
   ScaleWidth      =   5190
   StartUpPosition =   3  '窗口缺省
   Begin VB.TextBox Text1
      Height          =   375
      Left            =   360
      TabIndex        =   1
      Text            =   "Text1"
      Top             =   360


      Width           =   4455
   End
   Begin VB.CommandButton Command2
      Caption         =   "Command2"
      Height          =   495
      Left            =   1800
      TabIndex        =   0
      Top             =   840
      Width           =   1215
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Private Declare Function midiOutClose Lib "winmm.dll" (ByVal hMidiOut As Long) As Long
Private Declare Function midiOutOpen Lib "winmm.dll" (lphMidiOut As Long, ByVal uDeviceID As Long, ByVal dwCallback As Long, ByVal dwInstance As Long, ByVal dwFlags As Long) As Long
Private Declare Function midiOutShortMsg Lib "winmm.dll" (ByVal hMidiOut As Long, ByVal dwMsg As Long) As Long
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
'--------------------
Dim notes(1 To 8, 1 To 2) As Variant
'--------------------
Private Sub Form_Load()
    Randomize '此语句应在初始化时调用一次即可
    notes(1, 1) = "C4": notes(1, 2) = 60
    notes(2, 1) = "D4": notes(2, 2) = 62
    notes(3, 1) = "E4": notes(3, 2) = 64
    notes(4, 1) = "F4": notes(4, 2) = 65
    notes(5, 1) = "G4": notes(5, 2) = 67
    notes(6, 1) = "A5": notes(6, 2) = 69


    notes(7, 1) = "B5": notes(7, 2) = 71
    notes(8, 1) = "C5": notes(8, 2) = 72
End Sub
'--------------------
Private Sub Command2_Click()
Dim lMidiAPIReturn As Long
Dim tone As Integer
Dim mlmidiouthandle As Long
Dim volume As Long
Dim channel As Long
Dim i As Integer
Dim a As Integer
Dim b As Integer
Dim t As Integer
Dim s(8) As Integer
    For i = 1 To 8
        s(i) = i
    Next

    '随机打乱s(i)的顺序 即洗牌
    For i = 8 To 2 Step -1
        a = i: b = Int(Rnd() * i) + 1
        If a <> b Then
            t = s(a): s(a) = s(b): s(b) = t
        End If
    Next

    mlmidiouthandle = 0
    lMidiAPIReturn = midiOutOpen(mlmidiouthandle, -1, 0, 0, 0)
    volume = 90
    channel = 0

    Text1.Text = ""
    For i = 1 To 8
        Text1.Text = Text1.Text + CStr(s(i)) + " " + notes(s(i), 1) + ", "
        Text1.Refresh
        tone = notes(s(i), 2)
        lMidiAPIReturn = midiOutShortMsg(mlmidiouthandle, &H90 + ((tone) * &H100) + (volume * &H10000) + channel)
        Sleep 100
    Next

    lMidiAPIReturn = midiOutClose(mlmidiouthandle)
End Sub


[解决办法]


发了一部分就不能继续播放了?这个应该是代码有隐藏bug了!
查查内存指针这些问题,以及你读取数据桢的时候会不会没考虑到情况,

用抓包工具抓包看看!

热点排行