1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
| package com.lixb.app.rec.outaudio;
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.Context;
import android.content.Intent;
import android.media.AudioAttributes;
import android.media.AudioFormat;
import android.media.AudioPlaybackCaptureConfiguration;
import android.media.AudioRecord;
import android.media.projection.MediaProjection;
import android.media.projection.MediaProjectionManager;
import android.net.Uri;
import android.os.Build;
import android.os.IBinder;
import android.os.ParcelFileDescriptor;
import android.util.Log;
import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* @ClassName MediaProjectionService
* @Description
* @Author lixb
* @Date 2025/2/18 14:36
* @Version 1.0
*/
public class MediaProjectionService extends Service {
public static final int NOTIFICATION_ID = 111;
public static final String CHANNEL_ID = "Media Projection Channel";
public static final String TAG = "MediaProjectionService";
private Recorder mRecorder;
private Uri mSaveUri;
@Override
public void onCreate() {
super.onCreate();
createNotificationChannel();
startForeground(NOTIFICATION_ID,createNotification());
}
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(
CHANNEL_ID,
"Media Projection Channel",
NotificationManager.IMPORTANCE_DEFAULT
);
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(channel);
}
}
private Notification createNotification() {
Intent notificationIntent = new Intent(this, MainActivity.class);
int flags = Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? PendingIntent.FLAG_IMMUTABLE : 0;
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, flags);
return new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("Media Projection Service")
.setContentText("Screen recording is in progress.")
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentIntent(pendingIntent)
.build();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@SuppressLint("MissingPermission")
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
int resultCode = intent.getIntExtra("code", -1);
Intent data = intent.getParcelableExtra("data");
mSaveUri = intent.getParcelableExtra("save_uri");
MediaProjectionManager mpm = (MediaProjectionManager) getSystemService(Context.MEDIA_PROJECTION_SERVICE);
MediaProjection mp = mpm.getMediaProjection(resultCode, data);
Log.e(TAG, "onActivityResult config record >>>" );
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
AudioPlaybackCaptureConfiguration config = new AudioPlaybackCaptureConfiguration.Builder(mp)
.addMatchingUsage(AudioAttributes.USAGE_MEDIA) //少了这句会报错
.build();
AudioFormat format = new AudioFormat.Builder()
.setSampleRate(44100)
.setEncoding(AudioFormat.ENCODING_PCM_16BIT)
.setChannelMask(AudioFormat.CHANNEL_IN_MONO)
.build();
AudioRecord audioRecord = new AudioRecord.Builder().
setAudioFormat(format).setAudioPlaybackCaptureConfig(config).build();
mRecorder = new Recorder(audioRecord);
mRecorder.start();
}
return START_STICKY;
}
private class Recorder extends Thread {
private AudioRecord record;
public Recorder(AudioRecord record) {
this.record=record;
}
@Override
public void run() {
Log.e(TAG, "record thread start >>>" );
int minBufferSize = AudioRecord.getMinBufferSize(44100, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
byte[] buffer = new byte[minBufferSize];
FileOutputStream os = null;
try {
ParcelFileDescriptor pfd = getContentResolver().openFileDescriptor(mSaveUri, "w");
os = new FileOutputStream(pfd.getFileDescriptor());
os.write(new byte[44]); //wav预先填充
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
int audioLen = 0;
if (record.getState() == AudioRecord.STATE_INITIALIZED) {
record.startRecording();
while (record.getRecordingState() == AudioRecord.RECORDSTATE_RECORDING) {
int read = record.read(buffer, 0, buffer.length);
Log.e(TAG, "record audio data "+ read );
if (read > 0) {
// 处理录音数据
if (os != null) {
try {
os.write(buffer, 0, read);
audioLen+=read;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
Log.e(TAG, "audio len = " + audioLen);
if (null != os) {
try {
os.close();
ParcelFileDescriptor pfd = getContentResolver().openFileDescriptor(mSaveUri, "w");
os = new FileOutputStream(pfd.getFileDescriptor());
writeWaveFileHeader(os, audioLen, audioLen + 36, 44100, 1, 44100 * 2);
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Log.e(TAG, "record thread over <<<<<" );
}
public void stopRecord() {
if (null != record) {
record.stop();
record.release();
record = null;
}
}
}
@Override
public void onDestroy() {
super.onDestroy();
stopRecord();
stopForeground(true);
}
private void stopRecord() {
if (null != mRecorder) {
mRecorder.stopRecord();
}
}
}
|