Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

[Library] MCI v1.1 - Play/Control media files


  • Please log in to reply
71 replies to this topic
vlcek
  • Members
  • 341 posts
  • Last active: Apr 03 2014 01:27 PM
  • Joined: 19 Feb 2007
Hi all.
Can you write some setvolume example for %selectedfile%?
I want use this in my player at:
<!-- m -->svn://autohotkey.net/vlcekp1/myrepos/player/<!-- m -->

jballi
  • Members
  • 1028 posts
  • Last active:
  • Joined: 01 Oct 2005

Can you write some setvolume example for %selectedfile%?

The code for calling the MCI_SetVolume function is fairly straightforward:

MCI_SetVolume(OpenMCIDevice,1000) ;-- Sets the volume to max for the device
MCI_SetVolume(OpenMCIDevice,500)  ;-- Sets the volume to midway point for the device
MCI_SetVolume(OpenMCIDevice,0)    ;-- Sets the volume to 0 (mute) for the device
Although the MCI protocol supports volume change, most MCI devices do not support the associated MCI "Set Volume" command. If your player only plays media that uses MCI devices that support MCI volume change, the MCI_SetVolume function is a good choice. However, if your player plays a mix of media types (I suspect it does), AutoHotkey's SoundSet command might make more sense.

Good luck!

vlcek
  • Members
  • 341 posts
  • Last active: Apr 03 2014 01:27 PM
  • Joined: 19 Feb 2007
Thanks for example, but I have problem with this.
MCI_SetVolume(hMedia,+10) ;--
Set volume for hMedia to +10.
The player stop, when I call this function.

jballi
  • Members
  • 1028 posts
  • Last active:
  • Joined: 01 Oct 2005

Thanks for example, but I have problem with this.
MCI_SetVolume(hMedia,+10) ;--
Set volume for hMedia to +10.
The player stop, when I call this function.

Just to make sure that there weren't any hidden bugs, I tried calling the function (with your parameters) on my PC and it worked without generating an error.

Here's something to try: Open a debugger and check the output right after the function is called. If there is an MCI error, the function will send a user-friendly message to the debugger that will describe the problem. For example, if you call the function without opening the MCI device first, you'll get the following message: Return code=261 - The driver cannot recognize the specified command.

Good luck!

fincs
  • Moderators
  • 1662 posts
  • Last active:
  • Joined: 05 May 2007
Hah, I missed this... :lol:
Well, first congratulations for making a library to replace
my old and deprecated Sound.ahk...
I hope you suceed with this proyect ;)
fincs - the autor of Sound.ahk and Media.ahk

vlcek
  • Members
  • 341 posts
  • Last active: Apr 03 2014 01:27 PM
  • Joined: 19 Feb 2007
My friend downloaded the player from the svn and created function for ini read and ini write.
I have new problem.
Play a folder.
I'm using:
$^f::
IfWinNotActive, %ScriptName%
  {
Send, {^f}
   Return
}
SoundPlay, Nonexistent.
FileSelectFolder SelectedFolder, , 3, (*.*)
If SelectedFolder =
return
Else
{
   Loop %SelectedFolder%\*.*,0,1
      Soundplay %A_LoopFileFullPath%, WAIT
return
}
Return
Can you help me with rewrite this to mci?
Mci.ahk is best library for work with auio.

Drugwash
  • Members
  • 1076 posts
  • Last active: Dec 22 2014 05:35 PM
  • Joined: 07 Sep 2008
I've had some problems with filenames while fiddling with the example MCI player.

Dunno why it happened, but it wouldn't play certain MP3 files with long names (possibly too long path exceeding Win9x limit). However, same files wouldn't play even when names/paths were shortened below limit. Then, playing other acceptable file and trying the previously shortened one again, it would play fine! :?

So I ended up using fincs' Sound library instead of MCI, which would play the original files (unshortened, unmodified) without problems. :oops:

I've enhanced a little bit the Music Player that uses Sound; will post the script in its thread (provided I find it :D ) - it contains a rudimentary playlist feature.

jballi
  • Members
  • 1028 posts
  • Last active:
  • Joined: 01 Oct 2005

I have new problem.
Play a folder.
I'm using:

;{Snip}
SoundPlay, Nonexistent.
FileSelectFolder SelectedFolder, , 3, (*.*)
If SelectedFolder =
return
Else
{
   Loop %SelectedFolder%\*.*,0,1
      Soundplay %A_LoopFileFullPath%, WAIT
return
}
Return
Can you help me with rewrite this to mci?

