Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QUrl::fromLocalFile and a # sign in path

QUrl::fromLocalFile and a # sign in path

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 4 Posters 4.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    vlada
    wrote on last edited by vlada
    #1

    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?

    1 Reply Last reply
    0
    • Paul ColbyP Offline
      Paul ColbyP Offline
      Paul Colby
      wrote on last edited by Paul Colby
      #2

      Hi @vlada

      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?

      Cheers.

      1 Reply Last reply
      0
      • V Offline
        V Offline
        vlada
        wrote on last edited by
        #3

        I'm using this to open a local file for playback. When calling the function QMediaPlayer::setMedia I need to call it like this:

        mediaPlayer.setMedia(QUrl::fromLocalFile(mediaPath));
        

        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.

        Paul ColbyP 1 Reply Last reply
        0
        • V vlada

          I'm using this to open a local file for playback. When calling the function QMediaPlayer::setMedia I need to call it like this:

          mediaPlayer.setMedia(QUrl::fromLocalFile(mediaPath));
          

          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.

          Paul ColbyP Offline
          Paul ColbyP Offline
          Paul Colby
          wrote on last edited by
          #4

          @vlada said:

          When calling the function QMediaPlayer::setMedia I need to call it like this:

          mediaPlayer.setMedia(QUrl::fromLocalFile(mediaPath));
          

          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?

          @vlada said:

          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);
          

          Cheers.

          1 Reply Last reply
          0
          • V Offline
            V Offline
            vlada
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #6

              did you try QUrl::toPercentEncodinghttp://doc.qt.io/qt-5/qurl.html#toPercentEncoding)

              mediaPlayer.setMedia(QUrl::fromLocalFile(QUrl::toPercentEncoding(mediaPath)));
              

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              1 Reply Last reply
              0
              • V Offline
                V Offline
                vlada
                wrote on last edited by vlada
                #7

                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.

                1 Reply Last reply
                0
                • VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by
                  #8

                  can you post the entire function and the snippet where it gets called?

                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                  ~Napoleon Bonaparte

                  On a crusade to banish setIndexWidget() from the holy land of Qt

                  1 Reply Last reply
                  0
                  • V Offline
                    V Offline
                    vlada
                    wrote on last edited by
                    #9

                    I already wrote it in my second post. I use it in

                    mediaPlayer.setMedia(QUrl::fromLocalFile(mediaPath));
                    

                    The mediaPath is a QString with the complete path to my media file and mediaPlayer is an instance of QMediaPlayer.

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      Hi,

                      What about using: QUrl path = QUrl("file://" + mediaPath); ?

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved