package com.example.sonya import android.annotation.SuppressLint import…
- User
package com.example.sonya
import android.annotation.SuppressLint import android.app.Notification import android.app.NotificationChannel import android.app.NotificationManager import android.app.PendingIntent import android.app.Service import android.content.Intent import android.media.AudioFormat import android.media.AudioRecord import android.media.MediaRecorder import android.os.Build import android.os.IBinder import android.util.Log import androidx.annotation.RequiresApi import java.io.OutputStream import java.net.Socket
class AudioService : Service() {
private val sampleRate = 44100 private var isRecording = false private lateinit var audioRecord: AudioRecord companion object { var isPlayingAudio = false // Общая переменная для отслеживания состояния воспроизведения } override fun onBind(intent: Intent?): IBinder? { return null } @RequiresApi(Build.VERSION_CODES.O) @SuppressLint("ForegroundServiceType") override fun onCreate() { super.onCreate() createNotificationChannel() startForeground(1, createNotification()) } override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { startRecording() return START_STICKY } private fun createNotificationChannel() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { val serviceChannel = NotificationChannel( "AudioServiceChannel", "Audio Service Channel", NotificationManager.IMPORTANCE_DEFAULT ) val manager = getSystemService(NotificationManager::class.java) manager.createNotificationChannel(serviceChannel) } } @RequiresApi(Build.VERSION_CODES.O) private fun createNotification(): Notification { val notificationIntent = Intent(this, MainActivity::class.java) val pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_IMMUTABLE) return Notification.Builder(this, "AudioServiceChannel") .setContentTitle("Audio Service") .setContentText("Recording audio in background") .setSmallIcon(R.drawable.ic_launcher_foreground) .setContentIntent(pendingIntent) .build() } @SuppressLint("MissingPermission") private fun startRecording() { isRecording = true Thread { val bufferSize = AudioRecord.getMinBufferSize(sampleRate, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT) audioRecord = AudioRecord(MediaRecorder.AudioSource.MIC, sampleRate, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, bufferSize) audioRecord.startRecording() try { val socket = Socket("192.168.0.105", 6543) val outputStream: OutputStream = socket.getOutputStream() val buffer = ByteArray(bufferSize) while (isRecording) { // Приостанавливаем запись, если идет воспроизведение аудио if (isPlayingAudio) { Log.d("AudioService", "Recording paused: Audio is playing") Thread.sleep(100) // Ждем 100 мс перед следующей проверкой continue } val read = audioRecord.read(buffer, 0, buffer.size) outputStream.write(buffer, 0, read) } outputStream.close() socket.close() } catch (e: Exception) { Log.e("AudioService", "Error recording audio", e) } finally { audioRecord.stop() audioRecord.release() } }.start() } private fun stopRecording() { isRecording = false } override fun onDestroy() { super.onDestroy() stopRecording() } - Assistant
- User
почему когда я возвращаюсь в приложение из фона то методы onActivationReceived и onDeactivationReceived не работают, то есть кнопка не меняет цвет?
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.