AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

SoundPlay locks a file

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Bug Reports
View previous topic :: View next topic  
Author Message
Tekl



Joined: 24 Sep 2004
Posts: 813
Location: Germany

PostPosted: Wed Jul 19, 2006 3:47 pm    Post subject: SoundPlay locks a file Reply with quote

Hi,

If I once played a wave-file with SoundPlay, it will be always in use, even if the sound stopped playing. If I exit the script the file will be freed again. I don't use the wait-option.
_________________
Tekl
Back to top
View user's profile Send private message Visit poster's website
PhiLho



Joined: 27 Dec 2005
Posts: 6721
Location: France (near Paris)

PostPosted: Wed Jul 19, 2006 4:22 pm    Post subject: Reply with quote

What do you mean exactly by "in use"? How to you see that?
_________________
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")
Back to top
View user's profile Send private message Visit poster's website
WarrenFaith



Joined: 11 Jul 2006
Posts: 11

PostPosted: Wed Jul 19, 2006 4:28 pm    Post subject: Reply with quote

I think he cant delete it because windows says its in use.
Back to top
View user's profile Send private message
Titan



Joined: 11 Aug 2004
Posts: 5009
Location: imaginationland

PostPosted: Wed Jul 19, 2006 4:32 pm    Post subject: Reply with quote

I can confirm this. The wait option allows you to delete the file after it's finished playing though.
_________________

RegExReplace("irc.freenode.net/autohotkey", "^(?=(.(?=[\0-r\[]*((?<=\.).))))(?:[c-\x73]{2,8}(\S))+((2)|\b[^\2-]){2}\D++$", "$u3$1$3$4$2")
Back to top
View user's profile Send private message Visit poster's website
Tekl



Joined: 24 Sep 2004
Posts: 813
Location: Germany

PostPosted: Thu Jul 20, 2006 11:28 am    Post subject: Reply with quote

Hi,

Warren is right. The wav-file has been played long time ago and the file is still locked. I can't use the wait-option.

I now have found a workaround. Before deleting/overwriting I use PlaySound, nonexistantfile.wav, wait
_________________
Tekl
Back to top
View user's profile Send private message Visit poster's website
corrupt



Joined: 29 Dec 2004
Posts: 2384

PostPosted: Sat Jul 22, 2006 4:36 am    Post subject: Reply with quote

I'm guessing (just a guess - I didn't check the source) that a close command wasn't sent after the song finished playing so the handle to the file remains valid until an attempt to play another song is made.

Another workaround that can provide a lot more flexibility if necessary could be to use DllCall and MCI commands instead to play sound files. Here's a script I started a while ago that gives a bit of info. The script and functions aren't complete - but it has basic functionality and will play a file.
Code:
; *****************************
; miniM3P
; *****************************
; Author: corrupt
; Version: 0.01b
; Last Modified: April 22, 2006
; *****************************

OnExit, BBye
#SingleInstance, Force

; ********** GUI ***********

Gui, Add, Button, x6 y30 w40 h20 gPlayPause vPlayPause, Play
Gui, Add, Button, x46 y30 w40 h20 gStop vStop, Stop
Gui, Add, Button, x448 y36 w16 h16 gOpenSong vOpenSong, ...
Gui, Show, h65 w476, miniM3P
WinHWND := WinExist("A")
If 1<>
{
  fopen = %1%
  sOpen(fopen)
}
Else
  sBrowse()
Return

; ********** butons ***********

PlayPause:
If (SongOpen)
  sPlay()
Return

Stop:
If (SongOpen)
  sStop()
Return

OpenSong:
sBrowse()
Return

GuiClose:
ExitApp

BBye:
If (SongOpen)
  sClose()
ExitApp

; ********** functions ***********
; ---------------------------
; Open a file
; ---------------------------
sOpen(sPath, sAlias="")
{
Global WinHWND
; **** get the full path (8.3 format) to the file if the file exists
IfExist, %sPath%
  Loop, %sPath% {
    sTName = %A_LoopFileName%
    StringTrimRight, sTName, sTName, % (StrLen(A_LoopFileExt) + 1)
    sPath = %A_LoopFileShortPath%
  }
; **** Assign a default alias if not specified
If sAlias=
  sAlias = mp3song
; **** open the file for playback
sStr = OPEN %sPath% TYPE MPEGVIDEO ALIAS %sAlias%
IF (DllCall("winmm.dll\mciExecute", "Str", sStr)) {
  WinSetTitle, ahk_id %WinHWND%,, miniM3P - %sTName%
  sPlay()
  Return, True
}
MsgBox, Error: Error opening file
Return
}

