2

I am creating a python voice assistant referring from a youtube tutorial. Unfortunately, the video creator has a windows os. So he uses startfile from OS module. But this is not working in ubuntu. Here is the code that the video maker used:

elif 'play music' in query:     
        music_dir = '/home/thilak/Music'
        songs = os.listdir(music_dir)
        print(songs)
        os.startfile(os.path.join(music_dir, songs[0]))

But this didn't work out for me, so I used this one:

elif 'play music' in query:     
        music_dir = '/home/thilak/Music'
        songs = os.listdir(music_dir)
        print(songs)
        os.system(os.path.join(music_dir, songs[0])) #changing startfile to system

Still, this code too shows an error message like this:

Say that again please...
Listening....
recognizing...
User said: play music

['Despacito and senorita (copy).mp4', 'Curtis Waters - Stunnin’ (Lyrics) “ I’m a pretty boy,I’m stunning “-YVrha4CjldA.mkv', 'DJ Snake ft. Justin Bieber - Let Me Love You [Lyric Video]-SMs0GnYze34 (copy).mkv', 'Curtis Waters - Stunnin’ (Lyrics) “ I’m a pretty boy,I’m stunning “-YVrha4CjldA (copy).mkv', 'Despacito and senorita.mp4', 'DJ Snake ft. Justin Bieber - Let Me Love You [Lyric Video]-SMs0GnYze34.mkv']
sh: 1: Syntax error: "(" unexpected

Please help me resolve this issue. Thank you!

Ice Bear
  • 2,676
  • 1
  • 8
  • 24
Thilak S
  • 31
  • 1
  • 8
  • `sh: 1: Syntax error: "(" unexpected` -- search for that error. Nothing to do with Linux-specific APIs, Python or Ubuntu. In general, you also have to extract a [mcve] from your code for posting here. As a new user, please also take the [tour] and read [ask]. – Ulrich Eckhardt Dec 27 '20 at 13:40

1 Answers1

1

As given, the code was meant for windows, how about using ffmpeg's ffplay

elif 'play music' in query:     
        music_dir = '/home/thilak/Music'
        songs = os.listdir(music_dir)
        print(songs)
        os.popen('ffplay -nodisp -autoexit "'+os.path.join(music_dir, songs[0])+'" >/dev/null 2>&1')

before using the above codes do remember to install ffmpeg using apt

sudo apt-get update
sudo apt-get install ffmpeg
AmaanK
  • 1,032
  • 5
  • 25