PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
VS 2017 [RESOLVED] Play Sound Problem Via MCI API-VBForums
Results 1 to 12 of 12

Thread: [RESOLVED] Play Sound Problem Via MCI API

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2018
    Posts
    122

    Resolved [RESOLVED] Play Sound Problem Via MCI API

    I need some help with playing a sound file via the MCI Windows API. I added this line to my VB .net "Module"...

    Code:
    Public Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Integer, ByVal hwndCallback As Integer) As Integer
    and I am attempting to call it via a Public Sub called PlaySoundFile.. like this:

    Code:
    Public Sub PlaySoundFile(SoundFile As String)
    Call mciSendString("open " & SoundFile, CStr(0), 0, 0)
    Call mciSendString("play " & SoundFile, CStr(0), 0, 0)
    End Sub
    But it is not working.. what am I doing wrong? I am trying to use it in a command button like this:

    Code:
    Call PlaySoundFile("C:\Users\PathToSoundFile")

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,759

    Re: Play Sound Problem Via MCI API

    You ought to have your file path wrapped in double-quotes in case it contains spaces. I've never used mciSendString myself but a quick look around seems to suggest that the 'open' command at least also requires some flags after the file path. Are you sure that your command is valid? Should you maybe be actually using that return string to find out what happened?

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2018
    Posts
    122

    Re: Play Sound Problem Via MCI API

    Thanks well it worked after I wrapped in triple quotes I mean like this:

    Code:
    Call PlaySoundFile("""C:\Users\PathToSoundFile""")
    but even still, I am wondering what you said there about the return string. How can I use/implement this, just in case of other errors?

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Mar 2018
    Posts
    122

    Re: Play Sound Problem Via MCI API

    wondering about stoping playback though I have it and it works but when I stop and press play again it just starts where it left off which is pretty cool but how can I stop it so that when I press play again it starts from the beginning? Also why when I press play the button again it does not start playing the file from the start again? How can I make it do that?

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,759

    Re: Play Sound Problem Via MCI API

    Quote Originally Posted by DreamWarrior77 View Post
    Thanks well it worked after I wrapped in triple quotes I mean like this:

    Code:
    Call PlaySoundFile("""C:\Users\PathToSoundFile""")
    You may well already be aware of this but you're not really wrapping it in triple quotes. The outer double-quote at each end is denoting to VB that there's a literal String in between. The other two pairs are one literal double-quote each, as VB escapes a literal double-quote with another double quote. The C# equivalent would be this:
    csharp Code:
    1. PlaySoundFile(""C:\\Users\\PathToSoundFile"");
    because C# uses a backslash as an escape character. If you were to use a verbatim string literal in C# then the backslash is no longer recognised as an escape character, so it too would require escaping a literal double-quote with another double-quote:
    csharp Code:
    1. PlaySoundFile(@"""C:\Users\PathToSoundFile""");
    Quote Originally Posted by DreamWarrior77 View Post
    but even still, I am wondering what you said there about the return string. How can I use/implement this, just in case of other errors?
    I'm not 100% sure but I think the way to go would be to change your PInvoke signature and make that parameter type StringBuilder instead of type String. I think that you would then have to create an instance and pass it to that parameter. After execution, that StringBuilder should contain any text returned by the function.

    By the way, mentioning the C# equivalent of that VB code reminded me that I was going say earlier that the Call keyword is of no value here. Unlike VB6, there's never a situation in VB.NET where you NEED to use Call and the only situations where I'm aware that it adds any value is where you want invoke a constructor and call a method on the object without assigning anything to a variable. In that case, you can't start a line of code with the New keyword so Call means that you don;t have to introduce an unnecessary variable, e.g.
    vb.net Code:
    1. Call New SomeType().SomeMethod()
    Last edited by jmcilhinney; May 21st, 2019 at 12:34 AM.

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,759

    Re: Play Sound Problem Via MCI API

    Quote Originally Posted by DreamWarrior77 View Post
    wondering about stoping playback though I have it and it works but when I stop and press play again it just starts where it left off which is pretty cool but how can I stop it so that when I press play again it starts from the beginning? Also why when I press play the button again it does not start playing the file from the start again? How can I make it do that?
    Are you using "stop" and then "close" to stop play? I'm guessing but I would expect that to reset any media position.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Mar 2018
    Posts
    122

    Re: Play Sound Problem Via MCI API

    Fools mistake, I forgot to close after stop. Thanks ok the play button I just added "close" to the sub and it works as intended. Thanks
    One more for you, how can I add a loop on/off to my sub? I have seen it somewhere else earlier but was not sure how to do it.
    Do I use boolean type for this? I searched but do not see a "loop" command for MCI..
    if not in the sub then just in general to add to my button.
    Last edited by DreamWarrior77; May 21st, 2019 at 01:02 AM.

  8. #8
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,759

    Re: Play Sound Problem Via MCI API

    I'd suggest that you use the Thread Tools menu to mark this thread Resolved, given that the issue you created the thread for has indeed been resolved. If you have a new question, you should create a new thread with a title that describes the new issue specifically and provide all the information relevant to that new issue and only that information. If we keep each thread to a single topic and each topic to a single thread then the forum will be easier for everyone.

  9. #9
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    5,546
    Slow as hell.

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Mar 2018
    Posts
    122

    Re: [RESOLVED] Play Sound Problem Via MCI API

    Thanks jmcilhinney, I marked it as resolved. Will start a new post with the other issue. Thanks for the links sapator..

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Mar 2018
    Posts
    122

    Re: Play Sound Problem Via MCI API

    So there is no real benefit of me using call at all I mean for the most part? I am using it in subs and then call when using the sub in a button for example, or where ever else. I am coming from vb6 so I am just use to using it like that. Should I remove it from my code?

  12. #12
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    5,338

    Re: Play Sound Problem Via MCI API

    Quote Originally Posted by DreamWarrior77 View Post
    ...I am coming from vb6 so I am just use to using it like that. Should I remove it from my code?
    Yes.
    You shouldn't have used it in VB6 either.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width