A full answer to this question would require significant detail. The short answer: Load all of the media files from the selected folder into a list, a "playlist" if you will, and then play each media file one at a time. You need to open each file with the MCI_Open function and play each file using the MCI_Play function using the "Callback" option to notify you when a media file has finished playing.

The following is a working but unfinished (I repeat, unfinished) example of how the Callback feature works:

#NoEnv
#SingleInstance Force

gui Margin,0,0
gui Add,Button,w60 h25  ,Open
gui Add,Button,x+0 wp hp,Play
gui Add,Button,x+0 wp hp,Pause
gui Add,Button,x+0 wp hp vStop,Stop
gui Add
   ,Slider
   ,x+0 w160 hp
        || +Disabled  ;-- Start off disabled
        || +ToolTip
        || vSlider
        || gSlider


Messages=Notify messages are displayed here.  New items added to the top.
gui Add
   ,Edit
   ,xm w400 h180
        || +ReadOnly
        || vMessages
   ,%Messages%

gui Show


gosub ButtonOpen
return



GUIEscape:
GUIClose:
if Open
    MCI_Close(hMedia)

ExitApp


ButtonOpen:
if Open
    {
    MCI_Close(hMedia)
    SetTimer UpdateSlider,off
    GUIControl,,Slider,0
    GUIControl Disable,Slider
    }

if not DefaultFolder
    DefaultFolder:=A_MyDocuments

FileSelectFile MediaFile,1,%DefaultFolder%,Choose a media file
if MediaFile=
   return

SplitPath MediaFile,,DefaultFolder

hMedia:=MCI_Open(MediaFile)
if not hMedia
    {
    MsgBox Error opening media file
    return
    }

Open:=true
MediaLength:=MCI_Length(hMedia)
    ;-- For this example, the length of the media file is collected once on
    ;   open.  The value is used by the Slider routines.

GUIControl Enable,Slider
SetTimer UpdateSlider,1000
gosub ButtonPlay
return



ButtonPlay:
if Open
    {
    Status:=MCI_Status(hMedia)
    if Status=Stopped
        {
        Messages:=A_Now . ": Notify set`n" . Messages
        GUIControl,,Messages,%Messages%

        MCI_Play(hMedia,"","NotifyEndOfPlay")  ;-- Notify is set here
        }
     else
        if Status=Paused
            MCI_Resume(hMedia)
    }

return



ButtonPause:
if Open
    {
    Status:=MCI_Status(hMedia)
    if Status=Playing
        MCI_Pause(hMedia)
     else
        if Status=Paused
            MCI_Resume(hMedia)

    ;-- Note: Pause and Resume do not interrupt Notify
    }

return



ButtonStop:
if Open
    {
    MCI_Stop(hMedia)
    MCI_Seek(hMedia,0)
    }

return



Slider:
if Open
    {
    Status:=MCI_Status(hMedia)
    if Status in Playing,Paused
        {
        Messages:=A_Now . ": Notify set/reset`n" . Messages
        GUIControl,,Messages,%Messages%

        MCI_Play(hMedia,"from " . floor(MediaLength*(Slider/100)),"NotifyEndOfPlay")
            ;-- MCI_Seek is not used to reposition the media in this example
            ;   because the function will cause a Notify interruption for most
            ;   devices.
            ;   
            ;   Using the "From" flag, MCI_Play will successfully reposition
            ;   the media for most MCI devices.  Calling MCI_Play (with
            ;   callback) while media is playing will abort the original Notify
            ;   condition (if any) but will create a new Notify condition.
    
        if Status=Paused
            MCI_Pause(hMedia)
        }

    ;-- Reset focus
    GUIControl Focus,Stop
    }

return



UpdateSlider:
if Open
    {
    ;-- Only update slider if object is NOT in focus
    GUIControlGet Control,FocusV
    if Control<>Slider
        GUIControl,,Slider,% (MCI_Position(hMedia)/MediaLength)*100
    }

return



NotifyEndOfPlay(Flag)
    {
    Global
    outputdebug Function: %A_ThisFunc%  flag=%flag%

    if Flag=1
        MCINotifyValue=MCI_NOTIFY_SUCCESSFUL (1)

    if Flag=2
        MCINotifyValue=MCI_NOTIFY_SUPERSEDED (2)

    if Flag=4
        MCINotifyValue=MCI_NOTIFY_ABORTED (4)

    if Flag=8
        MCINotifyValue=MCI_NOTIFY_FAILURE (8)

    Messages:=A_Now . ": " . MCINotifyValue . "`n" . Messages
    GUIControl,,Messages,%Messages%

    if Flag=1
        gosub ButtonStop

    return
    }


#include MCI.ahk
If you need additional help, PM me and I will try to help you the best I can.