; ---------------------------
; Play / Pause
; ---------------------------
sPlay(sAlias="", sControl="")
{
  ; **** set defaults if not specified
  If sAlias=
    sAlias = mp3song
  If sControl=
    sControl = PlayPause

  ; **** Get playback status and play or pause depending on status
  PP := sPlayStatus()

  If PP = playing
  {
    sStr = PAUSE %sAlias%
    GuiControl, Text, %sControl%, Play
  }
  Else If PP = paused
  {
    sStr = RESUME %sAlias%
    GuiControl, Text, %sControl%, Pause
  }
  Else If PP = stopped
  {
    sStr = PLAY %sAlias%
    GuiControl, Text, %sControl%, Pause
  }
  Else {
    MsgBox, Error: `nUnexpected playback state: %PP%
    Return
  }

  DllCall("winmm.dll\mciExecute", "Str", sStr)
  Return
}

; ---------------------------
; Stop
; ---------------------------
sStop(sAlias="", sControl="")
{
  ; **** set defaults if not specified
  If sAlias=
    sAlias = mp3song
  If sControl=
    sControl = PlayPause
  sStr = STOP %sAlias%
  DllCall("winmm.dll\mciExecute", "Str", sStr)
  sStr = SEEK %sAlias% to 0
  DllCall("winmm.dll\mciExecute", "Str", sStr)
  GuiControl, Text, %sControl%, Play
  Return
}

; ---------------------------
; Close the file
; ---------------------------
sClose(sAlias="", sControl="")
{
  ; **** set defaults if not specified
  If sAlias=
    sAlias = mp3song
  If sControl=
    sControl = PlayPause
  sStr = CLOSE %sAlias%
  DllCall("winmm.dll\mciExecute", "Str", sStr)
  GuiControl, Text, %sControl%, Play
  Return
}

; ---------------------------
; playback status
; ---------------------------
sPlayStatus(sAlias="")
{
  ; **** set defaults if not specified
  If sAlias=
    sAlias = mp3song
  VarSetCapacity(stat1, 32, 32)
  sStr = STATUS %sAlias% MODE
  DllCall("winmm.dll\mciSendStringA", "UInt", &sStr, "UInt", &stat1, "Int", 32, "Int", 0)
  Return, stat1
}


; **** Custom ****
; ---------------------------
; Browse to a file, attempt to open
; ---------------------------
sBrowse()
{
Global SongOpen, SongPath
FileSelectFile, SongPath, 3,, Open MP3 file, Audio files(*.mp3; *.wav; *.*)
If SongPath
{
  If (SongOpen) {
    sClose()
    SongOpen = 0
  }
  IF (sOpen(SongPath))
    SongOpen = 1
}
Return
}
Back to top
View user's profile Send private message Visit poster's website
Chris
Site Admin


Joined: 02 Mar 2004
Posts: 10465

PostPosted: Sun Jul 23, 2006 2:35 am    Post subject: Reply with quote

Since I don't see a simple way to solve this other than using SetTimer internally, I've documented it as a known limitation

The alternatives and work-arounds above seem quite useful, so thanks for mentioning them.
Back to top
View user's profile Send private message Send e-mail
padarrju



Joined: 23 Jan 2006
Posts: 37
Location: Vadodara - Gujarat (INDIA)

PostPosted: Sat Jul 07, 2007 7:04 am    Post subject: Reply with quote

corrupt wrote:
I'm guessing (just a guess - I didn't check the source) that a close command wasn't sent after the song finished playing so the handle to the file remains valid until an attempt to play another song is made.

Another workaround that can provide a lot more flexibility if necessary could be to use DllCall and MCI commands instead to play sound files. Here's a script I started a while ago that gives a bit of info. The script and functions aren't complete - but it has basic functionality and will play a file.
Code:
; *****************************
; miniM3P
; *****************************
; Author: corrupt
; Version: 0.01b
; Last Modified: April 22, 2006
; *****************************

OnExit, BBye
#SingleInstance, Force

; ********** GUI ***********

Gui, Add, Button, x6 y30 w40 h20 gPlayPause vPlayPause, Play
Gui, Add, Button, x46 y30 w40 h20 gStop vStop, Stop
Gui, Add, Button, x448 y36 w16 h16 gOpenSong vOpenSong, ...
Gui, Show, h65 w476, miniM3P
WinHWND := WinExist("A")
If 1<>
{
  fopen = %1%
  sOpen(fopen)
}
Else
  sBrowse()
Return

; ********** butons ***********

PlayPause:
If (SongOpen)
  sPlay()
Return

Stop:
If (SongOpen)
  sStop()
Return

OpenSong:
sBrowse()
Return

GuiClose:
ExitApp

BBye:
If (SongOpen)
  sClose()
ExitApp

; ********** functions ***********
; ---------------------------
; Open a file
; ---------------------------
sOpen(sPath, sAlias="")
{
Global WinHWND
; **** get the full path (8.3 format) to the file if the file exists
IfExist, %sPath%
  Loop, %sPath% {
    sTName = %A_LoopFileName%
    StringTrimRight, sTName, sTName, % (StrLen(A_LoopFileExt) + 1)
    sPath = %A_LoopFileShortPath%
  }
; **** Assign a default alias if not specified
If sAlias=
  sAlias = mp3song
; **** open the file for playback
sStr = OPEN %sPath% TYPE MPEGVIDEO ALIAS %sAlias%
IF (DllCall("winmm.dll\mciExecute", "Str", sStr)) {
  WinSetTitle, ahk_id %WinHWND%,, miniM3P - %sTName%
  sPlay()
  Return, True
}
MsgBox, Error: Error opening file
Return
}

; ---------------------------
; Play / Pause
; ---------------------------
sPlay(sAlias="", sControl="")
{
  ; **** set defaults if not specified
  If sAlias=
    sAlias = mp3song
  If sControl=
    sControl = PlayPause

  ; **** Get playback status and play or pause depending on status
  PP := sPlayStatus()

  If PP = playing
  {
    sStr = PAUSE %sAlias%
    GuiControl, Text, %sControl%, Play
  }
  Else If PP = paused
  {
    sStr = RESUME %sAlias%
    GuiControl, Text, %sControl%, Pause
  }
  Else If PP = stopped
  {
    sStr = PLAY %sAlias%
    GuiControl, Text, %sControl%, Pause
  }
  Else {
    MsgBox, Error: `nUnexpected playback state: %PP%
    Return
  }

  DllCall("winmm.dll\mciExecute", "Str", sStr)
  Return
}

