Я сделал приложение для Android, которое передает голос из локальной сети Wi-Fi. Для прослушивания и потоковой передачи я использую Opus C API в JNI для декодирования и OpenSL Audio для чтения.
Я звоню в JNI из Сервиса, вот так.
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;
}
Это работает очень хорошо, когда я нажимаю кнопку, вызывается эта служба, и я получаю звук со своего смартфона Android.
НО, когда я блокирую экран своего смартфона, звук как бы "приглушен", когда я разблокирую, звук воспроизводится. Только на 6.0.X не на 5.0.X
Итак, я сначала подумал, что сетевой пакет был остановлен, я сделал отладку, и мой смартфон продолжает получать сетевые пакеты.
Моя служба работает, просто звук отключен.
Для JNI OpenSL я использую этот фрагмент кода:
Чтобы позвонить в мой 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();
}
И этот вызов:
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);
}
Любые идеи ?! Большое спасибо!!
РЕДАКТИРОВАТЬ: на Android 5.0.1 это работает, на Android 6.0.1 - нет?! Почему ??
РЕДАКТИРОВАТЬ2: Мои разрешения
<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"
РЕДАКТИРОВАТЬ 3: я не записываю, я только слушаю
РЕДАКТИРОВАТЬ 4: После установки разрешений, ЗАПИСАТЬ АУДИО, без изменений, на Android Marshmallow (Samsung S5), когда я блокирую экран, мое приложение отключено!