I've had some problems with filenames while fiddling with the example MCI player.

Dunno why it happened, but it wouldn't play certain MP3 files with long names (possibly too long path exceeding Win9x limit).

This is the first I've heard of anyone having filename problems with the MCI library/Examples. Unfortunately I don't have access to a Win9x machine anymore (killed my last one about a month ago) so I can't test it in that environment. If you figure out a fix/workaround, let me know and I'll make a fix.

jballi
  • Members
  • 1028 posts
  • Last active:
  • Joined: 01 Oct 2005

Hah, I missed this... :lol:
Well, first congratulations for making a library to replace
my old and deprecated Sound.ahk...
I hope you suceed with this proyect ;)
fincs - the autor of Sound.ahk and Media.ahk

Thanks for your support and endorsement. I think most serious developers will eventually migrate towards the feature-rich BASS library but the MCI library is small and fairly simply so it is still quite useful. Thanks for introducing it to the forum. :D

vlcek
  • Members
  • 341 posts
  • Last active: Apr 03 2014 01:27 PM
  • Joined: 19 Feb 2007
Why player write can't open media file, when I use this?
FileSelectFolder SelectedFolder, , M, (*.*)
If SelectedFolder =
return
Else

{
   Loop %SelectedFolder%\*.mp3,0,1
hMedia:=MCI_Open(a_loopfilefullpath)
if not hMedia
    {
    MsgBox Error opening media file %A_LoopFileFullPath%

    return
    }
Open:=true
MediaLength:=MCI_Length(hMedia)
    ;-- For this example, the length of the media file is collected once on
    ;   open.  The value is used by the Slider routines.
gosub Play
}
return
Can you help me?

Drugwash
  • Members
  • 1076 posts
  • Last active: Dec 22 2014 05:35 PM
  • Joined: 07 Sep 2008

This is the first I've heard of anyone having filename problems with the MCI library/Examples. Unfortunately I don't have access to a Win9x machine anymore (killed my last one about a month ago) so I can't test it in that environment. If you figure out a fix/workaround, let me know and I'll make a fix.

If I ever get around testing under such conditions and manage to find a pattern (I tried at the time but failed), I'll notify you. But as you said, BASS is the new kid on the block so there's no emergency. I jumped into the Trout boat myself, lately.

jballi
  • Members
  • 1028 posts
  • Last active:
  • Joined: 01 Oct 2005

Why player write can't open media file, when I use this?

FileSelectFolder SelectedFolder, , M, (*.*)
If SelectedFolder =
return
Else
{
   Loop %SelectedFolder%\*.mp3,0,1
hMedia:=MCI_Open(a_loopfilefullpath)
if not hMedia
    {
    MsgBox Error opening media file %A_LoopFileFullPath%

    return
    }
Open:=true
MediaLength:=MCI_Length(hMedia)
    ;-- For this example, the length of the media file is collected once on
    ;   open.  The value is used by the Slider routines.
gosub Play
}
return
Can you help me?

Again, short answer. In your example, you are opening every single MP3 file in the selected folder and then checking/playing the last one. The code might be more appropriately written like this. Notice the additional set of brackets.
.
.
.
Else
    {
    Loop %SelectedFolder%\*.mp3,0,1
        {
        hMedia:=MCI_Open(a_loopfilefullpath)

        if not hMedia
            {
            MsgBox Error opening media file %A_LoopFileFullPath%
            return
            }
    
        Open:=true
        MediaLength:=MCI_Length(hMedia)
        gosub Play
        MCI_Close(hMedia)
        }
    }
But and however, this method won't work as expected because the MCI_Play function does not, by default, wait until the media file is finished playing before returning. Please read the entire first post in this thread and the documentation in the MCI library for additional information.

If you have any additional questions on this topic, please PM me. I will try to help you the best I can.

Drugwash
  • Members
  • 1076 posts
  • Last active: Dec 22 2014 05:35 PM
  • Joined: 07 Sep 2008
You know, I mentioned my enhanced player earlier in this thread - albeit it's using Sound, not MCI - for a reason. ;) I believe the playlist feature may be ported fairly easily, or at least I hope so.

vlcek
  • Members
  • 341 posts
  • Last active: Apr 03 2014 01:27 PM
  • Joined: 19 Feb 2007
When I set wait parameter, and start play, my computer is suspended and I must wai, when player finish play.

jballi
  • Members
  • 1028 posts
  • Last active:
  • Joined: 01 Oct 2005

When I set wait parameter, and start play, my computer is suspended and I must wai, when player finish play.

Yes, the "wait" flag is not recommended except for very short clips. I noted this in the documentation for the MCI_Play function.