我为ExoPlayer按钮创建了我的布局
***
<ImageButton
android:id="@id/exo_play"
android:layout_width="50dp"
android:layout_height="50dp"
android:onClick="onPlayClick"
android:background="@drawable/btn_play" />
<ImageButton
android:id="@id/exo_pause"
android:layout_width="50dp"
android:layout_height="50dp"
android:onClick="onPauseClick"
android:background="@drawable/btn_pause" />
***
我的程序中有一个程序可以从PlayerActivity启动和停止。如何在点击EcxoPlayer中的暂停按钮时停止服务,当您点击播放时,服务重新启动了?
Code PlayerActivity
public class PlayerActivity extends AppCompatActivity {
private int id;
private String title;
private String autor;
private String file;
private String img;
private String MAYBE_ACTION = "MAYBE_ACTION";
static boolean isPlay = false;
ImageButton btnPLayPause;
private TextView txtRadio;
private TextView txtTitle;
private RoundedImageView imgRadio;
private ImageButton exo_pause;
private ImageButton exo_play;
private String internetStatus = "";
BroadcastReceiver br;
BroadcastReceiver serviceReceiver;
public final static String SERVICE_PARAM = "param";
public final static int SERVICE_STATUS = 0;
public final static String BROADCAST_ACTION = "ru.myapps";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_player);
id = getIntent().getExtras().getInt("id");
title = getIntent().getExtras().getString("title");
autor = getIntent().getExtras().getString("autor");
file = "link";
img = getIntent().getExtras().getString("img");
RadioPlayer.simpleExoPlayerView = new SimpleExoPlayerView(this);
RadioPlayer.simpleExoPlayerView = (SimpleExoPlayerView) findViewById(R.id.player);
btnPLayPause = (ImageButton) findViewById(R.id.btnPLayPause);
txtRadio = (TextView) findViewById(R.id.txtRadio);
imgRadio = (RoundedImageView) findViewById(R.id.imgRadio);
exo_pause = (ImageButton) findViewById(R.id.exo_pause);
exo_play = (ImageButton) findViewById(R.id.exo_play);
setTitle(title);
txtRadio.setText(title);
br = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
finish();
}
};
IntentFilter intentFilter = new IntentFilter(MAYBE_ACTION);
registerReceiver(br, intentFilter);
if (NetworkUtil.getConnectivityStatus(this) != 0) {
startPlayerService();
exo_pause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (isPlay) {
stopPlayerService();
}
}
});
exo_play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (NetworkUtil.getConnectivityStatus(PlayerActivity.this) != 0 ) {
if (!isPlay) {
startPlayerService();
}
}
}
});
}
}
@Override
protected void onStart() {
super.onStart();
}
public void startPlayerService() {
Intent serviceIntent = new Intent(PlayerActivity.this, PlayerService.class);
serviceIntent.putExtra(PlayerService.KEY_STREAM, file);
serviceIntent.putExtra(PlayerService.KEY_RADIO, title);
serviceIntent.setAction(PlayerConstants.ACTION.STARTFOREGROUND_ACTION);
startService(serviceIntent);
isPlay = true;
btnPLayPause.setImageResource(R.drawable.icon_stop);
exo_pause.setVisibility(View.VISIBLE);
exo_play.setVisibility(View.GONE);
}
private void stopPlayerService() {
Intent serviceIntent = new Intent(PlayerActivity.this, PlayerService.class);
serviceIntent.setAction(PlayerConstants.ACTION.STOPFOREGROUND_ACTION);
stopService(serviceIntent);
isPlay = false;
btnPLayPause.setImageResource(R.drawable.icon_play);
exo_pause.setVisibility(View.GONE);
exo_play.setVisibility(View.VISIBLE);
}
private BroadcastReceiver NetworkChangeReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
internetStatus = NetworkUtil.getConnectivityStatusString(context);
//Toast.makeText(context, internetStatus, Toast.LENGTH_LONG).show();
}
};
@Override
protected void onResume() {
super.onResume();
serviceReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
int serviceStatus = intent.getIntExtra(SERVICE_PARAM, 0);
if (serviceStatus == SERVICE_STATUS) {
btnPLayPause.setImageResource(R.drawable.icon_play);
}
}
};
IntentFilter filter = new IntentFilter(BROADCAST_ACTION);
registerReceiver(serviceReceiver, filter);
registerReceiver(NetworkChangeReceiver, new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE"));
registerReceiver(NetworkChangeReceiver, new IntentFilter("android.net.wifi.WIFI_STATE_CHANGED"));
}
@Override
protected void onStop() {
super.onStop();
}
@Override
protected void onDestroy() {
super.onDestroy();
if (NetworkChangeReceiver != null) unregisterReceiver(NetworkChangeReceiver);
unregisterReceiver(br);
unregisterReceiver(serviceReceiver);
}
}
我尝试在onresume中实现按钮的操作,但是当我暂停服务时它停止并且播放按钮不能播放
答案 0 :(得分:0)
OnClickListener
OnCreate
应该在public class PlayerActivity extends AppCompatActivity {
private int id;
private String title;
private String autor;
private String file;
private String img;
private String MAYBE_ACTION = "MAYBE_ACTION";
static boolean isPlay = false;
ImageButton btnPLayPause;
private TextView txtRadio;
private TextView txtTitle;
private RoundedImageView imgRadio;
private SimpleExoPlayerView simpleExoPlayerView;
private SimpleExoPlayer player;
private ImageButton exo_pause;
private ImageButton exo_play;
public static String LOG_TAG = "my_log";
private String internetStatus = "";
BroadcastReceiver br;
BroadcastReceiver serviceReceiver;
private AsyncTask jsonTask;
public final static String SERVICE_PARAM = "param";
public final static int SERVICE_STATUS = 0;
public final static String BROADCAST_ACTION = "ru.myapps";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_player);
id = getIntent().getExtras().getInt("id");
title = getIntent().getExtras().getString("title");
autor = getIntent().getExtras().getString("autor");
file = "link_mp3_files";
img = getIntent().getExtras().getString("img");
RadioPlayer.simpleExoPlayerView = new SimpleExoPlayerView(this);
RadioPlayer.simpleExoPlayerView = (SimpleExoPlayerView) findViewById(R.id.player);
btnPLayPause = (ImageButton) findViewById(R.id.btnPLayPause);
txtRadio = (TextView) findViewById(R.id.txtRadio);
imgRadio = (RoundedImageView) findViewById(R.id.imgRadio);
exo_pause = (ImageButton) findViewById(R.id.exo_pause);
exo_play = (ImageButton) findViewById(R.id.exo_play);
setTitle(title);
txtRadio.setText(title);
if (NetworkUtil.getConnectivityStatus(this) != 0) {
startPlayerService();
}
exo_pause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!isPlay) {
stopPlayerService();
}
}
});
exo_play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (NetworkUtil.getConnectivityStatus(PlayerActivity.this) != 0 ) {
if (isPlay) {
startPlayerService();
}
}
}
});
br = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
finish();
}
};
IntentFilter intentFilter = new IntentFilter(MAYBE_ACTION);
registerReceiver(br, intentFilter);
}
@Override
protected void onStart() {
super.onStart();
}
public void startPlayerService() {
Intent serviceIntent = new Intent(PlayerActivity.this, PlayerService.class);
serviceIntent.putExtra(PlayerService.KEY_STREAM, file);
serviceIntent.putExtra(PlayerService.KEY_RADIO, title);
serviceIntent.setAction(PlayerConstants.ACTION.STARTFOREGROUND_ACTION);
startService(serviceIntent);
isPlay = true;
btnPLayPause.setImageResource(R.drawable.icon_stop);
if(exo_play.getVisibility() == View.VISIBLE) {
exo_play.setVisibility(View.GONE);
exo_pause.setVisibility(View.VISIBLE);
}
}
private void stopPlayerService() {
Intent serviceIntent = new Intent(PlayerActivity.this, PlayerService.class);
serviceIntent.setAction(PlayerConstants.ACTION.STOPFOREGROUND_ACTION);
stopService(serviceIntent);
isPlay = false;
btnPLayPause.setImageResource(R.drawable.icon_play);
if(exo_pause.getVisibility() == View.VISIBLE) {
exo_pause.setVisibility(View.GONE);
exo_play.setVisibility(View.VISIBLE);
}
}
private BroadcastReceiver NetworkChangeReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
internetStatus = NetworkUtil.getConnectivityStatusString(context);
//Toast.makeText(context, internetStatus, Toast.LENGTH_LONG).show();
}
};
@Override
protected void onResume() {
super.onResume();
serviceReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
int serviceStatus = intent.getIntExtra(SERVICE_PARAM, 0);
if (serviceStatus == SERVICE_STATUS) {
btnPLayPause.setImageResource(R.drawable.icon_play);
}
}
};
IntentFilter filter = new IntentFilter(BROADCAST_ACTION);
registerReceiver(serviceReceiver, filter);
registerReceiver(NetworkChangeReceiver, new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE"));
registerReceiver(NetworkChangeReceiver, new IntentFilter("android.net.wifi.WIFI_STATE_CHANGED"));
}
@Override
protected void onStop() {
super.onStop();
}
@Override
protected void onDestroy() {
super.onDestroy();
if (NetworkChangeReceiver != null) unregisterReceiver(NetworkChangeReceiver);
unregisterReceiver(br);
unregisterReceiver(serviceReceiver);
}
}
,请试试并告知我们:
OnResume
在startPlayerService();
中,您可以检查它是否正在运行,如果您愿意,请将其收起。
您的问题是,在点击exo_play
时,您从未致电exo_play.setOnClickListener
。请参阅ionic.Platform.exitApp()
。我也删除了else语句,因为它是不必要的。