我正在进行直播。我的问题是,当我按下播放按钮时,图像应该变为停止按钮,当我按下该停止按钮时,它应该再次变为播放按钮。我很困惑怎么办?这是我的按钮点击监听器..
btnPublish.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
btnPublish.setImageResource(R.drawable.recordactive);
SharedPreferences.Editor editor = sp.edit();
editor.putString("rtmpUrl", rtmpUrl);
editor.apply();
mPublisher.startPublish(rtmpUrl);
mPublisher.startCamera();
}
});
当我按下播放按钮时,它正在更改为停止图像但是当我按下该停止按钮时,我的应用程序崩溃了。因为我没有为此编写任何代码..请指导我如何操作。基本上按下那个停止按钮后我想调用这个功能..
mPublisher.stopPublish();
mPublisher.stopRecord();
答案 0 :(得分:1)
boolean showing = false;
btnPublish.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(showing){
mPublisher.stopPublish();
mPublisher.stopRecord();
showing = false;
}
else
{
btnPublish.setImageResource(R.drawable.recordactive);
SharedPreferences.Editor editor = sp.edit();
editor.putString("rtmpUrl", rtmpUrl);
editor.apply();
mPublisher.startPublish(rtmpUrl);
mPublisher.startCamera();
showing = true;
}
}
});
答案 1 :(得分:0)
正如Ronak所说,你想要跟踪当前状态。你可以使用布尔变量来做到这一点。像这样的东西
boolean isRecordActive = false;
btnPublish.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isRecordActive) {
btnPublish.setImageResource(R.drawable.recordinactive);
// stop streaming
} else {
btnPublish.setImageResource(R.drawable.recordactive);
SharedPreferences.Editor editor = sp.edit();
editor.putString("rtmpUrl", rtmpUrl);
editor.apply();
mPublisher.startPublish(rtmpUrl);
mPublisher.startCamera();
}
isRecordActive != isRecordActive;
}
});
答案 2 :(得分:0)
我这样做了......
btnPublish.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//rtmpUrl = efu.getText().toString();
btnPublish1.setVisibility(View.VISIBLE);
btnPublish.setVisibility(View.GONE);
SharedPreferences.Editor editor = sp.edit();
editor.putString("rtmpUrl", rtmpUrl);
editor.apply();
mPublisher.startPublish(rtmpUrl);
mPublisher.startCamera();
}
});
btnPublish1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//rtmpUrl = efu.getText().toString();
btnPublish.setVisibility(View.VISIBLE);
btnPublish1.setVisibility(View.GONE);
mPublisher.stopPublish();
mPublisher.stopRecord();
}
});
答案 3 :(得分:0)
btnPublish.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
btnPublish.setImageResource(R.drawable.recordactive);
SharedPreferences.Editor editor = sp.edit();
editor.putString("rtmpUrl", rtmpUrl);
editor.apply();
//Check what is the image embedded on the button. Assuming button is //ImageButton
Integer integer = (Integer) btnPublish.getTag();
integer = integer == null ? 0 : integer;
switch(integer) {
case R.drawable.recordactive:
btnPublish.setDrawableResource(R.drawable.recordstop); //On click of active, //it should change to recordstop first
btnPublish.setTag(R.drawable.recordstop);
mPublisher.startPublish(rtmpUrl);
mPublisher.startCamera();
break;
case R.drawable.recordstop:
btnPublish.setDrawableResource(R.drawable.recordactive); //On click of stop,
//it should change to recordactive first
btnPublish.setTag(R.drawable.recordactive);
mPublisher.stopPublish();
mPublisher.stopRecord();
default:
btnPublish.setDrawableResource(R.drawable.recordactive); //default is record //active
btnPublish.setTag(R.drawable.recordactive);
break;
}
}
});
代码是一个模拟代码。希望有所帮助!!!
答案 4 :(得分:0)
您必须设置一个变量来存储按钮的当前状态,然后相应地工作。下面给出一个例子
boolean isPlaying = false;
btnPublish.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(isPlaying){
isPlaying = false;
//what to do when pressing stop button
//change your button image
//stop camera and other things...
}else{
isPlaying = true;
//what to do when pressing the play button
btnPublish.setImageResource(R.drawable.recordactive);
SharedPreferences.Editor editor = sp.edit();
editor.putString("rtmpUrl", rtmpUrl);
editor.apply();
mPublisher.startPublish(rtmpUrl);
mPublisher.startCamera();
}
}
});