简体   繁体   English

我的 python function 中的 for 循环没有运行

[英]The for loop in my python function is not running

My for loop inside the function "play()" is not getting executed at runtime.我在 function“play()”中的 for 循环在运行时没有被执行。

def play():
    print("1")
    for files in os.listdir(os.getcwd()):
        if files.endswith("mp3"):
            print("2")
            pygame.mixer.Sound.play(files)
        else:
            continue
play()

pygame seems a poor choice for a media player, unless you have it loaded for another reason. pygame对于媒体播放器来说似乎是一个糟糕的选择,除非您出于其他原因加载它。
I'd suggest you look on SO for vlc.py or Gstreamer examples.我建议您在 SO 上查看vlc.pyGstreamer示例。
That said, the player can only play one thing at a time, so you can't just throw the contents of your disk at it and you need to check if you found anything at all.也就是说,播放器一次只能播放一件事,因此您不能只是将磁盘中的内容扔给它,您需要检查是否找到任何东西。
Try something like this as your starting point:尝试这样的事情作为你的起点:

import os
import pygame.mixer
import time

pygame.mixer.init(frequency=44100)
def play():
    loaded = 0
    queue = []
    path = os.getcwd()
    for mp3 in os.listdir(path):
        if mp3.endswith("mp3"):
            if loaded == 0:
                pygame.mixer.music.load(mp3)
                loaded = 1
                print('Playing:', mp3)
            else:
                print('Queued:', mp3)
                queue.append(mp3)                

    print("\nAudio Queued: ", str(len(queue)), "\n")
    running = True
    if loaded:
        pygame.mixer.music.play()
        while running:
            time.sleep(1)
            if not pygame.mixer.music.get_busy():
                if len(queue):
                    pygame.mixer.music.load(queue[0])
                    print('Playing:', queue[0])
                    queue.pop(0)
                    pygame.mixer.music.play()
                else:
                    running = False
    else:
        print("No media found in ", path)
if __name__ == '__main__':
    play()

Beware the frequency setting in the init .注意init中的frequency设置。
You may have to load other modules to determine what it is for each audio item and re-init the player, to enforce it.您可能必须加载其他模块以确定每个音频项目的内容并重新启动播放器以强制执行它。 Otherwise, you may find things playing at the wrong tempo.否则,您可能会发现以错误的速度播放。

First of all, you don't need the argument 'nulll'.首先,您不需要参数“nulll”。 Second, your function does execute (Make sure you have.mp3 files in your directory):其次,您的 function 确实执行(确保您的目录中有 .mp3 文件):


def play():
    print("1")
    for file in os.listdir(os.getcwd()):
        if file.endswith("mp3"):
            print(file)
            # pygame.mixer.Sound.play(files)
        else:
            continue
play()

Finally if you are iterating through a loop you should name your for variable like a single element in a list.最后,如果您正在遍历循环,您应该将 for 变量命名为列表中的单个元素。

Okay, so just reproduced the error.好的,所以刚刚重现了错误。 Found out you were missing on few imports/ functions.发现您缺少一些导入/功能。 Here is the code you need to run.这是您需要运行的代码。


import os
import pygame.mixer
pygame.mixer.init()
def play():
    print("1")
    for files in os.listdir(os.getcwd()):
        if files.endswith("mp3"):
            print(files)
            pygame.mixer.music.load(files)
            pygame.mixer.music.play()
        else:
            print("No such file exists")

if __name__ == '__main__':
    play()

I guess you were not initiating the mixer function.我猜你没有启动混音器 function。 Also make sure you have mp3 file in your cwd.还要确保您的 cwd 中有 mp3 文件。 You can print cwd and ensure mp3 file is there.您可以打印 cwd 并确保 mp3 文件在那里。

You dont need nulll and print("1") as well.您也不需要 nulll 和 print("1") 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM