Jump to content

Recommended Posts

Posted

So I'm working on a colab project with multiple languages (which is the reason for my odd coding habits), and I'm trying to make a little CUI mp3 player. Now, it seems no matter what I do, it'll do all except play the file. All of the error checks seem to work fine, like a non-existant file, non-mp3, no file, etc, but when I pass an actual MP3 file to it, it just fails to open. I've used a blank script with just a SoundPlay("file.mp3") and it has played them fine, so that's not the problem.

Here's the code.

#Include <Sound.au3>
#AutoIt3Wrapper_Change2CUI=y
Global $fLog = FileOpen("soundplay.log", 1);
If (Not $cmdline[0]) Then
    If(EnvGet("SOUND_PLAYING") and EnvGet("SOUND_NAME")) Then
        For $i = 1 to 5 step +1
            if(ProcessClose("soundplayer.exe")) Then
                logWrite("Killed sound " & EnvGet("SOUND_NAME"));
                ExitLoop;
            Else
                logWrite("Attempted to kill sound " & EnvGet("SOUND_NAME") & ", but it failed.");
            EndIf
        Next
    EndIf
    logWrite("soundplayer.exe called without arguments.");
    Exit;
EndIf
If (Not StringInStr($cmdline[1], ".mp3")) Then
    logWrite("soundplayer.exe called for a non-mp3 sound file.");
    Exit;
EndIf
If (Not FileExists($cmdline[1])) Then
    logWrite("soundplayer.exe called for a non-existant file.");
    Exit;
EndIf

EnvSet("SOUND_PLAYING", 1);
EnvSet("SOUND_NAME", $cmdline[1]);
$sSound = _SoundOpen($cmdline[1]);
If ($sSound) Then
    logWrite("Began playing sound " & $cmdline[1]);
    _SoundPlay($sSound, 1);
Else
    logWrite("Could not open the sound " & $cmdline[1])
    Exit
EndIf

logWrite("Ended playing sound " & $cmdline[1]);
EnvSet("SOUND_NAME", 0);
EnvSet("SOUND_PLAYING", 0);
Func logWrite($msg)
    $tStamp = @Mon & "/" & @MDAY & "/" & @YEAR & " - " & @HOUR & ":" & @MIN & ":" & @SEC;
    FileWrite($fLog, $tStamp & " - " & $msg & @CRLF);
    Return 0;
EndFunc

Here's the log output with no parameters, a non-mp3 parameter, an MP3 (but non-existant) parameter, and finally two with actual MP3 files passed.

05/10/2010 - 16:17:59 - soundplayer.exe called without arguments.
05/10/2010 - 16:18:04 - soundplayer.exe called for a non-mp3 sound file.
05/10/2010 - 16:18:12 - soundplayer.exe called for a non-existant file.
05/10/2010 - 16:18:48 - Could not open the sound feelthenoize.mp3
05/10/2010 - 16:19:02 - Could not open the sound E:\Code\Final Project\feelthenoize.mp3

Can anyone lend a hand?

  • Moderators
Posted

NELyon,

As the Help file tells you, _SoundOpen returns an array. So you need to test that the return is indeed an array: :)

EnvSet("SOUND_PLAYING", 1);
EnvSet("SOUND_NAME", $testcmdline[1]);
$sSound = _SoundOpen($testcmdline[1]);

If IsArray($sSound) Then ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    logWrite("Began playing sound " & $testcmdline[1]);
    _SoundPlay($sSound, 1);
Else
    logWrite("Could not open the sound " & $testcmdline[1])
    Exit
EndIf

Works fine for me like that. :idea:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

  Reveal hidden contents

 

Posted

  On 5/10/2010 at 8:38 PM, 'Melba23 said:

NELyon,

As the Help file tells you, _SoundOpen returns an array. So you need to test that the return is indeed an array: :)

EnvSet("SOUND_PLAYING", 1);
EnvSet("SOUND_NAME", $testcmdline[1]);
$sSound = _SoundOpen($testcmdline[1]);

If IsArray($sSound) Then ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    logWrite("Began playing sound " & $testcmdline[1]);
    _SoundPlay($sSound, 1);
Else
    logWrite("Could not open the sound " & $testcmdline[1])
    Exit
EndIf

Works fine for me like that. :idea:

M23

I.. just... OH MY GOD I LOVE YOU.

I completely forgot how to do that, it's been so long since I've AutoIt'd. Thanks for the help.

  • Moderators
Posted (edited)

NELyon,

  Quote

I completely forgot how to do that, it's been so long since I've AutoIt'd

You also seem to have forgotten to use the "Add Reply" button at the top and bottom of the page rather then the "Reply" button in the post itself. Then you do not get the contents of the previous post quoted in your reply and the whole thread becomes easier to read. :)

By the way, just to make you feel better, _SoundOpen only returns an array since we updated the UDF last year, so your original code was probably correct for earlier versions. :idea:

M23

Edit: Added update info.

Edited by Melba23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

  Reveal hidden contents

 

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...