【问题标题】:Re-opening a file in python gives permission error在python中重新打开文件会出现权限错误
【发布时间】:2017-02-23 09:10:11
【问题描述】:

我已搜索并尝试实施此处建议的解决方案: Errno 13 Permission denied: 'file.mp3' Python Error while re-opening sound file in python

但似乎没有任何好的解决方案。这是我的代码,谁能告诉我我在这里做错了什么:

#!/usr/bin/env python3
# Requires PyAudio and PySpeech.
import time, os
import speech_recognition as sr
from gtts import gTTS
import pygame as pg
import mutagen.mp3

#Find out what input sound device is default (use if you have issues with    microphone)
#import pyaudio
#sdev= pyaudio.pa.get_default_input_device()

def play_music(sound_file, volume=0.8):
    '''
    stream music with mixer.music module in a blocking manner
    this will stream the sound from disk while playing
    '''
    # set up the mixer, this will set it up according to your sound file
    mp3 = mutagen.mp3.MP3(sound_file)
    pg.mixer.init(frequency=mp3.info.sample_rate)
    pg.mixer.music.set_volume(volume)
    try:
        pg.mixer.music.load(sound_file)
        print("HoBo Sound file {} loaded!".format(sound_file))
    except pg.error:
        print("HoBo Sound file {} not found! ({})".format(sound_file,   pg.get_error()))
        return
    pg.mixer.music.play()
    while pg.mixer.music.get_busy() == True:
        continue
    pg.mixer.quit()
    sound_file.close()

def speak(audioString):
    print(audioString)
    tts = gTTS(text=audioString, lang='en')
    tts.save("audio.mp3")
    # pick a mp3 file in folder or give full path
    sound_file = "audio.mp3"
    # optional volume 0 to 1.0
    volume = 0.6
    play_music(sound_file, volume)

def audioIn():
    # Record Audio from Microphone
    r = sr.Recognizer()
    with sr.Microphone() as source:
        print("Say something!")
        audio = r.listen(source)

    # Google Speech Recognition
    try:
        # for testing purposes, we're just using the default API key
        # to use another API key, use `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")`
        # instead of `r.recognize_google(audio)`
        data = r.recognize_google(audio)
        print("You said: ", data)

    except sr.UnknownValueError:
        print("Google Speech Recognition could not understand audio")
    except sr.RequestError as e:
        print("Could not request results from Google Speech Recognition service; {0}".format(e))

    return data


def hobo(data):
    if "how are you" in data:
        speak("I am fine")

    if "what time is it" in data:
        speak(time.ctime())

    if "where is" in data:
        data = data.split(" ")
        location = data[2]
        speak("Hold on Sir, I will show you where " + location + " is.")
        os.system("chromium-browser https://www.google.nl/maps/place/" + location + "/&")

# Starts the program
#time.sleep(2)
speak("Testing")

while(data != "stop"):
    data = audioIn()
    hobo(data)
else:
    quit

【问题讨论】:

  • 你能把你的问题说得更具体一些吗? Stackoverflow 不是一个只向人们扔代码并期望他们看到问题所在的网站。代码在哪里崩溃了?
  • 我怀疑的第一件事是您没有正确关闭文件,因此当您尝试重新打开它时它已经在使用中。看来您正在对字符串变量 (sound_file.close()) 调用 .close()。你确定这就是你应该用 Mutagen 做的吗?我不会花时间为你通读文档。
  • 非常感谢,很抱歉,这是我第一次使用 stackoverflow。下次我会做得更好 :) 你实际上帮助我指出了正确的方向,在 play_music 函数中添加 mp3.save() 帮助我取得了进步。谢谢!!

标签: python pygame speech-recognition mutagen


【解决方案1】:

所以我在我已经浏览过的原始线程之一中找到了修复程序。解决方法是实现一个 delete() 函数,如下所示:

def delete():
    time.sleep(2)
    pg.mixer.init()
    pg.mixer.music.load("somefilehere.mp3")
    os.remove("audio.mp3")

并更改 play_music() 函数,使其最后包含 delete() 函数(当然,我删除了 sound_file.close() 语句)。

【讨论】:

    【解决方案2】:

    按照以下方法进行

    import time
    from gtts import gTTS
    import pygame
    
    def Text_to_speech():
        Message = "hey there"
        speech = gTTS(text=Message)
        speech.save('textToSpeech.mp3')
        pygame.mixer.init()
        pygame.mixer.music.load("textToSpeech.mp3")
        pygame.mixer.music.play()
        time.sleep(3)
        pygame.mixer.music.unload()
    
    

    【讨论】:

      猜你喜欢
      • 2020-11-19
      • 1970-01-01
      • 2019-11-17
      • 1970-01-01
      • 2020-11-27
      • 2017-04-11
      • 2019-07-07
      • 2017-04-27
      • 1970-01-01
      相关资源
      最近更新 更多