omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Playing a list of audio files without blocking audio from other apps

    Pythonista
    audio sound
    3
    6
    931
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • jaalburquerque
      jaalburquerque last edited by

      Hello everyone. I am planning to write a short script to play a list of audio files in order. I was thinking of using the sound.play_effect() function because I would like the audio to play without blocking other audio from other apps from playing. I wonder if this is a good idea. If it is, I am also wondering if there would be a way to know when an effect has finished playing. This would allow me to play the audio files in order, one after another. If using sound.play_effect() would not be recommended, I am willing to use the sound.Player class. I would just like to be able to play the audio, allowing audio from other apps to be played also. Would anyone have any recommendations for me? I would greatly appreciate it. Thanks so much.

      1 Reply Last reply Reply Quote 0
      • JonB
        JonB last edited by

        This can be done for sure using objc_util. Look into AVAudioSession options (duckothers to soften other output, or mixWithOthers to not soften other music.

        What I'm not sure about is whether you can modify the sharedAudioSession() that the sound.Player uses, or if you need to use AVAudioPlayer directly.

        1 Reply Last reply Reply Quote 0
        • jaalburquerque
          jaalburquerque last edited by jaalburquerque

          Thanks so much. Very helpful information. Have a good day.

          1 Reply Last reply Reply Quote 0
          • jaalburquerque
            jaalburquerque last edited by jaalburquerque

            Hello. I was able to write a preliminary version of the script to play the audio files. The script is designed to play audio files contained in a directory in the file system. It assumes that all of the files in the specified directory are audio files. It uses the “FolderPicker” class mentioned in the other topic that I opened asking how to allow the user to select a directory in the file system [1]. I am attaching the script below. The script plays the audio files fine when I run it in the Pythonista app, but when I minimize the app, the audio stops playing. I would like the audio to continue playing even if the app is minimized and I use other apps. Would anyone know if this is possible?

            [1] https://forum.omz-software.com/topic/7506/allowing-the-user-to-select-a-directory-from-the-file-system

            Note: I edited the original post to move the [1] reference directly below the paragraph that references it. Also, I added a change to the script to make it runnable using a pythonista URL scheme, for example “pythonista://PlayAudioDirectory?action=run&argv=audio_directory

            PlayAudioDirectory.py:

            import sys
            import re
            import dialogs
            import FolderPicker
            from pathlib import Path
            import sound
            import time
            
            IPHONE_VOLUME_PATH = '/private/var/mobile/Containers/Shared/AppGroup/193D444B-2870-447A-A797-33D935C572B3/File Provider Storage'
            ICLOUD_VOLUME_PATH = '/private/var/mobile/Library/Mobile Documents/com~apple~CloudDocs'
            
            audioDirectory = None
            
            if len(sys.argv) > 1:
              audioDirectory = sys.argv[1]
              audioDirectory = re.sub(r'^On My iPhone', IPHONE_VOLUME_PATH, audioDirectory)
              audioDirectory = re.sub(r'^iCloud Drive', ICLOUD_VOLUME_PATH, audioDirectory)
            else:
              volume = None
            
              volumerChoice = dialogs.list_dialog('Select location of directory', ['This iPhone', 'iCloud'])
              if volumerChoice == 'This iPhone':
                volume = IPHONE_VOLUME_PATH
              elif volumerChoice == 'iCloud':
                volume = ICLOUD_VOLUME_PATH
                
              if volume != None:
                audioDirectory = FolderPicker.folder_picker_dialog('Please select the audio folder', root_dir = volume)
            
            if audioDirectory != None:
              audioPath = Path(audioDirectory)
              for child in sorted(audioPath.iterdir()):
                audioFilename = str(child.resolve())
                player = sound.Player(audioFilename)
                duration = player.duration
                player.play()
                time.sleep(duration)
            
            cvp 1 Reply Last reply Reply Quote 0
            • cvp
              cvp @jaalburquerque last edited by cvp

              @jaalburquerque First of all, you have to know that the numeric part 193D444B-2870-447A-A797-33D935C572B3 of your path is different on each device, thus if somebody wants to use your script, he has to identify this code by the little script explained some posts above in this forum.

              Your question is the same as a lot of users asking to run Pythonista in background. What is not really possible. Try to find the word background in this forum and read the big number of topics with that subject. 😢

              Thé nearest approach I could do is explained here but it has not been a real success.

              Anyway, the Pyto app provides a background possibility.

              1 Reply Last reply Reply Quote 0
              • jaalburquerque
                jaalburquerque last edited by

                Thanks for taking the time to answer the question that I had. Thanks for the information. Have a good day.

                1 Reply Last reply Reply Quote 0
                • First post
                  Last post
                Powered by NodeBB Forums | Contributors