hello ive been trying to get a sound file to play once i press a button in a situation using winsound but i am unable to get the audio to play here is the code i have

import winsound

import tkinter as tk
from functools import partial # a quick way to make a callback function
import choose_your_own_adventure_game_pt2 as AG
import time
winsound.PlaySound('Adrenaline - Intense Suspense Music (No Copyright).wav', winsound.SND_ASYNC)
print('welcome to the')
AG.title()
time.sleep(1)
print('please read each line carefuly once the gui launches')
time.sleep(13)


class Situation(tk.Frame):

    def __init__(self, master=None, story='', buttons=[],song=None, **kwargs):
        tk.Frame.__init__(self, master, **kwargs)

        story_lbl = tk.Label(self, text=story, justify=tk.LEFT, anchor=tk.NW, font=("Play", 10))
        story_lbl.pack()

        for btn_text, new_situation in buttons:
            btn = tk.Button(self, text=btn_text, command=partial(self.quit_, new_situation),padx=10, pady=10)
            btn.pack()
        if song:
            winsound.PlaySound(song, winsound.SND_ASYNC)
    def quit_(self, new_situation):
        self.destroy()
        load(new_situation)

def load(situation=None):
    frame = Situation(root, **SITUATIONS.get(situation))
    frame.pack()

SITUATIONS = {

    None:{
        'song':
        'intro to the game.wav',
        'story':

"""



hello player you have been chosen to embark on your first adventure,

this as a button based game so all your decisions are based on a press of a button,

you will play as a character named Sarah who has a wolf goddess inside her, the goddess' name is Winter,

you as Sarah will have the powers given to you by Winter to get through this game but if you use too much
of that power at any given time your game will end,

pretty soon you will be asked a series of questions your job is to choose the answer that best suits your style of playing the game

but be warned you may end up loosing in the end, so without furthur addo lets start this thing

welcome to 

MOON LANDING EXPIDITION                                             

male scientist:Hello Sarah can you hear me?


""",

        'buttons':[
            ('YES', 'situation_2'),

            ('NO','situation_2_1')
            ]
        },
    'situation_2':{
        'story':
"""


male scientist:good... do you know where you are?



""",
        'buttons' :[
            ('yes', 'situation_3'),

            ('no', 'situation_3_1')
            ]                  
        },
    'situation_2_1':{
        'story':
"""

male scientist: hmm if you can't hear me
then you wouldn't have responded


""",
        'buttons':[
            ('-_-',None)

            ]

        },
    'situation_3':{
        'story':
"""

male scientist:alright so do you know why you are here?
""",
         'buttons':[
             ('yes','situation_4'),

             ('no','situation_4_1')

             ]

        },
    'situation_3_1':{
        'story':
"""

male scientist: you are in a top secret military space center
""",
       'buttons':[
           ('alright','situation_3')

           ]    
        },
    'situation_4':{
        'story':
"""
male scientist:well then you seem to have the rare ability to read people's minds

(a second scientist enters the room and starts talking to the first on the other side of the glass)

female scientist:Dr. Smith there has been sightings of foreign aircrafts approaching our location

Dr. Smith: alright Susan we need to transport Sarah to the space explorer

Susan:Yes Sir

(Susan looks in your direction)

Susan:Sarah will you please head to the door behind you and follow the yellow lines on the floor
""",
     'buttons':[
         ('what is going on?','situation_5'),
         ('do not press', 'never_gonna_give_you_up'),
         ('am i in danger?','situation_5_1'),
         ('ok','situation_5_2')

         ],    
        },
            'situation_4_1':{
        'story':
"""
male scientist:you were chosen out of five million individuals to take part in a space exploration mission to the planet nexu...

(a second scientist enters the room and starts talking to the first on the other side of the glass)

female scientist:Dr. Smith there has been sightings of foreign aircrafts approaching our location

Dr. Smith: alright Susan we need to transport Sarah to the space explorer

Susan:Yes Sir

(Susan looks in your direction)

Susan:Sarah will you please head to the door behind you and follow the yellow lines on the floor
""",
     'buttons':[
         ('what is going on?','situation_5'),
         ('am i in danger?','situation_5_1'),
         ('ok','situation_5_2')

         ],    
        },    
            'never_gonna_give_you_up':{
                'story':
"""
were no strangers to love...


you know the rules and so do i...

iiiiiiiii just wanna tell you how im feeling 

gotta make you UNDERSTAND!!!

never gonna give you up never gonna let you down 

*rick astly.exe has sotpped working you will die now*
""",
    'buttons':[
        ('death', None)

        ],

                },
    }
def beginning():
    start_button.destroy()
    load() # load the first story
    winsound.PlaySound(None, winsound.SND_ASYNC)
#WINDOW
root = tk.Tk()
root.geometry('500x500-500-300')
root.title('Moon landing expedition')
root.attributes('-fullscreen', True)
#START
start_button = tk.Button(root, text="START", command=beginning)
start_button.place(relx=.5, rely=.5, anchor='c')




#THE LOOP
root.mainloop()

is there anyway i can play a sound file through the SITUATIONS variable?

Recommended Answers

I am not a TK person but on line 27 you have this variable "song" but I can't tell how it's value is set which leads me to ask how that is being set.

Jump to Post

Try winsound.SND_FILENAME | winsound.SND_ASYNC for the flags

Jump to Post

All 6 Replies

I am not a TK person but on line 27 you have this variable "song" but I can't tell how it's value is set which leads me to ask how that is being set.

commented: its in def __init__ and in SITUATIONS +0

Try winsound.SND_FILENAME | winsound.SND_ASYNC for the flags

commented: i have tried this already +0

You left out what happens with your code as is. If the file did not play I add a print statement just before the play statement so I can check out if I got the filename correct. If the filename is good then I print if the file exists to further debug.

Remember, I don't see much from you about error messages or what happens so far.

commented: its not that it gives an error its only that the audio doesn't play +0

This is the code I am using in my timer app and it works perfectly.

    # Stop timer and sound alarm if timer expired
    if self.txtRem.GetValue() == '00:00:00':
        self.Stop()
        winsound.PlaySound('alarm.wav',winsound.SND_FILENAME | winsound.SND_ASYNC)
        if self.chkPopup.IsChecked():
            dlg = TimerMessage('Timer Expired', self.txtDesc.GetValue())
            dlg.Show(1)

Does your wav file play properly in another app like vlc media player?

commented: yes it does and it works in python only if i don't add the SND_ASYNC yet it does outside the dictionary +0

hey ii figured out why it wasnt working it turns out that the file size is a factor if the wav file will play or not

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.