0

I'm trying to play an mp3 file using python VLC but it seems like nothing is happening and there is no error message. Below is the code:

import vlc
p = vlc.MediaPlayer(r"C:\Users\user\Desktop\python\projects\etc\lady_maria.mp3")
p.play()

I tried below code as I've read from another post:

import vlc
mp3 = "C:/Users/user/Desktop/python/projects/etc/lady_maria.mp3"

instance = vlc.get_default_instance()
media = instance.media_new(mp3)
media_list = instance.media_list_new([mp3])

player = instance.media_player_new()
player.set_media(media)

list_player = instance.media_list_player_new()
list_player.set_media_player(player)
list_player.set_media_list(media_list)

I also tried to use pygame mixer but same reason, no sounds, and no error message.

from pygame import mixer

mixer.init()
mixer.music.load(r'C:\Users\user\Desktop\python\projects\etc\lady_maria.mp3')
mixer.music.play()

All of these do not give an error message so I'm not sure what is going on.. Any advice will be greatly appreciated!

Sarah
  • 627
  • 7
  • 25
  • Could it be Audio Volume? – Kingsley Jun 10 '20 at 03:35
  • @Kingsley I tried the audio but still not working and no error messages... :( – Sarah Jun 10 '20 at 14:42
  • With the Pygame example - you did wait for the music to play right? `mixer.music.play()` just *starts* the music playing, then returns immediately. See: https://stackoverflow.com/a/8415875/1730895 – Kingsley Jun 10 '20 at 22:28
  • @Kingsley yes I waited but nothing, with volume 100. Do you recommend any other audio playing library other than vlc and pygame? – Sarah Jun 12 '20 at 01:11
  • Does that particular mp3 play OK with other software? – Kingsley Jun 12 '20 at 01:21
  • @Kingsley yes, I mean, I can play the mp3 file without any problem. I recently downloaded vlc (since originally I tried to use python vlc), do you think it could be the problem? – Sarah Jun 12 '20 at 01:30

3 Answers3

1

If audio files are playing fine on the system:-

for pygame library adjust volume using:

mixer.music.set_volume(1.0)  # float value from 0.0 to 1.0 for volume setting 
MockinJay
  • 86
  • 1
  • 7
  • Hi! Thank you for input, I tried this but still, no sound even after setting the volume to 100... Would that be another reason? – Sarah Jun 10 '20 at 14:42
1

So the problem was solved as:

from pygame import mixer

mixer.init()
mixer.music.load(r'C:\Users\user\Desktop\python\projects\etc\lady_maria.mp3')
mixer.music.play()
time.sleep(5)

adding time.sleep(5) fixed the problem!

Pygame, sounds don't play

Sarah
  • 627
  • 7
  • 25
0

I can't work out what the actual issue is given the code in the OP. Please try this test code.

import pygame
  
WINDOW_WIDTH  = 200
WINDOW_HEIGHT = 200

### initialisation
pygame.init()
pygame.font.init()
pygame.mixer.init()
window = pygame.display.set_mode( ( WINDOW_WIDTH, WINDOW_HEIGHT ) )

# Rain sound from: https://www.freesoundslibrary.com/sound-of-rain-falling-mp3/ (CC BY 4.0)
pygame.mixer.music.load( 'rain-falling.mp3' )
pygame.mixer.music.play( loops=-1 )             # loop forever

### Main Loop
clock = pygame.time.Clock()
font = pygame.font.SysFont(None, 40)
done = False
while not done:

    # Handle user-input
    for event in pygame.event.get():
        if ( event.type == pygame.QUIT ):
            done = True

    # Draw some text
    window.fill( ( 0, 0, 50 ) )
    ms = font.render( str( pygame.time.get_ticks() ), True, ( 255,255,255 ) )
    window.blit( ms, ( 100, 100 ) )
    pygame.display.flip()

    # Clamp FPS
    clock.tick_busy_loop(30)

pygame.quit()

If you download the MP3 from the link in the source, and running this script produces no sound, the issue is with your local machine setup. In this case perhaps verify you are using a recent version of Python and PyGame. I've tested this with Python 3.8.2 and PyGame 1.9.6.

I don't think installing VLC Media Player would hurt anything. This is my daily "go-to" media player.

HoldOffHunger
  • 18,769
  • 10
  • 104
  • 133
Kingsley
  • 14,398
  • 5
  • 31
  • 53
  • Hi, thank you so much for the help, well this one works just fine!!! I hear the rain noise loud and clear. Now I'm more confused about what I'm doing wrong... ;-( – Sarah Jun 15 '20 at 03:23