2017-02-23 3 views
0

Ich habe gesucht und versucht, Lösungen zu implementieren hier vorgeschlagen: Errno 13 Permission denied: 'file.mp3' Python Error while re-opening sound file in pythonRe-Öffnen einer Datei in Python gibt die Erlaubnis Fehler

Aber es scheint nicht zu dieser keine guten Lösungen. Hier ist mein Code, kann mir jemand sagen, was ich falsch hier tue:

#!/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 
+0

Können Sie Ihre Frage präzisieren? Stackoverflow ist kein Ort, an dem man einfach nur Code auf Leute wirft und erwartet, dass sie sehen, was nicht stimmt. Wo im Code stürzt es ab? –

+0

Und das erste, was ich vermutete, war, dass du die Datei nicht richtig geschlossen hast. Als du sie wieder öffnen wolltest, wurde sie bereits benutzt. Es scheint, dass Sie '.close()' für eine String-Variable ('sound_file.close()') aufrufen. Bist du sicher, dass du das mit Mutagen machen sollst? Ich werde mir nicht die Zeit nehmen, die Dokumente für dich durchzulesen. –

+0

Vielen Dank und Entschuldigung, dies ist das erste Mal, dass ich Stackoverflow benutzt habe. Ich werde das nächste Mal besser machen :) Sie half mir tatsächlich in die richtige Regie, Hinzufügen einer mp3.save() zur play_music Funktion half mir Fortschritte. Danke !! – BrotherHelmer

Antwort

0

So fand ich das Update in einem der ursprünglichen Fäden ich schon ging. Das Update war eine delete() Funktion wie so zu implementieren:

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

und Ändern der play_music() Funktion, so dass es die delete() Funktion am Ende enthält (und ich entfernte die sound_file.close() Erklärung von Kurs).

Verwandte Themen