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
jballi
  • Members
  • 1029 posts
  • Last active:
  • Joined: 01 Oct 2005

The function for the lenght of a media file gives me a longer time that the actual file has

The "problem" appears to be limited to variable bitrate (VBR) MP3s. I noted this error a very long time ago on another thread.

when I play a file sometimes it plays 2 or 3 times to fill the time that the function for length gives. Is it a function problem, a dll problem or an encoding one?


Although the MCI_Length function returns a value that is larger that the actual length, it does not (read: should not) affect playback.

If you haven't done it already, try to play your media using the sample scripts that are provided with this project. Many of the scripts will play most media types, including VBR MP3s.

Good luck!

-HTT-CantTouchMe
  • Members
  • 11 posts
  • Last active: Oct 02 2011 08:43 AM
  • Joined: 25 Jun 2010
Hey, is it possible to make sound loop?
If so how?
Ain't no rest for the wicked. Money don't grow on trees.

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

Hey, is it possible to make sound loop?
If so how?

Here's one way to do it. For the MCI_Play function, include from and to flags in the p_Flags parameter and specify a callback function name in the p_Callback parameter.

In the following example, the script will loop any media file from 25000 to 35000 (from 25 seconds to 35 seconds) until the Stop button is pressed. To loop the entire media file just get rid of the from and to flags.

#NoEnv
#SingleInstance Force

gui Margin,0,0
gui Add,Button,w70 h35,Open
gui Add,Button,x+0 wp hp,Play
gui Add,Button,x+0 wp hp,Pause
gui Add,Button,x+0 wp hp,Stop
gui Show

gosub ButtonOpen
return


GUIEscape:
GUIClose:
if Open
    MCI_Close(hMedia)

ExitApp


ButtonOpen:
if Open
    MCI_Close(hMedia)

if not DefaultFolder
    DefaultFolder:=A_MyDocuments

gui +OwnDialogs
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
gosub ButtonPlay
return


ButtonPlay:
if Open
    {
    Status:=MCI_Status(hMedia)
    if (Status="Stopped")
        gosub Play
     else
        if (Status="Paused")
            MCI_Resume(hMedia)
    }

return

Play:
MCI_Play(hMedia,"from 25000 to 35000","NotifyEndOfPlay")
return


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

return


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

return


NotifyEndOfPlay(Flag)
    {
    Global
;;;;;    Critical
    if (Flag=1)  ;-- 1=play ended normally
        gosub Play
    }


#include MCI.ahk
The only problem that I ran into is that callback routine gets lost (returns a to-be-determined value) if the loop period is too short (less than 3 or 4 seconds?). Reliability can be improved by adding a Critical statement to the callback function.

I hope this helps.

-HTT-CantTouchMe
  • Members
  • 11 posts
  • Last active: Oct 02 2011 08:43 AM
  • Joined: 25 Jun 2010
Thanks it works but

In the following example, the script will loop any media file from 25000 to 35000 (from 25 seconds to 35 seconds) until the Stop button is pressed. To loop the entire media file just get rid of the from and to flags.

I took the "from 25000 to 35000" part off and it won't loop. Even tested it on the one example you gave here.
Well the workaround I did was added "from 0 to 27000" which is the start until the end of the music.

Cheers
Ain't no rest for the wicked. Money don't grow on trees.

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

I took the "from 25000 to 35000" part off and it won't loop. Even tested it on the one example you gave here.
Well the workaround I did was added "from 0 to 27000" which is the start until the end of the music.

I'm glad you figured it out. The "from 25000 to 35000" flags were just an example. It makes sense that it didn't work if the track ended at 27000. As I mentioned earlier, you don't need the from and to flags if you want to play the entire media file.

jballi
  • Members
  • 1029 posts
  • Last active:
  • Joined: 01 Oct 2005
v1.1
Minor changes to support Unicode. See the bottom of the first post for more information.

