question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Playing audio in the background

See original GitHub issue

I searched the internet and looked into the code and it seems currently there’s no way to play sounds in the background of the script. So things like:

import pydub
from pydub.playback import play
import os

pydub.AudioSegment.converter = os.path.dirname(os.path.abspath(__file__)) + r"\ffmpeg.exe"
file = pydub.AudioSegment.from_mp3(r"file.mp3")
print("This should be playing music.")
play(file)
print("It has now played music")

Will print This should be playing music, wait until it ends and then print It has now played music!

A workaround for this would be to export the mp3 file into wav, use winsound to play the file (with the SND_ASYNC flag) then delete it at the end of the script.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
jiaarocommented, Jun 2, 2019

@guysoft First off, your implementation looks good, and anyone using (or wanting to use) pyaudio should have a look!

Having said that, if you use simpleaudio pydub.playback.play() does playback in a thread and it responds to keyboard interrupt properly. If you use pydub’s internal helper function (or just use simpleaudio directly) you can call a function at any time to stop playback:

import time

from pydub import generators
from pydub.playback import _play_with_simpleaudio

# 5 seconds of A 440
sound = generators.Sine(440).to_audio_segment(duration=5000)

##### using pydub's helper function:
playback = _play_with_simpleaudio(audio_segment)

# end playback after 3 seconds
time.sleep(3)
playback.stop()

##### using simpleaudio directly:
import simpleaudio
playback = simpleaudio.play_buffer(
    sound.raw_data, 
    num_channels=sound.channels, 
    bytes_per_sample=sound.sample_width, 
    sample_rate=sound.frame_rate
)
# end playback after 3 seconds
time.sleep(3)
playback.stop()
3reactions
guysoftcommented, Jul 27, 2017

Hey guys, I also needed to write this, since I couldn’t find a full solution here is mine: https://gist.github.com/guysoft/bda8ff5be7eb708fd7057850d788dbdf

You can -h the file and see how to run it. If you want this as a PR please tell me what needs to be done.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Enabling Background Audio | Apple Developer Documentation
iOS and tvOS apps require you to enable certain capabilities for some background operations. A common capability required by playback apps is to...
Read more >
Top ways to play audio or video in the background on android
Top ways to play audio or video in the background on android · A common method for all audio apps · Use YouTube...
Read more >
iOS playing audio in the background - Jonathan Sagorin
How to configure an iOS app to play audio in the background. Code samples available in Swift and Objective-C.
Read more >
How to Play Music in the Background on an Android Phone
1. Tap the "Music" icon in your Android smartphone's app drawer. ; 2. Tap on the name of a song from the list...
Read more >
Play a Video and Continue Playing Audio in Background
Now when you press Home button then the sound will be muted. So to allow the sound continue playing in background mode then...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found