-1

I'm trying to make an alarm clock, and it works for the most part. But I installed playsound so I could make it play music when it goes off as an alarm clock should. I'm not quite sure how to use paths and even when I place the .wav in the folder it's meant to be it doesn't play. Code:

import datetime
from playsound import playsound

alarmHour = int(input("What hour do you want to wake up?"))
alarmMinute = int(input("What minute do you want?"))
amPm = str(input("Am or Pm")).lower().strip()

if (amPm == "pm"):
    alarmHour = alarmHour + 12

while(1 == 1):
    if(alarmHour == datetime.datetime.now().hour and
       alarmMinute == datetime.datetime.now().minute):
     print("Wake up, lazy!")
     playsound("C:\\Users\\wicke\\AppData\\Local\\Programs\\Python\\Python39\\Lib\\site-packages\\askaway.wav")
     break

print("Exited")

The error:

Error 275 for command:
        open "C:\Users\jeandae\AppData\Local\Programs\Python\Python39\Lib\site-packages\askaway.wav"
    Cannot find the specified file.  Make sure the path and filename are correct.

    Error 263 for command:
        close "C:\Users\jeandae\AppData\Local\Programs\Python\Python39\Lib\site-packages\askaway.wav"
    The specified device is not open or is not recognized by MCI.
Failed to close the file: "C:\Users\jeandae\AppData\Local\Programs\Python\Python39\Lib\site-packages\askaway.wav"
Traceback (most recent call last):
  File "C:/Users/jeandae/AppData/Local/Programs/Python/Python39/alarm 2.py", line 16, in <module>
    playsound("C:\\Users\\jeandae\\AppData\\Local\\Programs\\Python\\Python39\\Lib\\site-packages\\askaway.wav")
  File "C:\Users\jeandae\AppData\Local\Programs\Python\Python39\lib\site-packages\playsound.py", line 72, in _playsoundWin
    winCommand(u'open {}'.format(sound))
  File "C:\Users\jeandae\AppData\Local\Programs\Python\Python39\lib\site-packages\playsound.py", line 64, in winCommand
    raise PlaysoundException(exceptionMessage)
playsound.PlaysoundException: 
    Error 275 for command:
        open "C:\Users\jeandae\AppData\Local\Programs\Python\Python39\Lib\site-packages\askaway.wav"
    Cannot find the specified file.  Make sure the path and filename are correct.
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
SebSebSeb
  • 9
  • 3
  • Include your code as ***text*** in the body of the question. In line with [ask], this makes it much easier for potential answerers to see where you may have run aground in your efforts to make your code work. (On top of that, it's much less effort/clicks to include it as text rather than an image.) – esqew Aug 03 '21 at 20:27
  • Consider simplifying your code to the minimal example that demonstrates your problem. You say your issue is that the `playsound` isn't playing a sound. Consider simplifying your code to just import `playsound` and call it instead of the rest of the code that complicates reasoning about if and how many times `playsound` is actually called. – Kyle Parsons Aug 03 '21 at 20:42
  • Some things to try: Does calling the playsound function directly, without any of the other code work? does playsound('file_that_does_not_exist.wav') produce the same behavior or does it crash? Is your volume on and the sound you're playing loud enough to hear? – Kaia Aug 03 '21 at 20:42
  • Are you sure the file is in the correct folder? you are not calling a folder explicitly so, depending on how you run the code, it will look in one of many folders to find the .wav file – JeffUK Aug 03 '21 at 20:51
  • @KyleParsons When I try to run it on it's own I get a long ass error. From what I'm reading a misplacement could certainly be the issue but I have no idea where I'm meant to place it. – SebSebSeb Aug 03 '21 at 21:22
  • @Keon , no, no it does not. To either one of your questions. My volume is indeed on and loud enough to hear, and thank God because as long as I've been trying to figure this out I'd hate myself if it wasn't. – SebSebSeb Aug 03 '21 at 21:24
  • @JeffUK ,I'm pretty sure? I gave it the exact location AND tried moving it directly to a python directory. But maybe the wrong one. I put it in /AppData/Local/Programs/Python/Python39/ for a test or two. I've tried it was askaway.wav and as ask away.wav – SebSebSeb Aug 03 '21 at 21:27
  • @SebSebSeb please edit the post to include the long ass error – Kaia Aug 04 '21 at 17:43
  • @Keon So about that, it goes past character limits – SebSebSeb Aug 04 '21 at 18:45
  • Can you post the start/end of it? You can also edit your post rather than post it in the comments, which will give a longer character limit – Kaia Aug 04 '21 at 18:58
  • @Keon, I edited the text body. Uhh. It formatted a bit weird and I'm not sure why – SebSebSeb Aug 04 '21 at 19:55
  • "Cannot find the specified file. Make sure the path and filename are correct." is the important part here. The things after it are related errors that happen because that original error. Is your sound file actually in `\Python\Python39\Lib\site-packages\ `? If it isn't, change the path to the correct one. If it is, it might be a permissions thing (python might not be able to see that folder). In that case, try playing a file from a more normal place like `C:\Users\jeandae\Music` or something? – Kaia Aug 04 '21 at 20:04
  • @keon I've tried it from music and I've trid it from Desktop. Also, thanks for the help so far! – SebSebSeb Aug 04 '21 at 20:52

2 Answers2

0

Give the play sound function the absolute path of the audio file ex:

playsound('C:/Users/user/Desktop/away.wav')
0

there is a problem with that package if you want you can use pydub which is more reliable than playsound.

Your code is okay and the problem is with the playsound it is not playing the audio. A very easy way to do so is.

import datetime

from pydub import AudioSegment
from pydub.playback import play

alarmHour = int(input("What hour do you want to wake up?"))
alarmMinute = int(input("What minute do you want?"))
amPm = str(input("Am or Pm")).lower().strip()

if (amPm == "pm"):
    alarmHour = alarmHour + 12

while(1 == 1):
    if(alarmHour == datetime.datetime.now().hour and
       alarmMinute == datetime.datetime.now().minute):
     print("Wake up, lazy!")
     file1= AudioSegment.from_file("audiofile.wav")
     play(file1)
     break

print("Exited")

But you need to install pydub and you might face some error i recommend you read this article to properly install pydub.

AlixaProDev
  • 472
  • 1
  • 5
  • 13
  • Thanks big time! I'll update you when I test it. For a good bit I was feeling like an absolute idiot. – SebSebSeb Aug 05 '21 at 00:17
  • SO. I download it, wrote this code from pydub import AudioSegment from pydub.playback import play sound = AudioSegment.from_file("C:\\Users\\wicke\\AppData\\Local\\Programs\\Python\\Python39\\Lib\\site-packages\\pydub\\__pycache__\\askaway.wav"), format="wav") play(sound) I get the "cannot assign to function call" – SebSebSeb Aug 05 '21 at 02:07
  • You have to write code as follows. Note that if you put the file in your current project directory you do not have to specify the relative path. just write the name of the file which is present in the current directory. from pydub import AudioSegment from pydub.playback import play sound = AudioSegment.from_file("audiofile.wav", format="wav") play(sound) – AlixaProDev Aug 05 '21 at 07:21
  • Even whenever I do that I get invalid syntax. At first I thought it may be the space between play and sound, but then when I remove that it just says the = is the syntax issue. I'm surprised how hard such a simple thing is to pull off in coding – SebSebSeb Aug 06 '21 at 01:28