-
Notifications
You must be signed in to change notification settings - Fork 0
/
videoplayer.cpp
executable file
·144 lines (123 loc) · 3.64 KB
/
videoplayer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "videoplayer.h"
#include <QDebug>
#include <QJsonArray>
#include <VLCQtCore/Common.h>
#include <VLCQtCore/Instance.h>
#include <VLCQtCore/Media.h>
#include <QJsonObject>
#include <QAbstractListModel>
#include "playlist.h"
YoutubePlayer::YoutubePlayer(QObject *parent)
:QObject(parent),
qmlPlayer(new VlcQmlPlayer(this)),
m_playlist(new Playlist(this))
{
setObjectName("player");
//vlcInstance = new VlcInstance(VlcCommon::args(), this);
//mediaPlayer = new VlcMediaPlayer(vlcInstance);
//qmlPlayer = new VlcQmlPlayer(this);
//m_playlist = new Playlist(this);
this->metaObject()->connectSlotsByName(this);
mediaPlayer = qmlPlayer->player();
//qmlPlayer->removePlayer();
//qmlPlayer->setPlayer(mediaPlayer);
connect(qmlPlayer->player(), &VlcMediaPlayer::end,
this, &YoutubePlayer::maybeNext);
}
void YoutubePlayer::play(QJsonArray formats)
{
QString audio, video;
// for(auto it=formats.begin();
// it != formats.end() && (audio.isNull() || video.isNull());
// it++)
for(auto it=formats.end();
it!= formats.begin() && (audio.isNull() || video.isNull());
it--)
{
auto obj = (*it).toObject();
if(audio.isEmpty() && obj["acodec"].toString() != "none")
audio = obj["url"].toString();
if(video.isEmpty() && obj["vcodec"].toString() != "none"){
qDebug() << "Found video? " << obj["format_note"];
video = obj["url"].toString();
}
}
qDebug() << video << " - " << audio;
play(QUrl{video}, QUrl{audio});
}
void YoutubePlayer::play(QUrl video, QUrl audio)
{
qDebug() << "Video Url: " << video.toString().toLatin1().data();
auto v = new VlcMedia{video.toString(), qmlPlayer->instance()};
libvlc_media_slaves_add(v->core(), libvlc_media_slave_type_audio, 0,
audio.toString().toLatin1().data());
qmlPlayer->open(v);
}
void YoutubePlayer::next()
{
auto a = m_playlist->nextTrack()["formats"].toArray();
if(a.empty())
return;
qDebug() << "Next: " << a;
qmlPlayer->pause();
play(a);
m_playlist->m_currentTrackIndex++;
emit m_playlist->currentTrackChanged();
}
void YoutubePlayer::maybeNext()
{
next();
}
void YoutubePlayer::playFirst()
{
m_playlist->m_currentTrackIndex = 0;
auto a = m_playlist->currentTrack()["formats"].toArray();
if(a.empty())
return;
qmlPlayer->pause();
play(a);
emit m_playlist->currentTrackChanged();
}
void YoutubePlayer::prev()
{
auto a = m_playlist->prevTrack()["formats"].toArray();
if(a.empty())
return;
qDebug() << "Prev: " << a;
qmlPlayer->pause();
play(a);
m_playlist->m_currentTrackIndex--;
emit m_playlist->currentTrackChanged();
}
VlcQmlPlayer* YoutubePlayer::getPlayer()
{
return qmlPlayer;
}
void YoutubePlayer::seekFwd()
{
qmlPlayer->setTime(mediaPlayer->time() + 10 * 1000);
}
void YoutubePlayer::seekBck()
{
qmlPlayer->setTime(mediaPlayer->time() - 10 * 1000);
}
void YoutubePlayer::seekFwdFwd()
{
qmlPlayer->setTime(mediaPlayer->time() + 60 * 1000);
}
void YoutubePlayer::seekBckBck()
{
qmlPlayer->setTime(mediaPlayer->time() - 60 * 1000);
}
void YoutubePlayer::on_playlist_mediaAdded(QModelIndexList newMedia)
{
auto model = m_playlist->property("model").value<QAbstractListModel*>();
qDebug() << "My playlist got some media";
if(model->rowCount() == newMedia.size()
&& qmlPlayer->state() == Vlc::Paused)
play(newMedia[0].data(model->roleNames().key("formats")).value<QJsonArray>());
}
void YoutubePlayer::on_playlist_mediaReset()
{
qDebug() << "AAAAAA RESEEEEEEEET";
}