Android 11/R - 从后台启动的前台服务开始录制音频/视频(闹钟接收器)
创始人
2024-08-12 18:01:01
0

要在Android 11/R中从后台启动前台服务并开始录制音频/视频,可以使用以下解决方案。

首先,在AndroidManifest.xml文件中添加必要的权限和服务声明。请确保包含以下权限:




然后,在AndroidManifest.xml文件中添加服务声明:


接下来,创建一个名为RecordingService的服务类。在此服务类中,我们将使用MediaProjectionManager和MediaRecorder来录制音频/视频。示例代码如下:

import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.media.MediaRecorder;
import android.media.projection.MediaProjection;
import android.media.projection.MediaProjectionManager;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

import androidx.core.app.NotificationCompat;

import java.io.IOException;

public class RecordingService extends Service {
    private static final String TAG = "RecordingService";

    private MediaProjectionManager mMediaProjectionManager;
    private MediaProjection mMediaProjection;
    private MediaRecorder mMediaRecorder;

    private boolean isRecording = false;

    private final IBinder mBinder = new LocalBinder();

    public class LocalBinder extends Binder {
        RecordingService getService() {
            return RecordingService.this;
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mMediaProjectionManager = (MediaProjectionManager) getSystemService(MEDIA_PROJECTION_SERVICE);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (intent != null && intent.getAction() != null) {
            if (intent.getAction().equals("START_RECORDING")) {
                startForegroundService();
                startRecording();
            } else if (intent.getAction().equals("STOP_RECORDING")) {
                stopRecording();
                stopForeground(true);
                stopSelf();
            }
        }
        return START_NOT_STICKY;
    }

    private void startForegroundService() {
        Intent notificationIntent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

        Notification notification = new NotificationCompat.Builder(this, "CHANNEL_ID")
                .setContentTitle("Recording Service")
                .setContentText("Recording in progress")
                .setSmallIcon(R.drawable.ic_notification)
                .setContentIntent(pendingIntent)
                .build();

        startForeground(1, notification);
    }

    private void startRecording() {
        mMediaProjection = mMediaProjectionManager.getMediaProjection(RESULT_OK, mMediaProjectionCallback);
        mMediaRecorder = new MediaRecorder();
        mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
        mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
        mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
        mMediaRecorder.setOutputFile("path_to_save_recording");
        try {
            mMediaRecorder.prepare();
        } catch (IOException e) {
            Log.e(TAG, "prepare() failed: " + e.getMessage());
        }
        mMediaRecorder.start();
        isRecording = true;
        Log.d(TAG, "Recording started");
    }

    private void stopRecording() {
        if (mMediaRecorder != null && isRecording) {
            mMediaRecorder.stop();
            mMediaRecorder.reset();
            mMediaRecorder.release();
            mMediaRecorder = null;
            mMediaProjection.stop();
            mMediaProjection = null;
            isRecording = false;
            Log.d(TAG, "Recording stopped");
        }
    }

    private final MediaProjection.Callback mMediaProjectionCallback = new MediaProjection.Callback() {
        @Override
        public void onStop() {
            stopRecording();
        }
    };
}

最后,在需要启动录制的地方(例如,闹钟接收器),使用以下代码来启动RecordingService并开始录制:

Intent startIntent = new Intent(context, RecordingService.class);
startIntent.setAction("START_RECORDING");
ContextCompat.startForegroundService(context, startIntent);

要停止录制,使用以下代码:

Intent stopIntent = new Intent(context, RecordingService.class);
stop

相关内容

热门资讯

安装apache-beam==... 出现此错误可能是因为用户的Python版本太低,而apache-beam==2.34.0需要更高的P...
避免在粘贴双引号时向VS 20... 在粘贴双引号时向VS 2022添加反斜杠的问题通常是由于编辑器的自动转义功能引起的。为了避免这个问题...
Android Recycle... 要在Android RecyclerView中实现滑动卡片效果,可以按照以下步骤进行操作:首先,在项...
omi系统和安卓系统哪个好,揭... OMI系统和安卓系统哪个好?这个问题就像是在问“苹果和橘子哪个更甜”,每个人都有自己的答案。今天,我...
原生ios和安卓系统,原生对比... 亲爱的读者们,你是否曾好奇过,为什么你的iPhone和安卓手机在操作体验上有着天壤之别?今天,就让我...
安装了Anaconda之后找不... 在安装Anaconda后,如果找不到Jupyter Notebook,可以尝试以下解决方法:检查环境...
Android - 无法确定任... 这个错误通常发生在Android项目中,表示编译Debug版本的Java代码时出现了依赖关系问题。下...
Android - NDK 预... 在Android NDK的构建过程中,LOCAL_SRC_FILES只能包含一个项目。如果需要在ND...
Akka生成Actor问题 在Akka框架中,可以使用ActorSystem对象生成Actor。但是,当我们在Actor类中尝试...
Agora-RTC-React... 出现这个错误原因是因为在 React 组件中使用,import AgoraRTC from “ago...