How to go around in playing audio files directly in Python 3?












0















I am trying the gTTS (Google Text To Speach) function in python, saving the mp3 file works (the file is being saved and can be played).



Now I am trying to play the file directly with the below code, but it is throwing an error



Code:



import gtts
import pyglet
import os
import time

text = ("Hello World")

obj = gtts.gTTS(text=text, lang='en')
speech_filename = 'c:/test_voice.mp3'
obj.save(speech_filename)

print("Play sound...")

music = pyglet.media.load(speech_filename, streaming=False)
music.play

sleep.time(music.duration) #prevent from killing
os.remove(speech_filename) #remove temp file


Error:



Traceback (most recent call last):
File "C:pythontext-to-speach.py", line 16, in
music = pyglet.media.load(speech_filename, streaming=False)



File "C:Pythonlibsite-packagespygletmediasourcesloader.py", line 63, in load
source = get_source_loader().load(filename, file)



File "C:Pythonlibsite-packagespygletmediasourcesloader.py", line 84, in load
return WaveSource(filename, file)



File "C:Pythonlibsite-packagespygletmediasourcesriff.py", line 200, in init



'AVbin is required to decode compressed media')
pyglet.media.sources.riff.WAVEFormatException: AVbin is required to decode compressed media










share|improve this question





























    0















    I am trying the gTTS (Google Text To Speach) function in python, saving the mp3 file works (the file is being saved and can be played).



    Now I am trying to play the file directly with the below code, but it is throwing an error



    Code:



    import gtts
    import pyglet
    import os
    import time

    text = ("Hello World")

    obj = gtts.gTTS(text=text, lang='en')
    speech_filename = 'c:/test_voice.mp3'
    obj.save(speech_filename)

    print("Play sound...")

    music = pyglet.media.load(speech_filename, streaming=False)
    music.play

    sleep.time(music.duration) #prevent from killing
    os.remove(speech_filename) #remove temp file


    Error:



    Traceback (most recent call last):
    File "C:pythontext-to-speach.py", line 16, in
    music = pyglet.media.load(speech_filename, streaming=False)



    File "C:Pythonlibsite-packagespygletmediasourcesloader.py", line 63, in load
    source = get_source_loader().load(filename, file)



    File "C:Pythonlibsite-packagespygletmediasourcesloader.py", line 84, in load
    return WaveSource(filename, file)



    File "C:Pythonlibsite-packagespygletmediasourcesriff.py", line 200, in init



    'AVbin is required to decode compressed media')
    pyglet.media.sources.riff.WAVEFormatException: AVbin is required to decode compressed media










    share|improve this question



























      0












      0








      0


      1






      I am trying the gTTS (Google Text To Speach) function in python, saving the mp3 file works (the file is being saved and can be played).



      Now I am trying to play the file directly with the below code, but it is throwing an error



      Code:



      import gtts
      import pyglet
      import os
      import time

      text = ("Hello World")

      obj = gtts.gTTS(text=text, lang='en')
      speech_filename = 'c:/test_voice.mp3'
      obj.save(speech_filename)

      print("Play sound...")

      music = pyglet.media.load(speech_filename, streaming=False)
      music.play

      sleep.time(music.duration) #prevent from killing
      os.remove(speech_filename) #remove temp file


      Error:



      Traceback (most recent call last):
      File "C:pythontext-to-speach.py", line 16, in
      music = pyglet.media.load(speech_filename, streaming=False)



      File "C:Pythonlibsite-packagespygletmediasourcesloader.py", line 63, in load
      source = get_source_loader().load(filename, file)



      File "C:Pythonlibsite-packagespygletmediasourcesloader.py", line 84, in load
      return WaveSource(filename, file)



      File "C:Pythonlibsite-packagespygletmediasourcesriff.py", line 200, in init



      'AVbin is required to decode compressed media')
      pyglet.media.sources.riff.WAVEFormatException: AVbin is required to decode compressed media










      share|improve this question
















      I am trying the gTTS (Google Text To Speach) function in python, saving the mp3 file works (the file is being saved and can be played).



      Now I am trying to play the file directly with the below code, but it is throwing an error



      Code:



      import gtts
      import pyglet
      import os
      import time

      text = ("Hello World")

      obj = gtts.gTTS(text=text, lang='en')
      speech_filename = 'c:/test_voice.mp3'
      obj.save(speech_filename)

      print("Play sound...")

      music = pyglet.media.load(speech_filename, streaming=False)
      music.play

      sleep.time(music.duration) #prevent from killing
      os.remove(speech_filename) #remove temp file


      Error:



      Traceback (most recent call last):
      File "C:pythontext-to-speach.py", line 16, in
      music = pyglet.media.load(speech_filename, streaming=False)



      File "C:Pythonlibsite-packagespygletmediasourcesloader.py", line 63, in load
      source = get_source_loader().load(filename, file)



      File "C:Pythonlibsite-packagespygletmediasourcesloader.py", line 84, in load
      return WaveSource(filename, file)



      File "C:Pythonlibsite-packagespygletmediasourcesriff.py", line 200, in init



      'AVbin is required to decode compressed media')
      pyglet.media.sources.riff.WAVEFormatException: AVbin is required to decode compressed media







      python-3.x






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 1 at 20:14







      user2520212

















      asked Jan 1 at 13:52









      user2520212user2520212

      215




      215
























          2 Answers
          2






          active

          oldest

          votes


















          1














          it is looking for AVbin,
          check following would help you
          https://stackoverflow.com/questions/10302873/python-pyglet-avbin-how-to-install-avbin






          share|improve this answer
























          • I installed AVbin library from [avbin.github.io/AVbin/Download.html] and had to do a simple adjustment to work on Windows 10 64bit - How to fix it on Windows10 [stackoverflow.com/questions/10302873/… But now time is showing an error NameError: name 'sleep' is not defined

            – user2520212
            Jan 1 at 14:57








          • 1





            try time.sleep() not sure sleep.time() is exist

            – ShivYaragatti
            Jan 1 at 15:25



















          0














          Ok so this is how I solved it, I also added a delay loop that waits for the audio file to finish and delete it afterwards.



          import gtts
          import pygame
          #install pyglet and install http://avbin.github.io/AVbin/Download.html
          #extract the avbin.dll from windows/system32/ folder to windows/system/ folder
          import os
          import time

          pygame.mixer.init()

          text = ("Hello, World")

          obj = gtts.gTTS(text=text, lang='en')
          speech_filename = "c:/python/code/test_voice.mp3"
          obj.save(speech_filename)

          print("Play sound...")

          pygame.mixer.music.load(speech_filename)
          pygame.mixer.music.play()

          busy = True
          while busy == True:
          if pygame.mixer.music.get_busy() == False:
          busy = False
          pygame.quit()

          os.remove(speech_filename) #remove temp file - remove line to keep file





          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53996002%2fhow-to-go-around-in-playing-audio-files-directly-in-python-3%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            it is looking for AVbin,
            check following would help you
            https://stackoverflow.com/questions/10302873/python-pyglet-avbin-how-to-install-avbin






            share|improve this answer
























            • I installed AVbin library from [avbin.github.io/AVbin/Download.html] and had to do a simple adjustment to work on Windows 10 64bit - How to fix it on Windows10 [stackoverflow.com/questions/10302873/… But now time is showing an error NameError: name 'sleep' is not defined

              – user2520212
              Jan 1 at 14:57








            • 1





              try time.sleep() not sure sleep.time() is exist

              – ShivYaragatti
              Jan 1 at 15:25
















            1














            it is looking for AVbin,
            check following would help you
            https://stackoverflow.com/questions/10302873/python-pyglet-avbin-how-to-install-avbin






            share|improve this answer
























            • I installed AVbin library from [avbin.github.io/AVbin/Download.html] and had to do a simple adjustment to work on Windows 10 64bit - How to fix it on Windows10 [stackoverflow.com/questions/10302873/… But now time is showing an error NameError: name 'sleep' is not defined

              – user2520212
              Jan 1 at 14:57








            • 1





              try time.sleep() not sure sleep.time() is exist

              – ShivYaragatti
              Jan 1 at 15:25














            1












            1








            1







            it is looking for AVbin,
            check following would help you
            https://stackoverflow.com/questions/10302873/python-pyglet-avbin-how-to-install-avbin






            share|improve this answer













            it is looking for AVbin,
            check following would help you
            https://stackoverflow.com/questions/10302873/python-pyglet-avbin-how-to-install-avbin







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jan 1 at 13:57









            ShivYaragattiShivYaragatti

            32417




            32417













            • I installed AVbin library from [avbin.github.io/AVbin/Download.html] and had to do a simple adjustment to work on Windows 10 64bit - How to fix it on Windows10 [stackoverflow.com/questions/10302873/… But now time is showing an error NameError: name 'sleep' is not defined

              – user2520212
              Jan 1 at 14:57








            • 1





              try time.sleep() not sure sleep.time() is exist

              – ShivYaragatti
              Jan 1 at 15:25



















            • I installed AVbin library from [avbin.github.io/AVbin/Download.html] and had to do a simple adjustment to work on Windows 10 64bit - How to fix it on Windows10 [stackoverflow.com/questions/10302873/… But now time is showing an error NameError: name 'sleep' is not defined

              – user2520212
              Jan 1 at 14:57








            • 1





              try time.sleep() not sure sleep.time() is exist

              – ShivYaragatti
              Jan 1 at 15:25

















            I installed AVbin library from [avbin.github.io/AVbin/Download.html] and had to do a simple adjustment to work on Windows 10 64bit - How to fix it on Windows10 [stackoverflow.com/questions/10302873/… But now time is showing an error NameError: name 'sleep' is not defined

            – user2520212
            Jan 1 at 14:57







            I installed AVbin library from [avbin.github.io/AVbin/Download.html] and had to do a simple adjustment to work on Windows 10 64bit - How to fix it on Windows10 [stackoverflow.com/questions/10302873/… But now time is showing an error NameError: name 'sleep' is not defined

            – user2520212
            Jan 1 at 14:57






            1




            1





            try time.sleep() not sure sleep.time() is exist

            – ShivYaragatti
            Jan 1 at 15:25





            try time.sleep() not sure sleep.time() is exist

            – ShivYaragatti
            Jan 1 at 15:25













            0














            Ok so this is how I solved it, I also added a delay loop that waits for the audio file to finish and delete it afterwards.



            import gtts
            import pygame
            #install pyglet and install http://avbin.github.io/AVbin/Download.html
            #extract the avbin.dll from windows/system32/ folder to windows/system/ folder
            import os
            import time

            pygame.mixer.init()

            text = ("Hello, World")

            obj = gtts.gTTS(text=text, lang='en')
            speech_filename = "c:/python/code/test_voice.mp3"
            obj.save(speech_filename)

            print("Play sound...")

            pygame.mixer.music.load(speech_filename)
            pygame.mixer.music.play()

            busy = True
            while busy == True:
            if pygame.mixer.music.get_busy() == False:
            busy = False
            pygame.quit()

            os.remove(speech_filename) #remove temp file - remove line to keep file





            share|improve this answer




























              0














              Ok so this is how I solved it, I also added a delay loop that waits for the audio file to finish and delete it afterwards.



              import gtts
              import pygame
              #install pyglet and install http://avbin.github.io/AVbin/Download.html
              #extract the avbin.dll from windows/system32/ folder to windows/system/ folder
              import os
              import time

              pygame.mixer.init()

              text = ("Hello, World")

              obj = gtts.gTTS(text=text, lang='en')
              speech_filename = "c:/python/code/test_voice.mp3"
              obj.save(speech_filename)

              print("Play sound...")

              pygame.mixer.music.load(speech_filename)
              pygame.mixer.music.play()

              busy = True
              while busy == True:
              if pygame.mixer.music.get_busy() == False:
              busy = False
              pygame.quit()

              os.remove(speech_filename) #remove temp file - remove line to keep file





              share|improve this answer


























                0












                0








                0







                Ok so this is how I solved it, I also added a delay loop that waits for the audio file to finish and delete it afterwards.



                import gtts
                import pygame
                #install pyglet and install http://avbin.github.io/AVbin/Download.html
                #extract the avbin.dll from windows/system32/ folder to windows/system/ folder
                import os
                import time

                pygame.mixer.init()

                text = ("Hello, World")

                obj = gtts.gTTS(text=text, lang='en')
                speech_filename = "c:/python/code/test_voice.mp3"
                obj.save(speech_filename)

                print("Play sound...")

                pygame.mixer.music.load(speech_filename)
                pygame.mixer.music.play()

                busy = True
                while busy == True:
                if pygame.mixer.music.get_busy() == False:
                busy = False
                pygame.quit()

                os.remove(speech_filename) #remove temp file - remove line to keep file





                share|improve this answer













                Ok so this is how I solved it, I also added a delay loop that waits for the audio file to finish and delete it afterwards.



                import gtts
                import pygame
                #install pyglet and install http://avbin.github.io/AVbin/Download.html
                #extract the avbin.dll from windows/system32/ folder to windows/system/ folder
                import os
                import time

                pygame.mixer.init()

                text = ("Hello, World")

                obj = gtts.gTTS(text=text, lang='en')
                speech_filename = "c:/python/code/test_voice.mp3"
                obj.save(speech_filename)

                print("Play sound...")

                pygame.mixer.music.load(speech_filename)
                pygame.mixer.music.play()

                busy = True
                while busy == True:
                if pygame.mixer.music.get_busy() == False:
                busy = False
                pygame.quit()

                os.remove(speech_filename) #remove temp file - remove line to keep file






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 1 at 20:10









                user2520212user2520212

                215




                215






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53996002%2fhow-to-go-around-in-playing-audio-files-directly-in-python-3%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Monofisismo

                    Angular Downloading a file using contenturl with Basic Authentication

                    Olmecas