0
我試圖錄制來自手機的音頻,而不是使用麥克風,當然使用MediaRecorder。現在我看到有一種方法可以讓我設置音頻源mediaRecorder.setAudioSource()
。它具有我可以使用的常數,如攝像機,麥克風,默認等。我想知道默認設置是否允許我記錄電話系統中播放的任何內容?還是有另一種方式?無論什麼可以幫助,提前感謝。如何在遊戲/應用程序中記錄或者從手機發生的任何事情,而不是通過麥克風錄音?
我試圖錄制來自手機的音頻,而不是使用麥克風,當然使用MediaRecorder。現在我看到有一種方法可以讓我設置音頻源mediaRecorder.setAudioSource()
。它具有我可以使用的常數,如攝像機,麥克風,默認等。我想知道默認設置是否允許我記錄電話系統中播放的任何內容?還是有另一種方式?無論什麼可以幫助,提前感謝。如何在遊戲/應用程序中記錄或者從手機發生的任何事情,而不是通過麥克風錄音?
有完整的代碼錄音並保存文件在內存中
public class AudioRecord extends AppCompatActivity
{
Button play, stop, record;
//private MediaRecorder myAudioRecorder;
//private String outputFile = null;
File audiofile = null;
//MediaRecorder recorder = null;
Uri fileUri = null;
Chronometer myChronometer;
private MediaRecorder mRecorder = null;
private static final String LOG_TAG = "AudioRecordTest";
//private static String mFileName = null;
private MediaPlayer mPlayer = null;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.record_audio);
myChronometer = (Chronometer) findViewById(R.id.chronometer);
play = (Button) findViewById(R.id.buttonPlay);
stop = (Button) findViewById(R.id.buttonStop);
record = (Button) findViewById(R.id.buttonRecord);
stop.setEnabled(false);
play.setEnabled(false);
long current = System.currentTimeMillis();
File sampleDir = Environment.getExternalStorageDirectory();
try {
audiofile = File.createTempFile("sound"+current, ".3gp", sampleDir);
} catch (IOException e) {
Log.e("AAA", "sdcard access error");
return;
}
record.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
myChronometer.start();
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setOutputFile(audiofile.getAbsolutePath());
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
try
{
mRecorder.prepare();
}
catch(IOException e)
{
Log.e(LOG_TAG, "prepare() failed");
}
mRecorder.start();
record.setEnabled(false);
stop.setEnabled(true);
Toast.makeText(getApplicationContext(), "Recording started", Toast.LENGTH_LONG).show();
}
});
stop.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
mRecorder.stop();
mRecorder.release();
mRecorder = null;
stop.setEnabled(false);
play.setEnabled(true);
addRecordingToMediaLibrary();
Toast.makeText(getApplicationContext(), "Audio recorded successfully", Toast.LENGTH_LONG).show();
myChronometer.stop();
myChronometer.setBase(SystemClock.elapsedRealtime());
}
});
play.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) throws IllegalArgumentException, SecurityException, IllegalStateException
{
Intent data = new Intent();
// data.putExtra("result",mFileName);
data.setData(fileUri);
//data.setData(Uri.fromFile(new File(mFileName)));
setResult(RESULT_OK, data);
finish();
// startPlaying();
}
});
}
protected void addRecordingToMediaLibrary() {
ContentValues values = new ContentValues(4);
long current = System.currentTimeMillis();
values.put(MediaStore.Audio.Media.TITLE, "audio" + audiofile.getName());
values.put(MediaStore.Audio.Media.DATE_ADDED, (int) (current/1000));
values.put(MediaStore.Audio.Media.MIME_TYPE, "audio/3gpp");
values.put(MediaStore.Audio.Media.DATA, audiofile.getAbsolutePath());
ContentResolver contentResolver = getContentResolver();
Uri base = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Uri newUri = contentResolver.insert(base, values);
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, newUri));
M.L("Added File " + newUri);
//Toast.makeText(this, "Added File " + newUri, Toast.LENGTH_LONG).show();
fileUri=newUri;
}
// Record audio XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/toolbar"></include>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/imageView"
android:layout_alignParentTop="true"
android:background="#050505"
android:orientation="vertical"
android:padding="20dp">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal"
android:drawableLeft="@drawable/mix_record"
android:gravity="center"
android:text=" Recoding... "
android:textColor="@color/white"
android:textSize="18sp"/>
<Chronometer
android:id="@+id/chronometer"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:textColor="@color/white"
android:textSize="60sp"
/>
</LinearLayout>
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/linearLayout5"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#171A1D"
android:src="@drawable/a_microphone"/>
<LinearLayout
android:id="@+id/linearLayout5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#9b9b9b"
android:gravity="center"
android:orientation="horizontal"
android:padding="16dp">
<Button
android:id="@+id/buttonRecord"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginRight="7dp"
android:text="Record"/>
<Button
android:id="@+id/buttonStop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/button"
android:layout_centerHorizontal="true"
android:text="Stop"/>
<Button
android:id="@+id/buttonPlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/button2"
android:layout_marginLeft="7dp"
android:text="send"/>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
感謝,但沒有回答我的問題。我知道有一個完整的代碼,什麼不是,我問的是如何將音頻記錄在手機中,而不是通過麥克風錄音。如錄製屏幕活動,在屏幕上錄製遊戲。更簡單一些,就是手機中播放的音頻。 –