Last active
December 26, 2019 17:56
-
-
Save Ochornma/95c202c2cf420e795e5dfebb019d9227 to your computer and use it in GitHub Desktop.
The radio main activity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class MainActivity extends AppCompatActivity { | |
| public static final String PREFRENCES = "com.dclm.radio"; | |
| public Boolean isOnline; | |
| private Intent intent; | |
| RadioService radioService; | |
| boolean mBound = false; | |
| private ImageButton buttonPlay, buttonPause; | |
| private Button buttonLive | |
| private SharedPreferences sharedPreferences; | |
| // Record whether audio is playing or not. | |
| private int audioIsPlaying; | |
| private ServiceConnection connection = new ServiceConnection() { | |
| @Override | |
| public void onServiceConnected(ComponentName className, | |
| IBinder service2) { | |
| // We've bound to LocalService, cast the IBinder and get LocalService instance | |
| RadioService.RadioLocalBinder binder2 = (RadioService.RadioLocalBinder) service2; | |
| radioService = binder2.getService2(); | |
| mBound = true; | |
| } | |
| @Override | |
| public void onServiceDisconnected(ComponentName arg0) { | |
| mBound = false; | |
| } | |
| }; | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.activity_main); | |
| //registerUpdateOnNetwork(); | |
| //stopping(); | |
| audioIsPlaying = 0; | |
| sharedPreferences = getSharedPreferences(PREFRENCES, Context.MODE_PRIVATE); | |
| SharedPreferences.Editor editor = sharedPreferences.edit(); | |
| editor.putInt("audioIsPlaying", audioIsPlaying); | |
| editor.apply(); | |
| // find the views of the listed i.e initialize the views | |
| buttonPlay = findViewById(R.id.play); | |
| buttonPause = findViewById(R.id.stop); | |
| buttonlive = findViewById(R.id.live); | |
| buttonPause.setVisibility(View.INVISIBLE); | |
| buttonlive.setVisibility(View.INVISIBLE); | |
| // seek the onclick listener for the button | |
| buttonPlay.setOnClickListener(new View.OnClickListener() { | |
| @Override | |
| public void onClick(View view) { | |
| SharedPreferences prefs = getSharedPreferences(PREFRENCES, MODE_PRIVATE); | |
| int resume = prefs.getInt("audioIsPlaying", 0); //0 is the default value. | |
| if(resume != 10){ | |
| intent = new Intent(MainActivity.this, RadioService.class); | |
| Util.startForegroundService(MainActivity.this, intent); | |
| } | |
| int state = dclmRadioService.player.getPlaybackState() | |
| buttonPlay.setVisibility(View.INVISIBLE); | |
| buttonPause.setVisibility(View.VISIBLE); | |
| buttonlive.setVisibility(View.VISIBLE); | |
| if (state == Player.STATE_READY) { | |
| radioService.startPlayer(); | |
| audioIsPlaying = 10; | |
| SharedPreferences.Editor editor = sharedPreferences.edit(); | |
| editor.putInt("audioIsPlaying", audioIsPlaying); | |
| editor.apply(); | |
| } else if (state == Player.STATE_IDLE) { | |
| intent = new Intent(MainActivity.this, RadioService.class); | |
| Util.startForegroundService(MainActivity.this, intent); | |
| //dclmRadioService.prepare(); | |
| Handler mHandler = new Handler(getMainLooper()); | |
| mHandler.post(new Runnable() { | |
| @Override | |
| public void run() { | |
| dclmRadioService.startPlayer(); | |
| } | |
| }); | |
| audioIsPlaying = 10; | |
| SharedPreferences.Editor editor = sharedPreferences.edit(); | |
| editor.putInt("audioIsPlaying", audioIsPlaying); | |
| editor.apply(); | |
| } else if (state == Player.STATE_ENDED) { | |
| intent = new Intent(MainActivity.this, RadioService.class); | |
| Util.startForegroundService(MainActivity.this, intent); | |
| radioService.startPlayer(); | |
| } | |
| } | |
| }); | |
| buttonPause.setOnClickListener(new View.OnClickListener() { | |
| @Override | |
| public void onClick(View view) { | |
| buttonPause.setVisibility(View.INVISIBLE); | |
| buttonPlay.setVisibility(View.VISIBLE); | |
| Handler mHandler = new Handler(getMainLooper()); | |
| mHandler.post(new Runnable() { | |
| @Override | |
| public void run() { | |
| dclmRadioService.pausePlayer(); | |
| } | |
| }); | |
| int state2 = radioService.player.getPlaybackState(); | |
| } | |
| }); | |
| buttonlive.setOnClickListener(new View.OnClickListener() { | |
| @Override | |
| public void onClick(View v) { | |
| dclmRadioService.prepare(); | |
| buttonPlay.setVisibility(View.INVISIBLE); | |
| buttonPause.setVisibility(View.VISIBLE); | |
| } | |
| }); | |
| } | |
| @Override | |
| protected void onResume() { | |
| super.onResume(); | |
| SharedPreferences prefs = getSharedPreferences(PREFRENCES, MODE_PRIVATE); | |
| int resume = prefs.getInt("audioIsPlaying", 0); //0 is the default value. | |
| if(resume == 10) { | |
| boolean remain = radioService.isPlaying(); | |
| Log.i("Main", String.valueOf(remain)); | |
| if (!remain){ | |
| buttonPause.setVisibility(View.VISIBLE); | |
| buttonPlay.setVisibility(View.INVISIBLE); | |
| } else { | |
| buttonPause.setVisibility(View.INVISIBLE); | |
| buttonPlay.setVisibility(View.VISIBLE); | |
| } | |
| } | |
| } | |
| @Override | |
| protected void onStart() { | |
| super.onStart(); | |
| // Bind to DCLMService | |
| Intent intent = new Intent(this, RadioService.class); | |
| bindService(intent, connection, BIND_AUTO_CREATE); | |
| } | |
| @Override | |
| protected void onStop() { | |
| super.onStop(); | |
| if(audioIsPlaying != 5) { | |
| unbindService(connection); | |
| mBound = false; | |
| } | |
| } | |
| @Override | |
| public void onBackPressed() { | |
| Intent setIntent = new Intent(Intent.ACTION_MAIN); | |
| setIntent.addCategory(Intent.CATEGORY_HOME); | |
| setIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | |
| startActivity(setIntent); | |
| } | |
| @Override | |
| protected void onDestroy() { | |
| intent = new Intent(MainActivity.this, RadioService.class); | |
| stopService(intent); | |
| super.onDestroy(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment