-1

I am trying to play an audio file but it only makes the windows alert sound when I open/run the code. How do I fix this?

EDIT: nothing on winsound works it just makes the windows alert noise (windows background.wav) right click on the sound button (bottom right), click sounds, click on the first one and press test. that's the noise.


code:

import winsound
winsound.PlaySound('music.mp3', winsound.SND_ASYNC)
martineau
  • 119,623
  • 25
  • 170
  • 301
Glitchd
  • 361
  • 2
  • 12
  • To keep it playing, you must prevent the script from ending and use the both the `winsound.SND_ASYNC` _and_ `winsound.SND_LOOP` flags (by combining them with `|`). You can prevent the script from ending immediately by adding a call to `input()` at the end. – martineau Mar 08 '19 at 19:35
  • @martineau it is in a game so the code doesn't end – Glitchd Mar 08 '19 at 20:19
  • Please edit your question and add a [mcve] that reproduces the problem. What is "windows background.wiv"? – martineau Mar 08 '19 at 20:30
  • .wiv is an audio file so it's pretty obvious – Glitchd Mar 08 '19 at 20:45
  • When debugging (especially without the real source code), even "obvious" things must be questioned and assumptions avoided, my friend. – martineau Mar 08 '19 at 20:48
  • :) ok my freind – Glitchd Mar 08 '19 at 20:52

2 Answers2

2

The winsound.PlaySound() function is just a thin wrapper around the PlaySound() Win32 API, which only plays waveform audios (WAV) and cannot recognize MP3 files.

If you're not willing to convert the file to WAV manually, you may use the MCI components.

The third-party playsound library provides a concise example for its usage, you can either install it or learn from its source code.

Arnie97
  • 1,020
  • 7
  • 19
  • 1
    i have it as .wav and i still get the du daa noise – Glitchd Mar 08 '19 at 20:18
  • @Glitchd: When I use something invalid as the first argument to `winsound.PlaySound()`, like `'c:/windows/media/not_there.wav'`, a beep sound is made (indicating an error I assume). The script keeps running though, and no exception is raised. – martineau Mar 08 '19 at 20:46
  • 1
    @martineau This is intended behavior; see SND_FILENAME in my first link above. – Arnie97 Mar 08 '19 at 20:49
  • @Arnie97 how do i fix it? – Glitchd Mar 08 '19 at 20:53
  • @Glitchd Did you properly convert your file to PCM waveform format, or just renamed it? – Arnie97 Mar 08 '19 at 20:57
  • it says WAV file next to the name and yes i did it by rename by changing file name extention – Glitchd Mar 08 '19 at 21:01
  • @Glitchd: Here's 14 ways to play (actual) mp3 files—renaming the file isn't good enough. See the question [Playing mp3 song on python](https://stackoverflow.com/questions/20021457/playing-mp3-song-on-python). The answer using `os.system()` might not require you to install a third-party Python module. – martineau Mar 08 '19 at 21:02
  • 1
    @Glitchd This makes no sense and won't turn your file into waveform audio. Search for "convert mp3 to wav" and use any one you like. – Arnie97 Mar 08 '19 at 21:05
  • nice answer, explains a lot. – starriet Dec 16 '21 at 09:00
  • for the record, PlaySound() can play mp3 files if they are packed in a wav file. WAV is just a container, inside sound data can be stored in different encodings. – Augusto Beiro Jul 20 '23 at 05:21
0

by using pygame I found:

from pygame import mixer # Load the required library

mixer.init()
mixer.music.load('music.mp3')
mixer.music.play()

which works fine

Glitchd
  • 361
  • 2
  • 12