Skip to content

Commit 808f6f8

Browse files
author
Sid Zi
committed
music service auto stop on idling + action bar scroll up on swipe up
1 parent e6a900e commit 808f6f8

File tree

4 files changed

+24
-31
lines changed

4 files changed

+24
-31
lines changed

app/src/main/java/com/sidzi/circleofmusic/services/MusicPlayerService.java

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import android.media.AudioManager;
88
import android.media.MediaPlayer;
99
import android.os.Binder;
10+
import android.os.Handler;
1011
import android.os.IBinder;
1112
import android.support.annotation.Nullable;
1213
import android.support.v4.content.LocalBroadcastManager;
@@ -30,6 +31,7 @@ public class MusicPlayerService extends Service {
3031
public static final String ACTION_CLOSE = "com.sidzi.circleofmusic.CLOSE";
3132
public static Track PLAYING_TRACK = null;
3233
public static boolean PLAYING_BUCKET;
34+
private static boolean PLAYING = false;
3335
private final IBinder mMIBinder = new MusicBinder();
3436
public MediaPlayer mMediaPlayer = null;
3537
private int PLAYING_TRACK_POSITION = -1;
@@ -39,14 +41,10 @@ public class MusicPlayerService extends Service {
3941
private Context mContext;
4042
private int songsTillSleep = -1;
4143

42-
@Override
43-
public int onStartCommand(Intent intent, int flags, int startId) {
44-
return super.onStartCommand(intent, flags, startId);
45-
}
46-
4744
@Override
4845
public void onCreate() {
4946
super.onCreate();
47+
// This is here because onStart is not called when BIND_AUTO_CREATE is used
5048
mContext = this;
5149
localBroadcastManager = LocalBroadcastManager.getInstance(this);
5250
final OrmHandler ormHandler = OpenHelperManager.getHelper(this, OrmHandler.class);
@@ -62,21 +60,16 @@ public void onCreate() {
6260

6361
@Override
6462
public boolean onUnbind(Intent intent) {
65-
// final Handler mHandler = new Handler();
66-
// mHandler.postDelayed(new Runnable() {
67-
// @Override
68-
// public void run() {
69-
// try {
70-
// if (!mMediaPlayer.isPlaying()) {
71-
// stopSelf();
72-
// }
73-
// } catch (NullPointerException e) {
74-
// stopSelf();
75-
// } finally {
76-
// mHandler.postDelayed(this, 15000);
77-
// }
78-
// }
79-
// }, 15000);
63+
final Handler mHandler = new Handler();
64+
mHandler.postDelayed(new Runnable() {
65+
@Override
66+
public void run() {
67+
if (!PLAYING)
68+
stopSelf();
69+
else
70+
mHandler.postDelayed(this, 15000);
71+
}
72+
}, 15000);
8073
return super.onUnbind(intent);
8174
}
8275

@@ -93,6 +86,7 @@ public void onDestroy() {
9386

9487

9588
public void play(String track_path) {
89+
PLAYING = true;
9690
if (mMediaPlayer == null)
9791
init();
9892
mMediaPlayer.reset();
@@ -119,6 +113,7 @@ public void onCompletion(MediaPlayer mediaPlayer) {
119113
}
120114

121115
public void play(String Path, String Artist, String Name) {
116+
PLAYING = true;
122117
if (mMediaPlayer == null)
123118
init();
124119
mMediaPlayer.reset();
@@ -134,6 +129,8 @@ public void play(String Path, String Artist, String Name) {
134129
}
135130

136131
public void bucketPlay(String track_path) {
132+
PLAYING = true;
133+
137134
if (mMediaPlayer == null)
138135
init();
139136
mMediaPlayer.reset();
@@ -173,11 +170,13 @@ public void next(boolean bucket) {
173170
}
174171

175172
public void pause() {
173+
PLAYING = false;
176174
mMediaPlayer.pause();
177175
uiUpdate(false);
178176
}
179177

180178
public void unpause() {
179+
PLAYING = true;
181180
mMediaPlayer.start();
182181
uiUpdate(false);
183182
}

app/src/main/java/com/sidzi/circleofmusic/ui/MainActivity.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@
5959

6060
public class MainActivity extends AppCompatActivity {
6161

62+
ServiceConnection musicServiceConnection;
63+
6264
@Override
6365
protected void onCreate(Bundle savedInstanceState) {
6466
super.onCreate(savedInstanceState);
@@ -107,7 +109,7 @@ public void onErrorResponse(VolleyError error) {
107109
Intent intent = new Intent(getApplicationContext(), MusicPlayerService.class);
108110
if (MusicPlayerService.PLAYING_TRACK == null)
109111
startService(intent);
110-
ServiceConnection musicServiceConnection = new ServiceConnection() {
112+
musicServiceConnection = new ServiceConnection() {
111113
@Override
112114
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
113115
MusicPlayerService.MusicBinder musicBinder = (MusicPlayerService.MusicBinder) iBinder;
@@ -121,7 +123,6 @@ public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
121123
else
122124
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(new Intent(MusicPlayerService.ACTION_PAUSE));
123125
}
124-
unbindService(this);
125126
}
126127

127128
@Override
@@ -232,11 +233,6 @@ public void onServiceDisconnected(ComponentName componentName) {
232233
});
233234
builder.create().show();
234235
break;
235-
case R.id.exit:
236-
stopService(new Intent(getApplicationContext(), MusicPlayerService.class));
237-
LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(MusicPlayerService.ACTION_CLOSE));
238-
finish();
239-
break;
240236
case R.id.register:
241237
final AlertDialog.Builder builder1 = new AlertDialog.Builder(this);
242238
final EditText etUsername = new EditText(this);
@@ -299,6 +295,7 @@ protected void onDestroy() {
299295
try {
300296
BucketSaver bucketSaver = new BucketSaver(this);
301297
bucketSaver.saveFile();
298+
unbindService(musicServiceConnection);
302299
} catch (IllegalArgumentException | NullPointerException e) {
303300
e.printStackTrace();
304301
}

app/src/main/res/layout/activity_main.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
android:layout_width="match_parent"
2222
android:layout_height="?attr/actionBarSize"
2323
android:background="?attr/colorPrimary"
24+
app:layout_scrollFlags="scroll|enterAlways"
2425
app:popupTheme="@style/AppTheme.PopupOverlay">
2526

2627
<android.support.v7.widget.SearchView

app/src/main/res/menu/menu_home.xml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,4 @@
2020
<item
2121
android:id="@+id/register"
2222
android:title="Register/Login" />
23-
24-
<item
25-
android:id="@+id/exit"
26-
android:title="Quit" />
2723
</menu>

0 commit comments

Comments
 (0)