QUrl::fromLocalFile and a # sign in path
I have a problem when using QUrl::fromLocalFile. If I try to open a file with # sign in it's name, it won't work and my program reports the file doesn't exist. I found out that the result in fact points instead of a file "file #2.txt" to "file %232.txt". How can I fix this?
Can I ask why you are using QUrl::fromLocalFile, and what you are then using to open that URL?
As I understand it, the resulting QUrl should, indeed, contain an escape sequence such as %23, as that's what a Web browser, for example, would expect (ie required by the relevant standards).
From the QUrl::fromLocalFile docs:
Note that only certain platforms can actually open this file using QFile::open().
So how are you trying to open this resulting QUrl?
I'm using this to open a local file for playback. When calling the function QMediaPlayer::setMedia I need to call it like this:
And if mediaPath contains the # character in it, it won't play. Unfortunately I don't know any other way to open a local file for playback.
When calling the function QMediaPlayer::setMedia I need to call it like this:
Seems reasonable... indeed, that pattern is shown throughout the QMediaPlayer docs.
Looking through the code, the QUrl is ultimately passed down to the relevant MultiMedia Backend, so this might be an issue specific to one or more MediaService plugins?
What platform are you using? Which Qt version? And which (if you know) MediaService plugin are you using?
Unfortunately I don't know any other way to open a local file for playback.
As a potential workaround, you can pass the the QFile stream to QMediaPlayer::setMedia like:
QFile * file = new QFile(mediaPath, this); if (!file.open(QIODevice::ReadOnly)) return; mediaPlayer.setMedia(QUrl::fromLocalFile(mediaPath), file);
Thank you for the advice. Using this construction I was able to open one of those files with # in it's name. Unfortunately another more then 50% of random files did not play. They were opened correctly. But when I called the mediaPlayer.play() function the playback didn't start without any error.
I noticed this problem on Windows 7. I expect the backend is DirectShow. The same file plays fine on Android. However there are another files which play fine on Windows and don't play on Android. I didn't find any reason why those files should not play. They don't have any special characters in path, it is plain ASCII. And the files play fine in other players. The error I get is again that the file was not found.
I'm thinking about switching to another backend instead. But I'm afraid that I don't have enough knowledge and experience to integrate libVLC, GStreamer or libAV. I found a project called QtAV which seems quite interesting.
But I would still prefer QtMultimedia and not introduce further dependencies. Since I would like to implement a spectrum analyzer a correct replaygain support (currently I'm changing output volume which is not correct) and maybe other sound effects, I will have a look at QAudioOutput and QAudioDecoder. Unfortunately I haven't found any example how these classes can be used together.
It would be nice if somebody updated the spectrum analyzer example with these classes.
In fact I need the exact opposite. What is really strange is the fact that it fails for some files on Windows and for other files on Android. At least on Windows I was able to identify that it is the # sign which causes problems. I have no idea why some files on Android won't load.
can you post the entire function and the snippet where it gets called?
I already wrote it in my second post. I use it in
The mediaPath is a QString with the complete path to my media file and mediaPlayer is an instance of QMediaPlayer.
What about using:
QUrl path = QUrl("file://" + mediaPath);?