推流直播设置静音

pull/107/head
frank 7 years ago
parent c84c32197b
commit 1dcc4e6c22
  1. 1
      Live/src/main/cpp/live.c
  2. 23
      Live/src/main/java/com/frank/live/Push/AudioPusher.java
  3. 9
      Live/src/main/java/com/frank/live/Push/LivePusher.java
  4. 21
      app/src/main/java/com/frank/ffmpeg/activity/LiveActivity.java
  5. 12
      app/src/main/res/layout/activity_live.xml
  6. 2
      app/src/main/res/values/strings.xml

@ -117,7 +117,6 @@ void *push_thread(void * args){
goto end; goto end;
} }
RTMPPacket_Free(packet); RTMPPacket_Free(packet);
LOGI("RTMP_SendPacket success...");
} }
pthread_mutex_unlock(&mutex); pthread_mutex_unlock(&mutex);
} }

@ -17,6 +17,7 @@ public class AudioPusher extends Pusher {
private boolean isPushing; private boolean isPushing;
private int minBufferSize; private int minBufferSize;
private LiveUtil liveUtil; private LiveUtil liveUtil;
private boolean isMute;
AudioPusher(AudioParam audioParam, LiveUtil liveUtil){ AudioPusher(AudioParam audioParam, LiveUtil liveUtil){
this.liveUtil = liveUtil; this.liveUtil = liveUtil;
@ -65,16 +66,26 @@ public class AudioPusher extends Pusher {
public void run() { public void run() {
super.run(); super.run();
audioRecord.startRecording(); audioRecord.startRecording();
while (isPushing){ while (isPushing){//处于推流状态
byte[] audioBuffer = new byte[minBufferSize]; if(!isMute){//如果不静音,才推音频流
int length = audioRecord.read(audioBuffer, 0, audioBuffer.length); byte[] audioBuffer = new byte[minBufferSize];
if(length > 0){ int length = audioRecord.read(audioBuffer, 0, audioBuffer.length);
// Log.i("AudioPusher", "is recording..."); if(length > 0){
liveUtil.pushAudioData(audioBuffer, length); // Log.i("AudioPusher", "is recording...");
liveUtil.pushAudioData(audioBuffer, length);
}
} }
} }
audioRecord.stop(); audioRecord.stop();
} }
} }
/**
* 设置静音
* @param isMute 是否静音
*/
void setMute(boolean isMute){
this.isMute = isMute;
}
} }

@ -59,4 +59,13 @@ public class LivePusher {
audioPusher.release(); audioPusher.release();
liveUtil.release(); liveUtil.release();
} }
/**
* 设置静音
* @param isMute 是否静音
*/
public void setMute(boolean isMute){
audioPusher.setMute(isMute);
}
} }

@ -66,6 +66,7 @@ public class LiveActivity extends AppCompatActivity implements View.OnClickListe
private void initView(){ private void initView(){
findViewById(R.id.btn_swap).setOnClickListener(this); findViewById(R.id.btn_swap).setOnClickListener(this);
((ToggleButton)findViewById(R.id.btn_live)).setOnCheckedChangeListener(this); ((ToggleButton)findViewById(R.id.btn_live)).setOnCheckedChangeListener(this);
((ToggleButton)findViewById(R.id.btn_mute)).setOnCheckedChangeListener(this);
SurfaceView surface_camera = (SurfaceView) findViewById(R.id.surface_camera); SurfaceView surface_camera = (SurfaceView) findViewById(R.id.surface_camera);
surfaceHolder = surface_camera.getHolder(); surfaceHolder = surface_camera.getHolder();
} }
@ -87,17 +88,27 @@ public class LiveActivity extends AppCompatActivity implements View.OnClickListe
@Override @Override
public void onClick(View v) { public void onClick(View v) {
if(v.getId() == R.id.btn_swap){ if(v.getId() == R.id.btn_swap){//切换摄像头
livePusher.switchCamera(); livePusher.switchCamera();
} }
} }
@Override @Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){ switch (buttonView.getId()){
livePusher.startPush(LIVE_URL, this); case R.id.btn_live://开始/停止直播
}else { if(isChecked){
livePusher.stopPush(); livePusher.startPush(LIVE_URL, this);
}else {
livePusher.stopPush();
}
break;
case R.id.btn_mute://设置静音
Log.i(TAG, "isChecked=" + isChecked);
livePusher.setMute(isChecked);
break;
default:
break;
} }
} }

@ -15,7 +15,8 @@
android:id="@+id/btn_swap" android:id="@+id/btn_swap"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/swap"/> android:text="@string/swap"
android:layout_marginTop="20dp"/>
<ToggleButton <ToggleButton
android:id="@+id/btn_live" android:id="@+id/btn_live"
@ -25,4 +26,13 @@
android:textOff="@string/start" android:textOff="@string/start"
android:layout_centerVertical="true"/> android:layout_centerVertical="true"/>
<ToggleButton
android:id="@+id/btn_mute"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="@string/sound"
android:textOff="@string/mute"
android:layout_marginTop="20dp"
android:layout_alignParentEnd="true"/>
</RelativeLayout> </RelativeLayout>

@ -35,5 +35,7 @@
<string name="swap">切换</string> <string name="swap">切换</string>
<string name="start">开始</string> <string name="start">开始</string>
<string name="stop">停止</string> <string name="stop">停止</string>
<string name="mute">静音</string>
<string name="sound">声音</string>
</resources> </resources>

Loading…
Cancel
Save