Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions apps/common-app/src/examples/AudioFile/AudioFile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,15 @@ const AudioFile: FC = () => {
'remotePlay',
() => {
AudioPlayer.play();
setIsPlaying(true);
}
);

const remotePauseSubscription = AudioManager.addSystemEventListener(
'remotePause',
() => {
AudioPlayer.pause();
setIsPlaying(false);
}
);

Expand Down
3 changes: 1 addition & 2 deletions apps/fabric-example/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
<uses-permission android:name="android.permission.READ_MEDIA_AUDIO"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>

<application
Expand All @@ -16,7 +15,7 @@
android:allowBackup="false"
android:theme="@style/AppTheme"
android:supportsRtl="true">
<service android:stopWithTask="true" android:name="com.swmansion.audioapi.system.MediaNotificationManager$NotificationService" android:foregroundServiceType="mediaPlayback|dataSync" />
<service android:stopWithTask="true" android:name="com.swmansion.audioapi.system.MediaNotificationManager$AudioForegroundService" android:foregroundServiceType="mediaPlayback" />
<activity
android:name=".MainActivity"
android:label="@string/app_name"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ AudioPlayer::AudioPlayer(
sampleRate_(sampleRate),
channelCount_(channelCount) {
isInitialized_ = openAudioStream();

nativeAudioPlayer_ = jni::make_global(NativeAudioPlayer::create());
}

bool AudioPlayer::openAudioStream() {
Expand Down Expand Up @@ -47,6 +49,7 @@ bool AudioPlayer::openAudioStream() {

bool AudioPlayer::start() {
if (mStream_) {
nativeAudioPlayer_->start();
auto result = mStream_->requestStart();
return result == oboe::Result::OK;
}
Expand All @@ -56,6 +59,7 @@ bool AudioPlayer::start() {

void AudioPlayer::stop() {
if (mStream_) {
nativeAudioPlayer_->stop();
mStream_->requestStop();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
#include <functional>
#include <memory>

#include <audioapi/android/core/NativeAudioPlayer.hpp>

namespace audioapi {

using namespace oboe;

class AudioContext;
class AudioBus;
class NativeAudioPlayer;

class AudioPlayer : public AudioStreamDataCallback, AudioStreamErrorCallback {
public:
Expand All @@ -19,13 +22,18 @@ class AudioPlayer : public AudioStreamDataCallback, AudioStreamErrorCallback {
float sampleRate,
int channelCount);

~AudioPlayer() override {
nativeAudioPlayer_.release();
cleanup();
}

bool start();
void stop();
bool resume();
void suspend();
void cleanup();

bool isRunning() const;
[[nodiscard]] bool isRunning() const;

DataCallbackResult onAudioReady(
AudioStream *oboeStream,
Expand All @@ -44,6 +52,8 @@ class AudioPlayer : public AudioStreamDataCallback, AudioStreamErrorCallback {
int channelCount_;

bool openAudioStream();

jni::global_ref<NativeAudioPlayer> nativeAudioPlayer_;
};

} // namespace audioapi
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#pragma once


#include <fbjni/fbjni.h>
#include <react/jni/CxxModuleWrapper.h>
#include <react/jni/JMessageQueueThread.h>
#include <memory>
#include <utility>
#include <unordered_map>

namespace audioapi {

using namespace facebook;
using namespace react;

class NativeAudioPlayer : public jni::JavaClass<NativeAudioPlayer> {
public:
static auto constexpr kJavaDescriptor =
"Lcom/swmansion/audioapi/core/NativeAudioPlayer;";

static jni::local_ref<NativeAudioPlayer> create() {
return newInstance();
}

void start() {
static const auto method = javaClassStatic()->getMethod<void()>("start");
method(self());
}

void stop() {
static const auto method = javaClassStatic()->getMethod<void()>("stop");
method(self());
}
};

} // namespace audioapi
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.swmansion.audioapi.core

import com.facebook.common.internal.DoNotStrip
import com.swmansion.audioapi.system.MediaSessionManager

@DoNotStrip
class NativeAudioPlayer {
private var sourceNodeId: String? = null

@DoNotStrip
fun start() {
this.sourceNodeId = MediaSessionManager.attachAudioPlayer(this)
MediaSessionManager.startForegroundServiceIfNecessary()
}

@DoNotStrip
fun stop() {
this.sourceNodeId?.let {
MediaSessionManager.detachAudioPlayer(it)
this.sourceNodeId = null
}
MediaSessionManager.stopForegroundServiceIfNecessary()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class LockScreenManager(
)

nb.setLargeIcon(bitmap)
mediaNotificationManager.get()?.show(nb, isPlaying)
mediaNotificationManager.get()?.updateNotification(nb, isPlaying)

artworkThread = null
} catch (ex: Exception) {
Expand Down Expand Up @@ -167,14 +167,14 @@ class LockScreenManager(

mediaSession.get()?.setMetadata(md.build())
mediaSession.get()?.setActive(true)
mediaNotificationManager.get()?.show(nb, isPlaying)
mediaNotificationManager.get()?.updateNotification(nb, isPlaying)
}

fun resetLockScreenInfo() {
if (artworkThread != null && artworkThread!!.isAlive) artworkThread!!.interrupt()
artworkThread = null

mediaNotificationManager.get()?.hide()
mediaNotificationManager.get()?.cancelNotification()
mediaSession.get()?.setActive(false)
}

Expand Down Expand Up @@ -235,7 +235,7 @@ class LockScreenManager(
updateNotificationMediaStyle()

if (mediaSession.get()?.isActive == true) {
mediaNotificationManager.get()?.show(nb, isPlaying)
mediaNotificationManager.get()?.updateNotification(nb, isPlaying)
}
}

Expand Down
Loading