Skip to content
minimediaplayer.h 2.19 KiB
Newer Older
/*
 * Copyright (C) 2019  Kevin Whitaker <eyecreate@eyecreate.org>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef MINIMEDIAPLAYER_H
#define MINIMEDIAPLAYER_H

#include <QObject>
#include <QtMultimedia/QMediaPlayer>

/**
 * Mini Media Player is a simple class that wraps some wrappers around Qt's Multimedia classes for adding and playing media from a playlist. Also parses URLs to be added to playlist using youtube-dl and makes sure they are valid.
 */
class MiniMediaPlayer : public QObject
{
    Q_OBJECT

public:
    MiniMediaPlayer(QObject *parent = 0);
    QList<QString> getTrackNames();
    
    bool isPlaying();
    
    QMap<QString,qint64> currentState();
    
    bool nextTrack();
    
    bool playPauseTrack(bool playing);
    
    qint64 getCurrentPosition();
    
    qint64 getTrackDuration();
    
    int getVolume();
    
    void setVolume(int volume);
    
signals:
    void playStateChanged(bool isPlaying);
    void playlistChanged(QStringList trackTitles);
    void trackDurationChanged(qint64 length);
    void trackPositionChanged(qint64 position);
    
private slots:
    void mediaStatusChanged(QMediaPlayer::MediaStatus status);
    void mediaStateChanged(QMediaPlayer::State state);
    void durationChanged(qint64 length);
    void positionChanged(qint64 position);
    
private:
    QMediaPlayer *m_player;
    QList<QPair<QString,QUrl>> playlist;
    
    bool isValidMediaUrl(QUrl mediaUrl);
    QUrl getStreamUrlFromUrl(QUrl mediaUrl);
    QList<QPair<QString,QUrl>> getStreamTitlesAndUrlsFromUrl(QUrl mediaUrl);

};

#endif // MINIMEDIAPLAYER_H