MCIWndCreate and MCI Windows

Just starting out? Need help? Post your questions and find answers here.
michaeled314
Enthusiast
Enthusiast
Posts: 340
Joined: Tue Apr 24, 2007 11:14 pm

MCIWndCreate and MCI Windows

Post by michaeled314 »

I am trying to create an MCI window to control playback of a sound file, however I am getting an read address error at memory address 0

Code: Select all

#Register_Window = 34230

If OpenLibrary(0,"msvfw32.dll")
  *MCIWndCreate = GetFunction(0,"MCIWndCreate")
EndIf

Structure MCIOpenParams
  FileName.s
  Alias.s
EndStructure

Structure MCIPlayParams
  Alias.s
  FromPos.l
  ToPos.l
EndStructure

Global play.MCIPlayParams
Global MCIMsg.l = 0

Procedure.s MCIGetError(Error.l)
  err.s = Space(255)
  mciGetErrorString_(Error,@err,255)
  ProcedureReturn err
EndProcedure

Procedure.l MCIOpen(Alias.s, FileName.s)
 Error = mciSendString_("open "+Chr(34)+FileName+Chr(34)+" type mpegvideo alias "+Alias,0,0,0)
 
 ProcedureReturn Error
EndProcedure

Procedure.l MCIPause(Alias.s)
  Error = mciSendString_("pause "+Alias,0,0,0)
  
  ProcedureReturn Error
EndProcedure

Procedure.l MCIPlay(Alias.s, FromPos.l, ToPos.l)
 mciString.s = "play "+Alias
 If FromPos
   mciString + " from "+Str(FromPos)
   If ToPos
     mciString + " to "+Str(ToPos)
   EndIf
 EndIf

 Error = mciSendString_(mciString,0,0,0)
 ProcedureReturn Error
EndProcedure

Procedure MCICallbackThread(*p.MCIOpenParams)
  
  Repeat
    Select MCIMsg
      Case #MCI_PLAY
        Error = MCIPlay(play\Alias,play\FromPos,play\ToPos)
        MessageRequester("Error","Error: "+MCIGetError(Error),#PB_MessageRequester_Ok)
        MCIMsg = 0
      Case #Register_Window
        q.s = *p\FileName
        Debug CallFunctionFast(*MCIWndCreate,WindowID(0),GetModuleHandle_(0),0,@q)
        MCIMsg = 0
    EndSelect
  Until MCIMsg = #MCI_CLOSE
EndProcedure

Procedure WndProc(hwnd,umsg,wparam,lparam)
  Result = #PB_ProcessPureBasicEvents
  
  ProcedureReturn Result
EndProcedure

If OpenWindow(0,0,0,400,400,"",#PB_Window_ScreenCentered)
  
  *p.MCIOpenParams = AllocateMemory(SizeOf(MCIOpenParams))
  *p\Alias = "1"
  *p\FileName = OpenFileRequester("Choose a File","","*.*",0)
  
  SetWindowCallback(@WndProc())
  CreateThread(@MCICallbackThread(),*p)
  
  MCIMsg = #Register_Window
  event.l
  Repeat
  Until event = #PB_Event_CloseWindow
EndIf
michaeled314
Enthusiast
Enthusiast
Posts: 340
Joined: Tue Apr 24, 2007 11:14 pm

Re: MCIWndCreate and MCI Windows

Post by michaeled314 »

Is it possible to open an MCI Window
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: MCIWndCreate and MCI Windows

Post by Shardik »

michaeled314 wrote:Is it possible to open an MCI Window
Yes, it is. But you have to change CallFunctionFast() to CallCFunctionFast().
Try this small example which works in Windows XP SP3 and Windows 7 x86:

Code: Select all

SoundFilename.S = "C:\Windows\Media\Chimes.Wav" ; <-- Change this path if Windows isn't installed in C:\Windows

If OpenWindow(0, 0, 0, 300, 28, "", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  If OpenLibrary(0,"msvfw32.dll")
    MCIWndCreate = GetFunction(0,"MCIWndCreate")

    If MCIWndCreate
      CallCFunctionFast(MCIWndCreate, WindowID(0), GetModuleHandle_(0), 0, @SoundFilename)
    EndIf

    Repeat
    Until WaitWindowEvent() = #PB_Event_CloseWindow
  EndIf
EndIf
michaeled314
Enthusiast
Enthusiast
Posts: 340
Joined: Tue Apr 24, 2007 11:14 pm

Re: MCIWndCreate and MCI Windows

Post by michaeled314 »

Thanks but now I will need to make the trackbar jump to right where you click... This means that I will need the handle of the MCI Trackbar

Incorporating this code

Code: Select all

Procedure Callback(hWnd,uMsg,wParam,lParam)
  Select uMsg
    Case #WM_MOUSEACTIVATE
      GetCursorPos_(p.POINT)
      ScreenToClient_(WindowID(0),p)
      State = ((p\x - 30)*10000)/180
      SendMessage_(GadgetID(0),#TBM_SETPOS,1,State)
  EndSelect
  
  Result = #PB_ProcessPureBasicEvents
  ProcedureReturn Result
EndProcedure

If OpenWindow(0,0,0,500,500,"NM",#PB_Window_ScreenCentered|#PB_Window_MinimizeGadget)
  TrackBarGadget(0,20,20,200,20,0,10000)
  SetWindowLong_(GadgetID(0), #GWL_STYLE, #TBS_AUTOTICKS | #TBS_BOTTOM | #TBS_ENABLESELRANGE | #TBS_HORZ | #TBS_RIGHT | #WS_CHILD | #WS_OVERLAPPED | #WS_VISIBLE)
  SetWindowCallback(@Callback())
  
  Repeat
    Event = WindowEvent()
  Until Event = #PB_Event_CloseWindow
EndIf
michaeled314
Enthusiast
Enthusiast
Posts: 340
Joined: Tue Apr 24, 2007 11:14 pm

Re: MCIWndCreate and MCI Windows

Post by michaeled314 »

SOLVED
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: MCIWndCreate and MCI Windows

Post by Shardik »

michaeled314 wrote:SOLVED
It's very nice that you were able to solve your problem but it would
have been much nicer if you would have posted your solution so that
others with the same problem might benefit from your work... :wink:
User avatar
electrochrisso
Addict
Addict
Posts: 989
Joined: Mon May 14, 2007 2:13 am
Location: Darling River

Re: MCIWndCreate and MCI Windows

Post by electrochrisso »

I was going to mention the same thing.
PureBasic! Purely the best 8)
Post Reply