I haven't worked with python in a while so excuse me if I made some obvious mistake.
I am creating an application in QT with Python (PySide6) where a list of words is displayed, and the user can select a word from the list and hear the audio of that word. The words are in a language that uses non-ASCII characters and all the MP3 files for word pronunciation are named with the word (example: 祖母.mp3).
Right now, the code for my main window looks something like:
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.ui = MyUserInterface.Ui_MainWindow()
self.ui.setupUi(self)
# some setup for the word list
self.ui.wordList.itemClicked.connect(self.onItemSelected_WordList)
self._audio_output = QAudioOutput()
self._player = QMediaPlayer()
self._player.setAudioOutput(self._audio_output)
self._player.errorOccurred.connect(self.player_error)
def onItemSelected_WordList(self):
currentItem = list.currentItem()
word = list.itemWidget(currentItem).word
self._player.setPosition(0)
self._audio_output.setVolume(100)
self._player.setSource(word.audioPath)
self._player.play()
print(self._player.source())
self.ui.wordList
is a QListWidget and each widget in the list contains a Word
object. The code for my Word class looks like something like this:
class Word(object):
audioFolderPath = "C:/path/to/the/audio/file/"
def __init__(self, wordInfo):
self.text = wordInfo[2]
self.meaning = wordInfo[6]
self.audiofile = self.text + ".mp3"
url = QtCore.QUrl(WordInstance.audioFolderPath)
self.audioPath = QtCore.QUrl(WordInstance.audioFolderPath + self.audiofile)
Let me run you through some things I tried to narrow down my problem:
- The variable
audioFolderPath
is an absolute path, as the files are not in the same folder as my project. I tried copying an audio file to the folder of the project and using the relative path instead of the absolute path. That did not work. - I then tried changing the name of the audio file so there were no non-ASCII characters and using a relative path. This worked.
- I then tried using the absolute path again, but with no non-ASCII characters in the file name. This also worked.
So, to conclude: It worked with a relative path, ASCII only. It worked with an absolute path, ASCII only. But it worked with neither path with non-ASCII characters in the file name. I tried using urllib.parse.quote()
to encode the file path but this didn't work either.
It wouldn't be that hard to rename the files to something that is ASCII-only, but I would prefer not to. My googling couldn't find a solution, so hoping for some help here :)