android Qの新機能キャプチャで再生中の曲を録音できるアプリを使っていますができません。ファイルはできていて、エラーも表示されませんが録音できていないです。なぜでしょうか?
Java
1public class MainActivity extends AppCompatActivity { 2 3 public MediaProjectionManager mMediaProjectionManager; 4 public MediaProjection mMediaProjection; 5 public AudioPlaybackCaptureConfiguration mAudio; 6 public AudioRecord mRecord; 7 // 定数 8 private int count = 0; 9 10 private static final int AUDIO_SAMPLE_FREQ = 44100; 11 private static final int CHANNEL_CONFIG = AudioFormat.CHANNEL_IN_MONO; 12 private static final int AUDIO_FORMAT = AudioFormat.ENCODING_PCM_16BIT; 13 14 private static final int AUDIO_BUFFER_SIZE = AudioRecord.getMinBufferSize( 15 AUDIO_SAMPLE_FREQ, AudioFormat.CHANNEL_IN_MONO, 16 AudioFormat.ENCODING_PCM_16BIT); 17 private static final int FRAME_BUFFER_SIZE = AUDIO_BUFFER_SIZE / 2; 18 private static final int BUFFER_SIZE_FACTOR = 2; 19 private short data[] = new short[FRAME_BUFFER_SIZE]; 20 Context context; 21 22 23 @Override 24 protected void onCreate(Bundle savedInstanceState) { 25 super.onCreate(savedInstanceState); 26 setContentView(R.layout.activity_main); 27 mMediaProjectionManager = (MediaProjectionManager) getSystemService(MEDIA_PROJECTION_SERVICE); 28 29 final File file = new File(getExternalFilesDir(null).getPath() + "/audio.wav"); 30 31 32 33 Button startButton = findViewById(R.id.start_button); 34 Button stopButton = findViewById(R.id.stop_button); 35 final TextView mRecordText = findViewById(R.id.read); 36 37 if(mMediaProjectionManager != null) { 38 mMediaProjectionManager.createScreenCaptureIntent(); 39 } 40 if( mAudio != null) { 41 mAudio = new AudioPlaybackCaptureConfiguration.Builder(mMediaProjection).addMatchingUsage(AudioAttributes.USAGE_MEDIA).build(); 42 mRecord = new AudioRecord.Builder().setAudioPlaybackCaptureConfig(mAudio).build(); 43 } 44 45 mRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, 46 AUDIO_SAMPLE_FREQ, AudioFormat.CHANNEL_IN_MONO, 47 AudioFormat.ENCODING_PCM_16BIT, AUDIO_BUFFER_SIZE); 48 mRecord.setRecordPositionUpdateListener(new AudioRecord.OnRecordPositionUpdateListener() { 49 // フレームごとの処理 50 @Override 51 public void onPeriodicNotification(AudioRecord mRecord) { 52 count++; 53 mRecord.read(data, 0, FRAME_BUFFER_SIZE); 54 } 55 @Override 56 public void onMarkerReached(AudioRecord mRecord) { 57 } 58 }); 59 60 mRecord.setPositionNotificationPeriod(FRAME_BUFFER_SIZE); 61 62 // 録音開始 63 64 startButton.setOnClickListener(new View.OnClickListener() { 65 66 @Override 67 public void onClick(View v) { 68 mRecord = new AudioRecord(MediaRecorder.AudioSource.DEFAULT, AUDIO_SAMPLE_FREQ, 69 CHANNEL_CONFIG, AUDIO_FORMAT, BUFFER_SIZE_FACTOR); 70 71 mRecord.startRecording(); 72 73 mRecord.read(data, 0, FRAME_BUFFER_SIZE); 74 75 try { 76 OutputStream os = openFileOutput("test.txt", MODE_PRIVATE); 77 OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8"); 78 PrintWriter pw = new PrintWriter(osw); 79 pw.append("mRecord"); 80 pw.close(); 81 osw.close(); 82 os.close(); 83 } catch (IOException e) { 84 e.printStackTrace(); 85 mRecordText.setText(String.valueOf(mRecord)); 86 } 87 } 88 }); 89 90 stopButton.setOnClickListener(new View.OnClickListener() { 91 92 @Override 93 public void onClick(View v) { 94 // 録音停止 95 mRecord.stop(); 96 97 } 98 }); 99 } 100 101 @Override 102 public void onActivityResult(int requestCode, int resultCode, Intent data) { 103 if (requestCode == 1 && resultCode == Activity.RESULT_OK) { 104 mMediaProjection = mMediaProjectionManager.getMediaProjection(resultCode, data); 105 this.finish(); 106 } 107 } 108 109 public void onDestroy(){ 110 super.onDestroy(); 111 mRecord.release(); 112 } 113}
あなたの回答
tips
プレビュー