Implemented native call to the android MediaSession.
This commit is contained in:
parent
7008a3ec9b
commit
4a163b0116
android
src
@ -7,7 +7,7 @@
|
|||||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
package="org.kde.kasts"
|
package="org.kde.kasts"
|
||||||
android:versionName="0.0.1"
|
android:versionName="0.0.1"
|
||||||
android:versionCode="1628121202"
|
android:versionCode="1628239323"
|
||||||
android:installLocation="auto">
|
android:installLocation="auto">
|
||||||
|
|
||||||
<supports-screens android:largeScreens="true" android:normalScreens="true" android:anyDensity="true" android:smallScreens="true"/>
|
<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 class MediaService extends Service {
|
||||||
public static final String TAG = "MediaService";
|
public static final String TAG = "MediaService";
|
||||||
|
|
||||||
private MediaSessionCompat mSession;
|
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 class MediaSessionCallback extends MediaSessionCompat.Callback {
|
||||||
private Context mContext;
|
private Context mContext;
|
||||||
@ -66,6 +92,7 @@ public class MediaService extends Service {
|
|||||||
MediaSessionCompat.FLAG_HANDLES_QUEUE_COMMANDS |
|
MediaSessionCompat.FLAG_HANDLES_QUEUE_COMMANDS |
|
||||||
MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
|
MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
|
||||||
mSession.setCallback(new MediaSessionCallback(this));
|
mSession.setCallback(new MediaSessionCallback(this));
|
||||||
|
mPBuilder = new PlaybackStateCompat.Builder();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -30,6 +30,7 @@ set(SRCS_base
|
|||||||
models/downloadmodel.cpp
|
models/downloadmodel.cpp
|
||||||
models/errorlogmodel.cpp
|
models/errorlogmodel.cpp
|
||||||
models/podcastsearchmodel.cpp
|
models/podcastsearchmodel.cpp
|
||||||
|
mediasessionclient.cpp
|
||||||
mpris2/mpris2.cpp
|
mpris2/mpris2.cpp
|
||||||
powermanagementinterface.cpp
|
powermanagementinterface.cpp
|
||||||
sync/sync.cpp
|
sync/sync.cpp
|
||||||
|
@ -23,6 +23,7 @@
|
|||||||
#include "models/errorlogmodel.h"
|
#include "models/errorlogmodel.h"
|
||||||
#include "powermanagementinterface.h"
|
#include "powermanagementinterface.h"
|
||||||
#include "settingsmanager.h"
|
#include "settingsmanager.h"
|
||||||
|
#include "mediasessionclient.h"
|
||||||
|
|
||||||
class AudioManagerPrivate
|
class AudioManagerPrivate
|
||||||
{
|
{
|
||||||
@ -73,6 +74,8 @@ AudioManager::AudioManager(QObject *parent)
|
|||||||
|
|
||||||
connect(this, &AudioManager::logError, &ErrorLogModel::instance(), &ErrorLogModel::monitorErrorMessages);
|
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
|
// Check if an entry was playing when the program was shut down and restore it
|
||||||
if (DataManager::instance().lastPlayingEntry() != QStringLiteral("none")) {
|
if (DataManager::instance().lastPlayingEntry() != QStringLiteral("none")) {
|
||||||
setEntry(DataManager::instance().getEntry(DataManager::instance().lastPlayingEntry()));
|
setEntry(DataManager::instance().getEntry(DataManager::instance().lastPlayingEntry()));
|
||||||
|
@ -8,9 +8,36 @@
|
|||||||
#include "audiomanager.h"
|
#include "audiomanager.h"
|
||||||
|
|
||||||
#include <QtAndroid>
|
#include <QtAndroid>
|
||||||
|
#include <qDebug>
|
||||||
|
|
||||||
MediaSessionClient::MediaSessionClient(QObject *parent)
|
MediaSessionClient::MediaSessionClient(AudioManager *audioPlayer, QObject *parent)
|
||||||
: 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
|
#pragma once
|
||||||
|
|
||||||
class MediaSessionClient
|
#include <QObject>
|
||||||
|
|
||||||
|
class AudioManager;
|
||||||
|
|
||||||
|
class MediaSessionClient : public QObject
|
||||||
{
|
{
|
||||||
public:
|
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