我正在尝试通过TCP将音频从Android设备的麦克风传输到服务器。问题是我在控制台上出错了。已建立TCP连接但未发送音频数据。
我意识到这可能是因为编解码器选择不当,因为有些人需要能够在流中寻找不可能的。我实际上可以使用任何有效的编解码器,但我读到MediaRecorder.OutputFormat.RAW_AMR和MediaRecorder.AudioEncoder.AMR_NB是流媒体的最佳组合。如果有更好的选择,请提出另一种选择。
以下是我在日志中看到的内容:
11-06 11:09:27.276 22983-22983/se.jensolsson.test.test D/ViewRootImpl@5ed8717[MainActivity]: ViewPostImeInputStage processPointer 0
11-06 11:09:27.355 22983-22983/se.jensolsson.test.test D/ViewRootImpl@5ed8717[MainActivity]: ViewPostImeInputStage processPointer 1
11-06 11:09:27.387 22983-25466/se.jensolsson.test.test I/MediaRecorderJNI: setup
11-06 11:09:27.394 22983-25466/se.jensolsson.test.test I/MediaRecorderJNI: setAudioSource(1)
11-06 11:09:27.397 22983-25466/se.jensolsson.test.test I/MediaRecorderJNI: setAudioEncoder(1)
11-06 11:09:27.400 22983-25466/se.jensolsson.test.test I/MediaRecorderJNI: setOutputFile
11-06 11:09:27.400 22983-25466/se.jensolsson.test.test I/MediaRecorderJNI: prepare
11-06 11:09:27.407 22983-25466/se.jensolsson.test.test I/MediaRecorderJNI: start
11-06 11:09:27.408 22983-25466/se.jensolsson.test.test E/MediaRecorder: start failed: -38
11-06 11:09:27.408 22983-25466/se.jensolsson.test.test W/System.err: java.lang.IllegalStateException
11-06 11:09:27.411 22983-25466/se.jensolsson.test.test W/System.err: at android.media.MediaRecorder._start(Native Method)
11-06 11:09:27.411 22983-25466/se.jensolsson.test.test W/System.err: at android.media.MediaRecorder.start(MediaRecorder.java:1170)
11-06 11:09:27.411 22983-25466/se.jensolsson.test.test W/System.err: at se.jensolsson.test.test.MainActivity$1$1.run(MainActivity.java:78)
11-06 11:09:27.411 22983-25466/se.jensolsson.test.test W/System.err: at java.lang.Thread.run(Thread.java:762)
以下是AndroidManifest.xml的相关部分
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
以下是源代码:
public class MainActivity extends AppCompatActivity {
private MediaRecorder mediaRecorder;
private boolean permissionToRecordAccepted;
private static final int REQUEST_RECORD_AUDIO_PERMISSION = 200;
private ParcelFileDescriptor pfd;
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode){
case REQUEST_RECORD_AUDIO_PERMISSION:
permissionToRecordAccepted = grantResults[0] == PackageManager.PERMISSION_GRANTED;
if (!permissionToRecordAccepted ) finish();
break;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.RECORD_AUDIO }, REQUEST_RECORD_AUDIO_PERMISSION);
Button buttonStartRecording = (Button)findViewById(R.id.button_start_recording);
buttonStartRecording.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new Thread(new Runnable() {
@Override
public void run() {
try {
Socket s = new Socket("10.0.83.8", 8888);
ParcelFileDescriptor pfd = ParcelFileDescriptor.fromSocket(s);
MediaRecorder recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(pfd.getFileDescriptor());
try {
recorder.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
recorder.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
});
}
}
我运行的设备是带有Android 7.0的三星Galaxy A5。我在gradle文件中使用minSdkVersion 22和targetSdkVersion 26。
编辑:预装的应用录音机工作正常。所以我不知道麦克风是如何忙碌的。
编辑2:如果我更改为以下内容并保存到文件而不是流,它似乎工作。所以我仍然打赌声音格式和流媒体有问题,因为网络流不支持搜索。如果是这种情况,我应该使用什么格式?
//recorder.setOutputFile(pfd.getFileDescriptor());
File outputFile = File.createTempFile("test", "mp4", getApplicationContext().getCacheDir());
recorder.setOutputFile(outputFile.getPath());
编辑3 没有一个答案是正确的。我现在发现主要问题是我无法将声音数据保存到ParcelFileDescriptor.fromSocket创建的流中
如果我这样做,它会起作用
ParcelFileDescriptor[] mParcelFileDescriptors = ParcelFileDescriptor.createPipe();
final ParcelFileDescriptor mParcelRead = new ParcelFileDescriptor(mParcelFileDescriptors[0]);
ParcelFileDescriptor mParcelWrite = new ParcelFileDescriptor(mParcelFileDescriptors[1]);
然后将流内容发送到服务器。 我不知道是否存在这方面的时间问题,或者它是否会导致某些声音文件格式的损坏,因为我猜标题中的字节可能会随时改变,具体取决于格式。