0

I am using my Raspberry Pi to play songs. I am trying to control the mplayer through a simple python programme. I have chosen python because I new to coding and have set up some simple imput driven menus to choose songs.

I found this code here to play the song :-

import os
SOUND = 'music.mp3'
command = 'mplayer %s 1>/dev/null 2>&1' % SOUND
os.system(command)

But I can't work out how to stop it part way through a track and return to my simple python menu to choose another option. Please help . . .

Ross
  • 13
  • 1
  • 4

1 Answers1

0

You can use subprocess.Popen and kill the process whenever you want to end the song:

from subprocess import Popen

SOUND = 'music.mp3'
proc = Popen(['mplayer', '%s', '1>/dev/null', '2>&1',SOUND])
import time

time.sleep(3)
proc.kill()
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321