Implemented native call to the android MediaSession.
This commit is contained in:
parent
48744f636f
commit
57a099b4c8
@ -7,7 +7,7 @@
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="org.kde.kasts"
|
||||
android:versionName="0.0.1"
|
||||
android:versionCode="1628121202"
|
||||
android:versionCode="1628239323"
|
||||
android:installLocation="auto">
|
||||
|
||||
<supports-screens android:largeScreens="true" android:normalScreens="true" android:anyDensity="true" android:smallScreens="true"/>
|
||||
|
@ -18,7 +18,33 @@ import android.util.Log;
|
||||
|
||||
public class MediaService extends Service {
|
||||
public static final String TAG = "MediaService";
|
||||
|
||||
private MediaSessionCompat mSession;
|
||||
private PlaybackStateCompat.Builder mPBuilder;
|
||||
|
||||
public void setSessionState(int state)
|
||||
{
|
||||
switch(state) {
|
||||
case 0: {
|
||||
mPBuilder.setActions(PlaybackStateCompat.ACTION_PAUSE | PlaybackStateCompat.ACTION_STOP);
|
||||
mPBuilder.setState(PlaybackStateCompat.STATE_PLAYING,
|
||||
0, 1.0f);
|
||||
mSession.setPlaybackState(mPBuilder.build());
|
||||
}
|
||||
case 1: {
|
||||
mPBuilder.setActions(PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_STOP);
|
||||
mPBuilder.setState(PlaybackStateCompat.STATE_PAUSED,
|
||||
0, 1.0f);
|
||||
mSession.setPlaybackState(mPBuilder.build());
|
||||
}
|
||||
case 2: {
|
||||
mPBuilder.setActions(PlaybackStateCompat.ACTION_PAUSE | PlaybackStateCompat.ACTION_STOP);
|
||||
mPBuilder.setState(PlaybackStateCompat.STATE_STOPPED,
|
||||
0, 1.0f);
|
||||
mSession.setPlaybackState(mPBuilder.build());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class MediaSessionCallback extends MediaSessionCompat.Callback {
|
||||
private Context mContext;
|
||||
@ -66,6 +92,7 @@ public class MediaService extends Service {
|
||||
MediaSessionCompat.FLAG_HANDLES_QUEUE_COMMANDS |
|
||||
MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
|
||||
mSession.setCallback(new MediaSessionCallback(this));
|
||||
mPBuilder = new PlaybackStateCompat.Builder();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -30,6 +30,7 @@ set(SRCS_base
|
||||
models/downloadmodel.cpp
|
||||
models/errorlogmodel.cpp
|
||||
models/podcastsearchmodel.cpp
|
||||
mediasessionclient.cpp
|
||||
mpris2/mpris2.cpp
|
||||
powermanagementinterface.cpp
|
||||
resources.qrc
|
||||
|
@ -23,6 +23,7 @@
|
||||
#include "models/errorlogmodel.h"
|
||||
#include "powermanagementinterface.h"
|
||||
#include "settingsmanager.h"
|
||||
#include "mediasessionclient.h"
|
||||
|
||||
static const double MAX_RATE = 1.0;
|
||||
static const double MIN_RATE = 2.5;
|
||||
@ -78,6 +79,8 @@ AudioManager::AudioManager(QObject *parent)
|
||||
|
||||
connect(this, &AudioManager::logError, &ErrorLogModel::instance(), &ErrorLogModel::monitorErrorMessages);
|
||||
|
||||
MediaSessionClient mClient(this);
|
||||
|
||||
// Check if an entry was playing when the program was shut down and restore it
|
||||
if (DataManager::instance().lastPlayingEntry() != QStringLiteral("none")) {
|
||||
setEntry(DataManager::instance().getEntry(DataManager::instance().lastPlayingEntry()));
|
||||
|
@ -8,9 +8,36 @@
|
||||
#include "audiomanager.h"
|
||||
|
||||
#include <QtAndroid>
|
||||
#include <qDebug>
|
||||
|
||||
MediaSessionClient::MediaSessionClient(QObject *parent)
|
||||
MediaSessionClient::MediaSessionClient(AudioManager *audioPlayer, QObject *parent)
|
||||
: QObject(parent)
|
||||
, m_audioPlayer(audioPlayer)
|
||||
{
|
||||
//connections to be added here.
|
||||
connect(m_audioPlayer, &AudioManager::playbackStateChanged, this, &MediaSessionClient::setState);
|
||||
}
|
||||
|
||||
void MediaSessionClient::setState()
|
||||
{
|
||||
QDebug() << m_audioPlayer->playbackState();
|
||||
switch(m_audioPlayer->playbackState()) {
|
||||
case QMediaPlayer::StoppedState :
|
||||
QAndroidJniObject::callStaticMethod<jint>
|
||||
("org/kde/kasts/MediaService"
|
||||
, "setSessionState"
|
||||
, "(I)I"
|
||||
, 2);
|
||||
case QMediaPlayer::PausedState :
|
||||
QAndroidJniObject::callStaticMethod<jint>
|
||||
("org/kde/kasts/MediaService"
|
||||
, "setSessionState"
|
||||
, "(I)I"
|
||||
, 1);
|
||||
case QMediaPlayer::PlayingState :
|
||||
QAndroidJniObject::callStaticMethod<jint>
|
||||
("org/kde/kasts/MediaService"
|
||||
, "setSessionState"
|
||||
, "(I)I"
|
||||
, 0);
|
||||
}
|
||||
}
|
||||
|
@ -6,8 +6,18 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
class MediaSessionClient
|
||||
#include <QObject>
|
||||
|
||||
class AudioManager;
|
||||
|
||||
class MediaSessionClient : public QObject
|
||||
{
|
||||
public:
|
||||
explicit MediaSessionClient(QObject *parent = nullptr);
|
||||
explicit MediaSessionClient(AudioManager *audioPlayer, QObject *parent = nullptr);
|
||||
|
||||
private Q_SLOTS:
|
||||
void setState();
|
||||
|
||||
private:
|
||||
AudioManager *m_audioPlayer = nullptr;
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user