Skip to content

Commit

Permalink
Fixed crashing bug and added pausing music when call is received
Browse files Browse the repository at this point in the history
  • Loading branch information
Stevan committed Jun 17, 2016
1 parent 1490610 commit 560f30a
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 28 deletions.
18 changes: 1 addition & 17 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 0 additions & 6 deletions .idea/vcs.xml

This file was deleted.

1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<uses-permission android:name="android.permission.MEDIA_CONTENT_CONTROL" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

<application
android:allowBackup="true"
Expand Down
35 changes: 35 additions & 0 deletions app/src/main/java/com/smedic/tubtub/BackgroundAudioService.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import android.support.v4.media.session.MediaControllerCompat;
import android.support.v4.media.session.MediaSessionCompat;
import android.support.v7.app.NotificationCompat;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.util.SparseArray;

Expand Down Expand Up @@ -96,6 +98,7 @@ public void onCreate() {
mMediaPlayer.setOnCompletionListener(this);
mMediaPlayer.setOnPreparedListener(this);
initMediaSessions();
initPhoneCallListener();
}

@Override
Expand All @@ -104,6 +107,29 @@ public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}

private void initPhoneCallListener(){
PhoneStateListener phoneStateListener = new PhoneStateListener() {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
if (state == TelephonyManager.CALL_STATE_RINGING) {
//Incoming call: Pause music
pauseVideo();
} else if(state == TelephonyManager.CALL_STATE_IDLE) {
//Not in call: Play music
resumeVideo();
} else if(state == TelephonyManager.CALL_STATE_OFFHOOK) {
//A call is dialing, active or on hold
}
super.onCallStateChanged(state, incomingNumber);
}
};

TelephonyManager mgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
if(mgr != null) {
mgr.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
}
}

/**
* Handles intent (player options play/pause/stop...)
*
Expand Down Expand Up @@ -398,6 +424,15 @@ private void pauseVideo() {
}
}

/**
* Resumes video
*/
private void resumeVideo() {
if (mMediaPlayer != null) {
mMediaPlayer.start();
}
}

/**
* Restarts video
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ public void run() {

/**
* Remove playlist with specific ID from DB and list
*
* @param playlistId
*/
private void removePlaylist(final String playlistId) {
Expand Down Expand Up @@ -350,11 +351,15 @@ public void run() {
* @return
*/
private String extractUserName(String emailAddress) {
String[] parts = emailAddress.split("@");
if (parts.length > 0 && parts[0] != null)
return parts[0];
else
return "";
if (emailAddress != null) {
String[] parts = emailAddress.split("@");
if (parts.length > 0) {
if (parts[0] != null) {
return parts[0];
}
}
}
return "";
}

/**
Expand Down

0 comments on commit 560f30a

Please sign in to comment.