nogbadthebad
  • Members
  • 20 posts
  • Last active: Mar 18 2014 07:25 AM
  • Joined: 03 Nov 2009
I'm trying to record from my microphone.
However when I run the test 'microphone sound level' on some of the examples, it always shows that I have no sound coming from my microphone.
My sound card is broken, so I am using a usb headset/microphone. Therefore 'line in' is not my default mic. I suspect this has something to do with the problem.
thanks

Trubbleguy
  • Members
  • 122 posts
  • Last active: Jan 15 2017 10:50 AM
  • Joined: 20 Jan 2007
I know this is on my Wishlist, but to complete the package, but, is it possible to add/Modify tags with your mci library, it would totally complete it. and i'm sure it would totally rock the socks of most ahk media nutz.

windows has built in tag reading, but reading up on MSDN about it, is like reading Japanese. your skill set i'm sure would easily deal with the problems involved. all the tag reading librarys i have tried work in one way but not another, or lock up my pc when i ask to loop a folder or bsod me. Could you please take a look????

Uberi
  • Moderators
  • 1119 posts
  • Last active: May 02 2015 06:05 PM
  • Joined: 23 Aug 2010
If by that you mean editing or reading ID3 tags for MP3 files, that is not something MCI implements or handles.

For that, see here, or here.

jballi
  • Members
  • 1029 posts
  • Last active:
  • Joined: 01 Oct 2005
Debugging someone's hardware problem from afar is very difficult but there are a couple of things to try.

I'm trying to record from my microphone. However when I run the test 'microphone sound level' on some of the examples, it always shows that I have no sound coming from my microphone.

As the name implies, the MCI Library uses the built-in Media Control Interface (MCI). You have probably already tried it but just in case you haven't...

1) Try recording using the default Microsoft Sound Recorder program (the program uses MCI). If the microphone doesn't work on that program, it probably won't work on the MCI Library examples.

I bought a new headphone/microphone combo (not USB) a while back and I had trouble using the new device with MCI so just in case you are experiencing the same problem...

2) By default, some of the newer microphone sensitivity levels may be at a fraction of your old/other microphone. For most applications, Microsoft correctly boosts the level to compensate or provides a utility to do it. Microsoft used to provide a system-wide option to boost the microphone but I haven't been able to find it in years. I think that it might have been written out in one of the Microsoft upgrades.

To see if the microphone sensitivity level is the problem, hold the microphone directly on your mouth and speak very loudly. If you see any movement on the sound levels, the sensitivity level is probably the problem. No, I don't know how to fix this problem.

My sound card is broken, so I am using a usb headset/microphone. Therefore 'line in' is not my default mic. I suspect this has something to do with the problem.

Just to clarify, "Line In" has never been the default source for a microphone. "Line In" has always been used as an alternate input source. "Microphone" is the default source for a microphone.

I'm sorry I couldn't be more helpful. Good luck!

Diamond
  • Members
  • 198 posts
  • Last active: Dec 02 2015 11:18 AM
  • Joined: 31 Mar 2006
Is it possible to set the bits per sample, number of channels, and the sampling rate for the record function? And if so, could someone give me an example of how this would be done? By default it uses 8 bits mono 11025 Hz which is extremely low quality for what I have in mind.
Thanks for the work you've put into these functions jballi.

Diamond
  • Members
  • 198 posts
  • Last active: Dec 02 2015 11:18 AM
  • Joined: 31 Mar 2006
Never mind I think I figured it out. I guess this could be modified to send these parameters when the function is initially called.

MCI_Record(p_lpszDeviceID,p_Flags="")
{
MCI_SendString("set " . p_lpszDeviceID . A_Space . "bitspersample 16 channels 2 samplespersec 44100")
Return MCI_SendString("record " . p_lpszDeviceID . A_Space . p_Flags)
}


Sorry about not using proper code tags, but I am visually impaired and certain aspects of the editor are not very accessible.