I made an android app, that streams voice from local wifi network. To listen and stream I use Opus C API in JNI to decode, and OpenSL Audio to read.
I call the JNI from Service, like that.
public class StreamService extends Service {
private NativeReceiver mNativeReceiver;
...
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// Gérer la requête
mNativeReceiver = new NativeReceiver();
mCanPlay = mNativeReceiver.init();
mNativeReceiver.startNativeReceiver();
...
return Service.START_NOT_STICKY;
}
It's works very well, when I click on my Button, this service is called, and I get sound out from my android smartphone.
BUT, when I lock the screen of my smartphone, sound is like "muted", when I unlock sound is playing. Only on 6.0.X not on 5.0.X
So my first thinking was network packet was stopped, I made a debug and my smartphone continue to receive network packets.
My Service running, just sound is muted.
For the JNI OpenSL, I use this snippet of code:
To call my JNI:
public void startNativeReceiver() {
new Thread(new Runnable() {
@Override
public void run() {
// set priority
android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);
start();
}
}).start();
}
And that call this:
const int queue_elements_num = 20;
const int start_after = 4;
SpscCircularQueue *queue = NULL;
OpusDecoder *opus_dec = NULL;
uint32_t samplerate = 24000;
float frame_length = 20;
bool player_initialized = false;
bool player_started = false;
int frames_in_queue = 0;
while (!rcv_th_terminate) {
ssize_t rcv_len = read(udp_sock, rcv_buf, sizeof(rcv_buf));
if (rcv_len <= 0) {
__android_log_print(ANDROID_LOG_WARN, "SMPlayer",
"Failed to receive UDP data");
continue;
}
__android_log_print(ANDROID_LOG_INFO, "SMPlayer",
"reading");
if (!player_initialized) {
if (rcv_buf[0] != 0x01) {
__android_log_print(ANDROID_LOG_ERROR, "SMPlayer",
"Unsupported version of SMPlayer "
"streaming protocol");
return NULL;
}
/* initialize decoder */
int opus_err = 0;
opus_dec = opus_decoder_create(samplerate, 1, &opus_err);
if (!opus_dec) {
__android_log_print(ANDROID_LOG_ERROR, "SMPlayer",
"unable to initialize OPUS decoder, "
"error code - %d", opus_err);
return NULL;
}
/* initialize circular queue */
uint32_t frame_buf_size = samplerate / 1000 * frame_length * 2;
queue = spsc_circular_queue_alloc(queue_elements_num, frame_buf_size);
if (!queue) {
__android_log_print(ANDROID_LOG_ERROR, "SMPlayer",
"unable to allocate SPSC Circular Queue");
opus_decoder_destroy(opus_dec);
return NULL;
}
/* initialize the play and start the playback */
osl_player_init(queue, samplerate);
player_initialized = true;
} else {
int dec_samples = opus_decode(opus_dec, rcv_buf + 2, rcv_len - 2,
(int16_t *)dec_buf, sizeof(dec_buf), 0);
if (dec_samples <= 0) {
__android_log_print(ANDROID_LOG_ERROR, "SMPlayer",
"failed to decode the data");
osl_player_terminate();
spsc_circular_queue_free(queue);
opus_decoder_destroy(opus_dec);
exit(EXIT_FAILURE);
}
while (!spsc_circular_queue_push(queue, dec_buf) && !rcv_th_terminate)
usleep((useconds_t)(frame_length * 1000.0));
if (!player_started) {
frames_in_queue++;
if (frames_in_queue == start_after) {
osl_player_jumpstart(queue);
player_started = true;
}
}
}
}
osl_player_terminate();
if (empty_buf_cnt)
__android_log_print(ANDROID_LOG_DEBUG, "SMPlayer",
"Empty buffers played: %" PRIu64, empty_buf_cnt);
spsc_circular_queue_free(queue);
opus_decoder_destroy(opus_dec);
}
Any ideas ?! Thanks a looot !!
EDIT: On Android 5.0.1 it's works, on Android 6.0.1 it does't ?! Why ??
EDIT2: My permissions
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<application
android:allowBackup="true"
EDIT 3 : I don't record, I only listen
EDIT 4: After put permissions, RECORD AUDIO, no changes, on Android Marshmallow (Samsung S5) when I lock the screen, my app is like muted!
I think your permission expires in 6.0 and higher android versions, so you check your permissions if your permission is in Dangerous permission then give it dynamically.