python 如何在播放音频时执行下一段代码

wyyhbhjk  于 2023-01-04  发布在  Python
关注(0)|答案(1)|浏览(143)

我正在编写一个程序,它从文本文件中读取单词,如果找到与音频文件匹配的单词,则播放音频。但是,发生的情况是,for循环直到playsound()播放完声音才中断。我希望for循环中断并再次迭代,同时在后台播放音频。我该如何实现呢?
这是密码:

import os
from playsound import playsound
import time

text = open("book.txt",encoding="utf-8").read()
words_length = len(text.split())
split = text.split()
path = "Audio_tracks"
dir_list = os.listdir(path)

def linear_search(list, n , key):
    for i in range(0, n):  
        if (list[i] == key):  
            return i 
    return -1 

for word in split:
    file = f"{word}.mp3"
    file.lower()
    search_result = linear_search(dir_list, len(dir_list), file)
    if search_result == -1:
        print("Audio not found")
    else:
        playsound(f"Audio_tracks/{file}")
    time.sleep(0.16)

这是我的txt文件(以防万一):

start In the year 1878 I took my degree of Doctor of Medicine of the University of London, and proceeded to Netley to go through the course prescribed for surgeons in the army. Having completed my studies there, I was duly attached to the Fifth Northumberland Fusiliers as Assistant Surgeon. The regiment was stationed in India at the time, and before I could join it, the second Afghan war had broken out. On landing at Bombay, I learned that my corps had advanced through the passes, and was already deep in the enemy’s country. I followed, however, with many other officers who were in the same situation as myself, and succeeded in reaching Candahar in safety, where I found my regiment, and at once entered upon my new duties. The campaign brought honours and promotion to many, but for me it had nothing but misfortune and disaster. I was removed from my brigade and attached to the Berkshires, with whom I served at the fatal battle of Maiwand. There I was struck on the shoulder by a Jezail bullet, which shattered the bone and grazed the subclavian artery. I should have fallen into the hands of the murderous Ghazis had it not been for the devotion and courage shown by Murray, my orderly, who threw me across a pack-horse, and succeeded in bringing me safely to the British lines. Worn with pain, and weak from the prolonged hardships which I had undergone, I was removed, with a great train of wounded sufferers, to the base hospital at Peshawar. Here I rallied, and had already improved so far as to be able to walk about the wards, and even to bask a little upon the verandah, when I was struck down by enteric fever, that curse of our Indian possessions. For months my life was despaired of, and when at last I came to myself and became convalescent, I was so weak and emaciated that a medical board determined that not a day should be lost in sending me back to England. I was dispatched, accordingly, in the troopship Orontes, and landed a month later on Portsmouth jetty, with my health irretrievably ruined, but with permission from a paternal government to spend the next nine months in attempting to improve it. I had neither kith nor kin in England, and was therefore as free as air—or as free as an income of eleven shillings and sixpence a day will permit a man to be. Under such circumstances, I naturally gravitated to London, that great cesspool into which all the loungers and idlers of the Empire are irresistibly drained. There I stayed for some time at a private hotel in the Strand, leading a comfortless, meaningless existence, and spending such money as I had, considerably more freely than I ought. So alarming did the state of my finances become, that I soon realized that I must either leave the metropolis and rusticate somewhere in the country, or that I must make a complete alteration in my style of living. Choosing the latter alternative, I began by making up my mind to leave the hotel, and to take up my quarters in some less pretentious and less expensive domicile. On the very day that I had come to this conclusion, I was standing at the Criterion Bar, when some one tapped me on the shoulder, and turning round I recognized young Stamford, who had been a dresser under me at Bart’s. The sight of a friendly face in the great wilderness of London is a pleasant thing indeed to a lonely man. In old days Stamford had never been a particular crony of mine, but now I hailed him with enthusiasm, and he, in his turn, appeared to be delighted to see me. In the exuberance of my joy, I asked him to lunch with me at the Holborn, and we started off together in a hansom. “Whatever have you been doing with yourself, Watson?” he asked in undisguised wonder, as we rattled through the crowded London streets. “You are as thin as a lath and as brown as a nut.” I gave him a short sketch of my adventures, and had hardly concluded it by the time that we reached our destination. “Poor devil!” he said, commiseratingly, after he had listened to my misfortunes. “What are you up to now?” “Looking for lodgings,” I answered. “Trying to solve the problem as to whether it is possible to get comfortable rooms at a reasonable price.” “That’s a strange thing,” remarked my companion; “you are the second man to-day that has used that expression to me.” “And who was the first?” I asked. “A fellow who is working at the chemical laboratory up at the hospital. He was bemoaning himself this morning because he could not get someone to go halves with him in some nice rooms which he had found, and which were too much for his purse.” “By Jove!” I cried, “if he really wants someone to share the rooms and the expense, I am the very man for him. I should prefer having a partner to being alone.”Young Stamford looked rather strangely at me over his wine-glass. “You don’t know Sherlock Holmes yet,” he said; “perhaps you would not care for him as a constant companion.” “Why, what is there against him?” “Oh, I didn’t say there was anything against him. He is a little queer in his ideas—an enthusiast in some branches of science. As far as I know he is a decent fellow enough.” “A medical student, I suppose?” said I. “No—I have no idea what he intends to go in for. I believe he is well up in anatomy, and he is a first-class chemist; but, as far as I know, he has never taken out any systematic medical classes. His studies are very desultory and eccentric, but he has amassed a lot of out-of-the way knowledge which would astonish his professors.” “Did you never ask him what he was going in for?” I asked. “No; he is not a man that it is easy to draw out, though he can be communicative enough when the fancy seizes him.” “I should like to meet him,” I said. “If I am to lodge with anyone, I should prefer a man of studious and quiet habits. I am not strong enough yet to stand much noise or excitement. I had enough of both in Afghanistan to last me for the remainder of my natural existence. How could I meet this friend of yours?” “He is sure to be at the laboratory,” returned my companion. “He either avoids the place for weeks, or else he works there from morning to night. If you like, we shall drive round together after luncheon.” “Certainly,” I answered, and the conversation drifted away into other channels.

相关问题