; ---------------------------
; Stop
; ---------------------------
sStop(sAlias="", sControl="")
{
  ; **** set defaults if not specified
  If sAlias=
    sAlias = mp3song
  If sControl=
    sControl = PlayPause
  sStr = STOP %sAlias%
  DllCall("winmm.dll\mciExecute", "Str", sStr)
  sStr = SEEK %sAlias% to 0
  DllCall("winmm.dll\mciExecute", "Str", sStr)
  GuiControl, Text, %sControl%, Play
  Return
}

; ---------------------------
; Close the file
; ---------------------------
sClose(sAlias="", sControl="")
{
  ; **** set defaults if not specified
  If sAlias=
    sAlias = mp3song
  If sControl=
    sControl = PlayPause
  sStr = CLOSE %sAlias%
  DllCall("winmm.dll\mciExecute", "Str", sStr)
  GuiControl, Text, %sControl%, Play
  Return
}

; ---------------------------
; playback status
; ---------------------------
sPlayStatus(sAlias="")
{
  ; **** set defaults if not specified
  If sAlias=
    sAlias = mp3song
  VarSetCapacity(stat1, 32, 32)
  sStr = STATUS %sAlias% MODE
  DllCall("winmm.dll\mciSendStringA", "UInt", &sStr, "UInt", &stat1, "Int", 32, "Int", 0)
  Return, stat1
}


; **** Custom ****
; ---------------------------
; Browse to a file, attempt to open
; ---------------------------
sBrowse()
{
Global SongOpen, SongPath
FileSelectFile, SongPath, 3,, Open MP3 file, Audio files(*.mp3; *.wav; *.*)
If SongPath
{
  If (SongOpen) {
    sClose()
    SongOpen = 0
  }
  IF (sOpen(SongPath))
    SongOpen = 1
}
Return
}


Very nice script. I have developed one login script which displays birthday daily based on current date & then plays a prayer (song) whenever I start my pc. I wanted to use some keys to increase / decrease/mute the volume. I did it but somehow I was not able to Pause the prayer inbetween. At last I found your script.
Now my job is over !

Thanks a lot lot lot ....

Regards,

Raju C Padaria
Back to top
View user's profile Send private message
corrupt



Joined: 29 Dec 2004
Posts: 2384

PostPosted: Sat Jul 07, 2007 2:23 pm    Post subject: Reply with quote

Thanks. I'm glad to hear that you found it useful Smile .
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Bug Reports All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group