QMediaPlayer & QMediaPlaylist - Delete Item (Qt 5.3)



  • Hello all,

    Im developing a music player as a learning application to integrate QML and C++ with QT.

    I have the class to manage the QMediaPlayer and the QMediaPlaylist in C++ and the QML is only for UI (or little functions in JS). I have the list of songs as a listview and im deleting a song from the playlist when I drag the item out of the list. When I delete a song after the playing one, it works perfect but i'm having problems deleting songs in the list before the played song. The thing is , if the song deleted is before in the playlist i must control the index of the QMediaPlaylist and the position of the QMediaPlayer. I tried to set the position of the player once the "seekableChanged" of the QMediaPlayer is thrown, but the song starts at position 0 every time despite the position seems to be correctly set.

    Here is the code of the method to delete songs:

    @void Player::DeleteSong(int index)
    {
    //Only deletes the songs different at the current played/paused
    if(m_mediaPlaylist->currentIndex() != index)
    {
    QMediaPlayer::State originalState = m_player->state();
    int currentIndex = m_mediaPlaylist->currentIndex();
    qint64 position = m_player->position();

        //If the song deleted is before
        if(index < currentIndex)
        {
            //Save the position of the player to set it when seekableChanged is thrown
            m_previousPosition = position;
            m_player->pause();
            m_mediaPlaylist->removeMedia(index);
            //Set the song to the currentIndex-1 because the song deleted was before
            m_mediaPlaylist->setCurrentIndex(currentIndex-1);
        }
        //Else the song deleted is after
        else
        {
            m_mediaPlaylist->removeMedia(index);
        }
    
        //Set the original state again
        switch(originalState){
           case QMediaPlayer::PlayingState:
                m_player->play();
                break;
        }
        
        //Emit delete song to update the UI in QML
        emit deleteSongSignal(index);
    }
    

    }@

    SeekableSlot:

    @void Player::seekableChangedSlot(bool seekable)
    {
    if(seekable){
    if(m_previousPosition>0){
    qDebug()<<"Set position in seekable: " << m_previousPosition;
    m_player->setPosition(m_previousPosition);
    m_previousPosition = 0;
    }
    }
    }@

    Position Changed Slot:
    @void Player::positionChangedSlot(qint64 position){
    qDebug()<<"positionChangedSlot: "<<position;
    QString timeString = GetTimeString(position);
    //Notify the UI to set the slider position
    emit sliderPositionChangedSignal(position,timeString);
    }@

    This is the output:

    bq. //Playing state
    positionChangedSlot: 5765
    positionChangedSlot: 6780
    //After delete the song
    positionChangedSlot: 0
    positionChangedSlot: 0
    Set position in seekable: 7434
    positionChangedSlot: 7434
    //It is set well but returns to position 0 (Why!?)
    positionChangedSlot: 0
    positionChangedSlot: 771
    positionChangedSlot: 1785

    Anyone knows why my player goes to position 0 after i set it in the seekable slot?

    Thank you so much!

    PD: I use the setSosition with a slider from qml (it emits a value and I set the new position from C++ and it works perfect!)



  • Anyone could give me a little shine in this darkness? I tried to solve the problem so many times but I still didn't manage to make it work.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.