再次单击时,“暂停”按钮将变为不可见

时间:2017-11-20 06:27:08

标签: android exoplayer

我尝试实现以下内容:当我暂停时,我停止服务并播放音频。当您单击播放按钮时,启动服务并播放音频。但只有再次单击时,暂停按钮才会变为不可见。帮我解决一下

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();
            }
        };
        final 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) {
                stopPlayerService();
                if (exo_pause.getVisibility() == View.VISIBLE) {
                    Log.d("TAG", "visible");
                } else {
                    Log.d("TAG", "invisible");
                }
            }
        });

        exo_play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (NetworkUtil.getConnectivityStatus(PlayerActivity.this) != 0 ) {
                    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.btn_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.btn_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);
        }
    };

    @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.btn_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);
    }

}

我导出日志以了解exo_pause监听器中的按钮是否可见。日志显示按钮不可见,但按钮实际上是可见的,只有当您再次单击暂停时它才会变为不可见

听众exo_pause.setOnClickListener

对照

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:background="@android:color/transparent"
    android:layoutDirection="ltr"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal"
        android:paddingTop="4dp">

        <ImageButton
            android:id="@id/exo_prev"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:background="@drawable/btn_prev"
            android:visibility="invisible" />

        <ImageButton
            android:id="@id/exo_rew"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:background="@drawable/btn_rewind"
            android:visibility="gone" />

        <ImageButton
            android:id="@id/exo_play"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:background="@drawable/btn_play" />

        <ImageButton
            android:id="@id/exo_pause"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:background="@drawable/btn_pause" />

        <ImageButton
            android:id="@id/exo_ffwd"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:background="@drawable/btn_fast_forward"
            android:visibility="gone" />

        <ImageButton
            android:id="@id/exo_next"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:background="@drawable/btn_next"
            android:visibility="gone" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="4dp"
        android:background="@android:color/transparent"
        android:gravity="center_vertical"
        android:orientation="horizontal">

        <TextView
            android:id="@id/exo_position"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:includeFontPadding="false"
            android:paddingLeft="4dp"
            android:paddingRight="4dp"
            android:textColor="@android:color/holo_orange_dark"
            android:textSize="14sp"
            android:textStyle="bold" />

        <com.google.android.exoplayer2.ui.DefaultTimeBar
            android:id="@id/exo_progress"
            android:layout_width="0dp"
            android:layout_height="26dp"
            android:layout_weight="1" />

        <TextView
            android:id="@id/exo_duration"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:includeFontPadding="false"
            android:paddingLeft="4dp"
            android:paddingRight="4dp"
            android:textColor="@android:color/holo_orange_dark"
            android:textSize="14sp"
            android:textStyle="bold" />

    </LinearLayout>

</LinearLayout>

活动xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="ru.myapps.fairytalesforchildren.activity.PlayerActivity">

    <LinearLayout
        android:id="@+id/radioInfo"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/player_gradient"
        android:gravity="center"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical"
            android:padding="@dimen/padding">

            <com.makeramen.roundedimageview.RoundedImageView
                android:id="@+id/imgRadio"
                android:layout_width="@dimen/radio_icon_size"
                android:layout_height="@dimen/radio_icon_size"
                android:scaleType="centerCrop"
                android:src="@mipmap/ic_launcher"
                ads:riv_border_color="@android:color/white"
                ads:riv_border_width="@dimen/border"
                ads:riv_corner_radius="@dimen/radio_icon_corner" />

            <TextView
                android:id="@+id/txtRadio"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="@dimen/margin_bottom"
                android:layout_marginTop="@dimen/margin_top"
                android:gravity="center"
                android:text="**"
                android:textAllCaps="true"
                android:textColor="@android:color/white"
                android:textSize="@dimen/text_size"
                android:textStyle="normal|bold" />

        </LinearLayout>

        <LinearLayout
            android:id="@+id/radioBtns"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:background="#2000"
            android:gravity="center"
            android:orientation="vertical"
            android:paddingBottom="@dimen/padding_top"
            android:paddingTop="@dimen/padding_bottom">

            <ImageButton
                android:id="@+id/btnPLayPause"
                android:layout_width="@dimen/play_btn_size"
                android:layout_height="@dimen/play_btn_size"
                android:background="@android:color/transparent"
                android:contentDescription="**"
                android:onClick="onPlayPause"
                android:scaleType="centerCrop"
                app:srcCompat="@drawable/btn_play" />

        </LinearLayout>

        <com.google.android.exoplayer2.ui.SimpleExoPlayerView
            android:id="@+id/player"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:layout_marginBottom="10dp"
            android:layout_marginTop="10dp"
            android:background="@drawable/player_gradient"
            ads:show_timeout="0"
            app:controller_layout_id="@layout/exo_playback_control_view" />

    </LinearLayout>

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

删除这些行:

if(exo_play.getVisibility() == View.VISIBLE) {
        exo_play.setVisibility(View.GONE);
        exo_pause.setVisibility(View.VISIBLE);

    }

之后,在暂停按钮onClickListener中使用这一行并使用方法检查视频是否正在播放:

if(video.isPlaying){
exo_play.setImageResource(R.drawable.btn_pause);
stopPlayerService();
}
    else{
 exo_play.setImageResource(R.drawable.btn_play);
startPlayerService();
}

我觉得它对你很有帮助。