先上效果图:
图中的效果是SensorTile录音,通过BLE传输到手机实时播出,可以开始和停止,声音请自行脑补。
前面讲的几个例子都是基于例程中FeatureListActivity类的,这样小型的修改是可以的,但是如果要做一个大型项目那就行不通了。
1. 烧录BM2固件,使用BlueMS获取BlueVoice的LICENSE。
2. 打开ST提供的例程,新建AudioActivity.java文件extends AppCompatActivity implements View.OnClickListener。
3. 在AndroidManifest.xml中注册该Activity:
- <activity
- android:name=".AudioActivity"
- android:label="Audio"/>
4. 新建activity_audio.xml的布局文件,里面放置两个按钮:
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/Btn_Play"
- android:text="Play"/>
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/Btn_Stop"
- android:text="Stop"/>
5. 在AudioActivity中写好启动接口:
- public static Intent getStartIntent(Context c, @NonNull Node node) {
- Intent i = new Intent(c, AudioActivity.class);
- i.putExtra(NODE_TAG, node.getTag());
- i.putExtras(NodeContainerFragment.prepareArguments(node));
- return i;
- }
6. 写好FeatureListener,用来处理接收的数据:
- private final Feature.FeatureListener mAudioListener = new Feature.FeatureListener() {
- @Override
- public void onUpdate:(final Feature f, final Feature.Sample sample) {
- if(f.getName().equals("AudioFeature")){
- synchronized (this) {
- mAudioTrack.write(FeatureAudioADPCM.getAudio(sample), 0, sample.data.length);
- }
- }
- }
- };
7. 在AudioActivity类中的onCreate中读取和保存状态,绑定布局文件,初始化mNode 和mAudioTrack如下:
- setContentView(R.layout.activity_audio);
- if (savedInstanceState == null) {
- Intent i = getIntent();
- mNodeContainer = new NodeContainerF,ragment();
- mNodeContainer.setArguments(i.getExtras());
- getFragmentManager().beginTransaction().add(mNodeContainer, NODE_FRAGMENT).commit();
- } else {
- mNodeContainer = (NodeContainerFragment) getFragmentManager().findFragmentByTag(NODE_FRAGMENT);
- }
- String nodeTag = getIntent().getStringExtra(NODE_TAG);
- mNode = Manager.getSharedInstance().getNodeWithTag(nodeTag);
- int playBufSize = AudioTrack.getMinBufferSize(8000, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT);
- if(mAudioTrack==null)
- mAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, 8000,AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT, playBufSize, AudioTrack.MODE_STREAM);
8. 在onStart中注册前面的mAudioListener,使能FeatureAudioADPCM类和FeatureAudioADPCMSync类,开启播放。 - if(!mNode.isEnableNotification(mNode.getFeature(FeatureAudioADPCM.class))) {//Ensure the ADPCM is EnableNotification
- mNode.getFeature(FeatureAudioADPCM.class).addFeatureListener(mAudioListener);
- mNode.enableNotification(mNode.getFeature(FeatureAudioADPCM.class));//EnableNotification ADPCM
- }
- if(!mNode.isEnableNotification(mNode.getFeature(FeatureAudioADPCMSync.class))) {//Ensure the ADPCMSync is EnableNotification
- mNode.enableNotification(mNode.getFeature(FeatureAudioADPCMSync.class));//EnableNotification ADPCMSync
- }
- mAudioTrack.play();
9. 后退函数onBackPressed:
- mNodeContainer.keepConnectionOpen(true);
- super.onBackPressed();
10. 退出函数onStop,停止播放,注销语音类:
- synchronized(this) {
- mAudioTrack.pause();
- mAudioTrack.flush();
- }
- if(mNode.isEnableNotification(mNode.getFeature(FeatureAudioADPCM.class))) {//Ensure the ADPCM is disableNotification
- mNode.getFeature(FeatureAudioADPCM.class).removeFeatureListener(mAudioListener);
- mNode.disableNotification(mNode.getFeature(FeatureAudioADPCM.class));//disableNotification ADPCM
- }
- if(mNode.isEnableNotification(mNode.getFeature(FeatureAudioADPCMSync.class))) {//Ensure the ADPCMSync is EnableNotification
- mNode.disableNotification(mNode.getFeature(FeatureAudioADPCMSync.class));//EnableNotification ADPCMSync
- }
- super.onStop();
11. 按钮响应函数onClick,播放按钮就是onStart中的内容,停止按钮就是onStop中的内容:
- case R.id.Btn_Play:
- //onStart
- break;
- case R.id.Btn_Stop:
- //onStop
- break;
12. 启动该AudioActivity:
可以直接从ScanActivity中启动,也可以从其他类中启动,本例从FeatureListActivity中的按钮中启动。- startActivity(AudioActivity.getStartIntent(this, mNode));//start AudioActivity
好啦,现在全部工作就完成了,后期可以在此基础上进行语音识别,大家敬请期待。
APP:
V1.03.apk
(1.9 MB)
(下载次数: 4, 2017-1-11 14:08 上传)
本帖最后由 lb8820265 于 2017-1-11 17:04 编辑