我用的为WIN98的系统。为什么我用waveoutgetvolume获得声音的时候,总是得以同一个值。
  还有一个问题是:用waveoutgetvolume得到的值,再原封不动的用waveoutsetvolume设置它。结果在托盘中的音量改变了,不知道是什么原因。
  下面是我的程序代码:
  var fdsa:DWord;
  begin
    waveoutgetvolume(0,@fdsa);
    waveoutsetvolume(0,fdsa);
  end;
  为了防止是设备代号的问题,我把waveoutgetvolume中的0 从1 到7 都试过了还是不行。不知道什么原因。
  请各位前辈们指点!!

解决方案 »

  1.   

    波形音量:
    function GetWaveVolume(var Value: Byte; var ErrorStr: String): Boolean;
    var
      Error: LongInt;
      hMix: HMIXER;
      Ml: MIXERLINE;
      Mc: MIXERCONTROL;
      Mlc: MIXERLINECONTROLS;
      Mcd: TMIXERCONTROLDETAILS;
      Mcdu: MIXERCONTROLDETAILS_UNSIGNED;
    begin
      Error := mixerOpen(@hMix, 0, 0, 0, 0);
      Result := Error = MMSYSERR_NOERROR;
      if Result then begin
        try
          ml.cbStruct        := sizeof(MIXERLINE);
          ml.dwComponentType := MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT;
         // get the Wave line of the mixer device
          Error := mixerGetLineInfo(hMix, @ml, MIXER_GETLINEINFOF_COMPONENTTYPE);
          Result := Error = MMSYSERR_NOERROR;
          if Result then begin
            mlc.cbStruct      := sizeof(MIXERLINECONTROLS);
            mlc.dwLineID      := ml.dwLineID;
            mlc.dwControlType := MIXERCONTROL_CONTROLTYPE_VOLUME;
            mlc.cControls     := 1;
            mlc.pamxctrl      := @mc;
            mlc.cbmxctrl      := sizeof(MIXERCONTROL);
            // get the volume controls associated with the Waveout line
            Error := mixerGetLineControls(hMix, @mlc, MIXER_GETLINECONTROLSF_ONEBYTYPE);
            REsult := MMSYSERR_NOERROR = Error;
            if Result then begin
              mcdu.dwValue    := Round(Mc.Bounds.dwMaximum * (Value / 100));
              mcd.cbStruct    := sizeof(TMIXERCONTROLDETAILS);
              mcd.hwndOwner   := 0;
              mcd.dwControlID := mc.dwControlID;
              mcd.paDetails   := @mcdu;
              mcd.cbDetails   := sizeof(MIXERCONTROLDETAILS_UNSIGNED);
              mcd.cChannels   := 1;
              // set the volume
              Error := mixerGetControlDetails(hMix, @mcd, MIXER_SETCONTROLDETAILSF_VALUE);
              Value := Round((mcdu.dwValue * 100)/Mc.Bounds.dwMaximum);
              Result := Error = MMSYSERR_NOERROR;
              if not Result then ErrorStr := 'MixerGetControlDetails() Failed!';
            end else ErrorStr := 'MixerGetLineControls() Failed!';
          end else ErrorStr := 'MixerGetLineInfo() Failed!';
        finally
          MixerClose(hMix);
        end;
      end else ErrorStr := 'MixerOpen() Failed!';
    end;
    function SetWaveVolume(const Value: Byte; var ErrorStr: String): Boolean;
    var
      Error: LongInt;
      hMix: HMIXER;
      Ml: MIXERLINE;
      Mc: MIXERCONTROL;
      Mlc: MIXERLINECONTROLS;
      Mcd: TMIXERCONTROLDETAILS;
      Mcdu: MIXERCONTROLDETAILS_UNSIGNED;
    begin
      Error := mixerOpen(@hMix, 0, 0, 0, 0);
      Result := Error = MMSYSERR_NOERROR;
      if Result then begin
        try
          ml.cbStruct        := sizeof(MIXERLINE);
          ml.dwComponentType := MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT;
         // get the Wave line of the mixer device
          Error := mixerGetLineInfo(hMix, @ml, MIXER_GETLINEINFOF_COMPONENTTYPE);
          Result := Error = MMSYSERR_NOERROR;
          if Result then begin
            mlc.cbStruct      := sizeof(MIXERLINECONTROLS);
            mlc.dwLineID      := ml.dwLineID;
            mlc.dwControlType := MIXERCONTROL_CONTROLTYPE_VOLUME;
            mlc.cControls     := 1;
            mlc.pamxctrl      := @mc;
            mlc.cbmxctrl      := sizeof(MIXERCONTROL);
            // get the volume controls associated with the Waveout line
            Error := mixerGetLineControls(hMix, @mlc, MIXER_GETLINECONTROLSF_ONEBYTYPE);
            REsult := MMSYSERR_NOERROR = Error;
            if Result then begin
              mcdu.dwValue    := Round(Mc.Bounds.dwMaximum * (Value / 100));
              mcd.cbStruct    := sizeof(TMIXERCONTROLDETAILS);
              mcd.hwndOwner   := 0;
              mcd.dwControlID := mc.dwControlID;
              mcd.paDetails   := @mcdu;
              mcd.cbDetails   := sizeof(MIXERCONTROLDETAILS_UNSIGNED);
              mcd.cChannels   := 1;
              // set the volume
              Error := mixerSetControlDetails(hMix, @mcd, MIXER_SETCONTROLDETAILSF_VALUE);
              Result := Error = MMSYSERR_NOERROR;
              if not Result then ErrorStr := 'MixerSetControlDetails() Failed!';
            end else ErrorStr := 'MixerGetLineControls() Failed!';
          end else ErrorStr := 'MixerGetLineInfo() Failed!';
        finally
          MixerClose(hMix);
        end;
      end else ErrorStr := 'MixerOpen() Failed!';
    end;
      

  2.   

    主音量:
    function GetMasterVolume(var Value: Byte; var ErrorStr: String): Boolean;
    var
      Error: LongInt;
      hMix: HMIXER;
      Ml: MIXERLINE;
      Mc: MIXERCONTROL;
      Mlc: MIXERLINECONTROLS;
      Mcd: TMIXERCONTROLDETAILS;
      Mcdu: MIXERCONTROLDETAILS_UNSIGNED;
    begin
      Error := mixerOpen(@hMix, 0, 0, 0, 0);
      Result := Error = MMSYSERR_NOERROR;
      if Result then begin
        try
          ml.cbStruct        := sizeof(MIXERLINE);
          ml.dwComponentType := MIXERLINE_COMPONENTTYPE_DST_SPEAKERS;
         // get the speaker line of the mixer device
          Error := mixerGetLineInfo(hMix, @ml, MIXER_GETLINEINFOF_COMPONENTTYPE);
          Result := Error = MMSYSERR_NOERROR;
          if Result then begin
            mlc.cbStruct      := sizeof(MIXERLINECONTROLS);
            mlc.dwLineID      := ml.dwLineID;
            mlc.dwControlType := MIXERCONTROL_CONTROLTYPE_VOLUME;
            mlc.cControls     := 1;
            mlc.pamxctrl      := @mc;
            mlc.cbmxctrl      := sizeof(MIXERCONTROL);
            // get the volume controls associated with the speaker line
            Error := mixerGetLineControls(hMix, @mlc, MIXER_GETLINECONTROLSF_ONEBYTYPE);
            REsult := MMSYSERR_NOERROR = Error;
            if Result then begin
              mcdu.dwValue    := Round(Mc.Bounds.dwMaximum * (Value / 100));
              mcd.cbStruct    := sizeof(TMIXERCONTROLDETAILS);
              mcd.hwndOwner   := 0;
              mcd.dwControlID := mc.dwControlID;
              mcd.paDetails   := @mcdu;
              mcd.cbDetails   := sizeof(MIXERCONTROLDETAILS_UNSIGNED);
              mcd.cChannels   := 1;
              // set the volume
              Error := mixerGetControlDetails(hMix, @mcd, MIXER_SETCONTROLDETAILSF_VALUE);
              Value := Round(mcdu.dwValue * 100 / Mc.Bounds.dwMaximum);
              Result := Error = MMSYSERR_NOERROR;
              if not Result then ErrorStr := 'MixerGetControlDetails() Failed!';
            end else ErrorStr := 'MixerGetLineControls() Failed!';
          end else ErrorStr := 'MixerGetLineInfo() Failed!';
        finally
          MixerClose(hMix);
        end;
      end else ErrorStr := 'MixerOpen() Failed!';
    end;function SetMasterVolume(const Value: Byte; var ErrorStr: String): Boolean;
    var
      Error: LongInt;
      hMix: HMIXER;
      Ml: MIXERLINE;
      Mc: MIXERCONTROL;
      Mlc: MIXERLINECONTROLS;
      Mcd: TMIXERCONTROLDETAILS;
      Mcdu: MIXERCONTROLDETAILS_UNSIGNED;
    begin
      Error := mixerOpen(@hMix, 0, 0, 0, 0);
      Result := Error = MMSYSERR_NOERROR;
      if Result then begin
        try
          ml.cbStruct        := sizeof(MIXERLINE);
          ml.dwComponentType := MIXERLINE_COMPONENTTYPE_DST_SPEAKERS;
         // get the speaker line of the mixer device
          Error := mixerGetLineInfo(hMix, @ml, MIXER_GETLINEINFOF_COMPONENTTYPE);
          Result := Error = MMSYSERR_NOERROR;
          if Result then begin
            mlc.cbStruct      := sizeof(MIXERLINECONTROLS);
            mlc.dwLineID      := ml.dwLineID;
            mlc.dwControlType := MIXERCONTROL_CONTROLTYPE_VOLUME;
            mlc.cControls     := 1;
            mlc.pamxctrl      := @mc;
            mlc.cbmxctrl      := sizeof(MIXERCONTROL);
            // get the volume controls associated with the speaker line
            Error := mixerGetLineControls(hMix, @mlc, MIXER_GETLINECONTROLSF_ONEBYTYPE);
            REsult := MMSYSERR_NOERROR = Error;
            if Result then begin
              mcdu.dwValue    := Round(Mc.Bounds.dwMaximum * (Value / 100));
              mcd.cbStruct    := sizeof(TMIXERCONTROLDETAILS);
              mcd.hwndOwner   := 0;
              mcd.dwControlID := mc.dwControlID;
              mcd.paDetails   := @mcdu;
              mcd.cbDetails   := sizeof(MIXERCONTROLDETAILS_UNSIGNED);
              mcd.cChannels   := 1;
              // set the volume
              Error := mixerSetControlDetails(hMix, @mcd, MIXER_SETCONTROLDETAILSF_VALUE);
              Result := Error = MMSYSERR_NOERROR;
              if not Result then ErrorStr := 'MixerSetControlDetails() Failed!';
            end else ErrorStr := 'MixerGetLineControls() Failed!';
          end else ErrorStr := 'MixerGetLineInfo() Failed!';
        finally
          MixerClose(hMix);
        end;
      end else ErrorStr := 'MixerOpen() Failed!';
    end;