0

I am trying to play a youtube video using pafy and vlc:

def run(command, args, voice_instance):
    if command == "pune":
        search_query = " ".join(args)
        result = YoutubeSearch(search_query, max_results=10).to_dict()[0]

        video_title = result["title"]
        url_suffix = result["url_suffix"]

        url = f"https://www.youtube.com/{url_suffix}"
        video = pafy.new(url)
        best = video.getbest()
        playurl = best.url
        Instance = vlc.Instance("--no-video")
        player = Instance.media_player_new()
        Media = Instance.media_new(playurl)
        Media.get_mrl()
        player.set_media(Media)
        voice_instance.say(f'Pun {video_title}')
        player.play()
    
    if "oprește" in command:
        print('1')
        player.stop()
        print('2')

It plays the video, but when i say opreste it prints 1 then stops, and the video is still playing.

Any ideas on how could i fix this ?

2
  • 1
    We probably need to see more of your code to see how you're calling run to give you a real answer but from what you've posted it looks likely that player is going to be undefined when you call it a second time. You need to keep track of the player variable somewhere. Commented Oct 9, 2021 at 20:46
  • This is just an addon for github.com/AlfredoSequeida/karen That is my entire code without imports..i can't think of any way to keep track of the player variable..i tried some things but nothing is working. Do you know how could i do that ? Commented Oct 9, 2021 at 20:52

1 Answer 1

1

Shooting off the hip, but if all you can do is provide a single function, maybe you can store your player as a global variable?

def run(command, args, voice_instance):

    if globals().get('player'):
        instance = globals()['instance']
        player = globals()['player']
    else:
        instance = globals()['instance'] = vlc.Instance("--no-video")
        player = globals()['player'] = instance.media_player_new()    

    if command == "pune":
        search_query = " ".join(args)
        result = YoutubeSearch(search_query, max_results=10).to_dict()[0]

        video_title = result["title"]
        url_suffix = result["url_suffix"]

        url = f"https://www.youtube.com/{url_suffix}"
        video = pafy.new(url)
        best = video.getbest()
        playurl = best.url

        media = instance.media_new(playurl)
        media.get_mrl()
        player.set_media(media)

        voice_instance.say(f'Pun {video_title}')
        player.play()
    
    if "oprește" in command:
        print('1')
        player.stop()
        print('2')
        # and possibly garbage collect the player
        # del globals()['player']
        # del globals()['instance']
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.