Results 1 to 3 of 3

Thread: Phonon::MediaSource

  1. #1
    Join Date
    Sep 2010
    Posts
    2
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Phonon::MediaSource

    It is my first time to post a question here, and I have a simple problem about Phonon::MediaSource class. Codes like this:
    Qt Code:
    1. QString string = "It's ok.mp3";
    2. Phonon::MediaSource source1(string);
    3. Phonon::MediaSource source2(string);
    4. if(source1 == source2)
    5. {
    6. qDebug("equal");
    7. }
    8. else
    9. {
    10. qDebug("not equal");
    11. }
    To copy to clipboard, switch view to plain text mode 
    The output is "not equal".
    why above codes will return false? And how to get true when I compare two MediaSources?

    By the way, I am using the QList<Phonon::MediaSource> to store the MediaSources in my project, but I don't want to stroe the same source in it, I know that it should use the method QList::contains(MediaSource source) to decide whether the source has existed. of course, I fail. It will store the same mediasource more than once. I think it may have a relation to the 'MediaSource:perator== ( const MediaSource & other )'
    Qt Code:
    1. QList<Phonon::MediaSource> sources;
    2. Phonon::source1("it's ok.mp3");
    3. Phonon::source2("it's ok.mp3");
    4. if(!sources.contains(source1))
    5. {
    6. sources.append(source1);
    7. }
    8. if(!sources.contains(source2))
    9. {
    10. sources.append(source2);
    11. }
    To copy to clipboard, switch view to plain text mode 
    Look forward to any suggestions or help. Thank you!

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Phonon::MediaSource

    This is the code for Mediasource:

    Qt Code:
    1. MediaSource::MediaSource(const MediaSource &rhs)
    2. : d(rhs.d)
    3. {
    4. }
    5.  
    6. MediaSource &MediaSource::operator=(const MediaSource &rhs)
    7. {
    8. d = rhs.d;
    9. return *this;
    10. }
    11.  
    12. bool MediaSource::operator==(const MediaSource &rhs) const
    13. {
    14. return d == rhs.d;
    15. }
    To copy to clipboard, switch view to plain text mode 

    This means that the operator == only returns true when the pointer to the private structure is the same.
    This in turn means you can only get true when you use the = operator or the MediaSource(otherMediaSource) constructor.

    In your case, the private structures are different and will always return false

    Suggestion:
    Qt Code:
    1. if (source1.url() == source2.url()) // maybe also check the type
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to tbscope for this useful post:

    Lendamand (8th September 2010)

  4. #3
    Join Date
    Sep 2010
    Posts
    2
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Phonon::MediaSource

    Thank you for tbscope's help, but I still have a problem. How can I make sure that QList just store the only one mediasoure, while I must construct my source using 'MediaSource(QString fileName)'? The following is my QT codes which may be similar to the QT's demo:music player.
    Qt Code:
    1. void MainWindow::addfile()
    2. { QStringList files = QFileDialog::getOpenFileNames(this, tr("Select Music Files"), QDesktopServices::storageLocation(QDesktopServices::MusicLocation));
    3. if (files.isEmpty())
    4. return;
    5. int index = sources.size();
    6. foreach (QString string, files)
    7. {
    8. Phonon::MediaSource source(string);
    9. if(!sources.contains(source)) //how can I make sure sources just store the same mediasource one time?
    10. {
    11. sources.append(source);
    12. }
    13. }
    14. if (!sources.isEmpty() && sources.size()>index)
    15. metaInformationResolver->setCurrentSource(sources.at(index));
    16. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. How to create custom (streaming video) Phonon::MediaSource?
    By high_flyer in forum Qt Programming
    Replies: 2
    Last Post: 17th February 2017, 20:30
  2. Phonon MediaSource authentication
    By serenti in forum Qt Programming
    Replies: 0
    Last Post: 11th June 2009, 23:01
  3. Qt4.4.3 Phonon
    By alexcuiCN in forum Qt Programming
    Replies: 0
    Last Post: 18th April 2009, 03:09
  4. Phonon V4L
    By kovariadam in forum Newbie
    Replies: 1
    Last Post: 31st January 2009, 15:35
  5. Phonon
    By QTInfinity in forum Qt Programming
    Replies: 2
    Last Post: 19th November 2008, 14:24

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.