Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EventListener not working once stop is called. #25

Open
sandeepReddyG opened this issue Aug 26, 2020 · 2 comments
Open

EventListener not working once stop is called. #25

sandeepReddyG opened this issue Aug 26, 2020 · 2 comments

Comments

@sandeepReddyG
Copy link

sandeepReddyG commented Aug 26, 2020

I am using the Recording module to stream data to PC over websocket as below. But the data is getting sent only once. If I press start for the second time , keypress in sent to PC but no voice data sent as the eventlistener not getting called. What am i doing wrong here?

import React from 'react';
import {Component,Text,View,Button, Alert } from 'react-native';
import Recording from 'react-native-recording';

export default class VoiceScreen extends React.Component {
constructor(){
super();
this.state={
status:true
}
}
componentDidMount(){
//await this.checkPermission();
Recording.init({

  bufferSize: 640,

  sampleRate: 16000,

  bitsPerChannel: 16,

  channelsPerFrame: 1,

})

Recording.addRecordingEventListener(this.DataHandler);

ws.onmessage=(m)=>
{
  console.log(m.data +"status "+this.state.status);
  if(m.data=='READY')
  {
    this.startVoice();
  }
  else
  {
    this.stopVoice();
  }
}

}

startVoice(){
Recording.start();
}

stopVoice()
{
Recording.stop();
}

DataHandler=(data)=>{

  console.log(" Sending auido data of len "+data.length*2);
  var pcm_len = data.length*2;
  //data.unshift(pcm_len>>8);
  data.unshift(pcm_len);
  data.unshift(0x03);
  var view = new Uint16Array(data);

  //var view1 = new Uint8Array(view);
  ws.send(view);
  data.pop();
  data.pop();

}

sendVoiceKeyPress(){

cmd = [];
cmd.push(0x04);
cmd.push(0x00);
cmd.push(0x05);
cmd.push(0x00);
cmd.push(0x56);
cmd.push(0x4F);
cmd.push(0x49);
cmd.push(0x43);
cmd.push(0x45);
start_speech = new Uint8Array(cmd);
ws.send(start_speech);

this.setState({status:false});

}

sendVoiceKeyRelease(){
this.stopVoice();
cmd = [];
cmd.push(0x05);
cmd.push(0x00);
cmd.push(0x05)
cmd.push(0x00);
cmd.push(0x56);
cmd.push(0x4F);
cmd.push(0x49);
cmd.push(0x43);
cmd.push(0x45);
stop_speech = new Uint8Array(cmd);
ws.send(stop_speech);
this.setState({status:true});

}

componentWillUnmount() {

//Recording.stop()

}

render() {

return(
    <View>
   {this.state.status?<Button title='start' onPress={()=>this.sendVoiceKeyPress()}/>:<Button title='stop' onPress={()=>this.sendVoiceKeyRelease()}/>}
   </View>
)

}

}

`

@nwatab
Copy link
Contributor

nwatab commented Nov 17, 2020

From my understainding of android code, Recording.init() again to retake after Recording.stop(). Recording.stop() sets audioRecord = null[1], so you have to create AudioRecord instance again by calling Recording.init()[2] before calling Recording.start() again. iOS should behave in the same way.

[1] https://github.com/qiuxiang/react-native-recording/blob/master/lib/android/src/main/java/cn/qiuxiang/react/recording/RecordingModule.java#L111
[2] https://github.com/qiuxiang/react-native-recording/blob/master/lib/android/src/main/java/cn/qiuxiang/react/recording/RecordingModule.java#L82

@MAsif99
Copy link

MAsif99 commented Dec 23, 2024

From my understainding of android code, Recording.init() again to retake after Recording.stop(). Recording.stop() sets audioRecord = null[1], so you have to create AudioRecord instance again by calling Recording.init()[2] before calling Recording.start() again. iOS should behave in the same way.

[1] https://github.com/qiuxiang/react-native-recording/blob/master/lib/android/src/main/java/cn/qiuxiang/react/recording/RecordingModule.java#L111 [2] https://github.com/qiuxiang/react-native-recording/blob/master/lib/android/src/main/java/cn/qiuxiang/react/recording/RecordingModule.java#L82

"Thanks, the three lines you mentioned above resolved my issue."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants