-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Gautam
committed
Apr 4, 2018
1 parent
e0b1ec4
commit 907113c
Showing
14 changed files
with
277 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
82 changes: 82 additions & 0 deletions
82
sample/src/main/java/com/chibde/audiovisualizer/sample/MediaPlayerService.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package com.chibde.audiovisualizer.sample; | ||
|
||
import android.app.Service; | ||
import android.content.Intent; | ||
import android.media.MediaPlayer; | ||
import android.os.Binder; | ||
import android.os.IBinder; | ||
import android.support.annotation.Nullable; | ||
import android.support.v4.content.LocalBroadcastManager; | ||
|
||
public class MediaPlayerService extends Service { | ||
|
||
public static final String INTENT_FILTER = "MediaPlayerServiceIntentFilter"; | ||
public static final String INTENT_AUDIO_SESSION_ID = "intent_audio_session_id"; | ||
|
||
private IBinder mediaPlayerServiceBinder = new MediaPlayerServiceBinder(); | ||
private MediaPlayer mediaPlayer; | ||
|
||
@Override | ||
public void onCreate() { | ||
super.onCreate(); | ||
mediaPlayer = MediaPlayer.create(this, R.raw.red_e); | ||
mediaPlayer.setLooping(false); | ||
|
||
Intent intent = new Intent(INTENT_FILTER); //put the same message as in the filter you used in the activity when registering the receiver | ||
intent.putExtra(INTENT_AUDIO_SESSION_ID, mediaPlayer.getAudioSessionId()); | ||
// Send audio session id through | ||
LocalBroadcastManager.getInstance(this).sendBroadcast(intent); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public IBinder onBind(Intent intent) { | ||
return mediaPlayerServiceBinder; | ||
} | ||
|
||
public void replay() { | ||
if (mediaPlayer != null) { | ||
mediaPlayer.seekTo(0); | ||
} | ||
} | ||
|
||
@Override | ||
public void onRebind(Intent intent) { | ||
super.onRebind(intent); | ||
} | ||
|
||
@Override | ||
public boolean onUnbind(Intent intent) { | ||
return true; | ||
} | ||
|
||
@Override | ||
public void onDestroy() { | ||
super.onDestroy(); | ||
if (mediaPlayer != null) { | ||
mediaPlayer.release(); | ||
} | ||
} | ||
|
||
public boolean isPlaying() { | ||
return mediaPlayer != null && mediaPlayer.isPlaying(); | ||
} | ||
|
||
public void pause() { | ||
if (mediaPlayer != null) { | ||
mediaPlayer.pause(); | ||
} | ||
} | ||
|
||
public void start() { | ||
if (mediaPlayer != null) { | ||
mediaPlayer.start(); | ||
} | ||
} | ||
|
||
public class MediaPlayerServiceBinder extends Binder { | ||
MediaPlayerService getService() { | ||
return MediaPlayerService.this; | ||
} | ||
} | ||
} |
138 changes: 138 additions & 0 deletions
138
sample/src/main/java/com/chibde/audiovisualizer/sample/ServiceExampleActivity.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
package com.chibde.audiovisualizer.sample; | ||
|
||
import android.Manifest; | ||
import android.content.BroadcastReceiver; | ||
import android.content.ComponentName; | ||
import android.content.Context; | ||
import android.content.Intent; | ||
import android.content.IntentFilter; | ||
import android.content.ServiceConnection; | ||
import android.content.pm.PackageManager; | ||
import android.os.IBinder; | ||
import android.support.annotation.NonNull; | ||
import android.support.v4.content.ContextCompat; | ||
import android.support.v4.content.LocalBroadcastManager; | ||
import android.support.v7.app.AppCompatActivity; | ||
import android.os.Bundle; | ||
import android.view.View; | ||
import android.widget.ImageButton; | ||
|
||
import com.chibde.visualizer.BarVisualizer; | ||
|
||
public class ServiceExampleActivity extends AppCompatActivity { | ||
private MediaPlayerService mBoundService; | ||
private BarVisualizer barVisualizer; | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
if (getActionBar() != null) { | ||
getActionBar().setDisplayHomeAsUpEnabled(true); | ||
} | ||
setContentView(R.layout.activity_service_example); | ||
barVisualizer = findViewById(R.id.visualizer); | ||
|
||
// set custom color to the line. | ||
barVisualizer.setColor(ContextCompat.getColor(this, R.color.custom)); | ||
|
||
// define custom number of bars you want in the visualizer between (10 - 256). | ||
barVisualizer.setDensity(70); | ||
initialize(); | ||
} | ||
|
||
@Override | ||
protected void onStart() { | ||
super.onStart(); | ||
// register LocalBroadcastManager | ||
LocalBroadcastManager.getInstance(this).registerReceiver(bReceiver, new IntentFilter(MediaPlayerService.INTENT_FILTER)); | ||
Intent intent = new Intent(this, MediaPlayerService.class); | ||
startService(intent); | ||
bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE); | ||
} | ||
|
||
private void initialize() { | ||
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M | ||
&& checkSelfPermission(Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) { | ||
requestPermissions(BaseActivity.WRITE_EXTERNAL_STORAGE_PERMS, BaseActivity.AUDIO_PERMISSION_REQUEST_CODE); | ||
} | ||
} | ||
|
||
@Override | ||
public void onRequestPermissionsResult( | ||
int requestCode, | ||
@NonNull String[] permissions, | ||
@NonNull int[] grantResults) { | ||
switch (requestCode) { | ||
case BaseActivity.AUDIO_PERMISSION_REQUEST_CODE: | ||
if (grantResults.length > 0 | ||
&& grantResults[0] != PackageManager.PERMISSION_GRANTED) { | ||
finish(); | ||
} | ||
} | ||
} | ||
|
||
|
||
public void replay(View view) { | ||
mBoundService.replay(); | ||
} | ||
|
||
public void playPause(View view) { | ||
playPauseBtnClicked((ImageButton) view); | ||
} | ||
|
||
/** | ||
* receive audio session id required for visualizer through | ||
* broadcast receiver from service | ||
* ref https://stackoverflow.com/a/27652660/5164673 | ||
*/ | ||
private BroadcastReceiver bReceiver = new BroadcastReceiver() { | ||
|
||
@Override | ||
public void onReceive(Context context, Intent intent) { | ||
int audioSessionId = intent.getIntExtra(MediaPlayerService.INTENT_AUDIO_SESSION_ID, -1); | ||
if (audioSessionId != -1) { | ||
barVisualizer.setPlayer(audioSessionId); | ||
} | ||
} | ||
}; | ||
|
||
@Override | ||
protected void onDestroy() { | ||
super.onDestroy(); | ||
stopService(new Intent(this, MediaPlayerService.class)); | ||
unbindService(serviceConnection); | ||
} | ||
|
||
protected void onPause() { | ||
super.onPause(); | ||
// unregister LocalBroadcastManager | ||
LocalBroadcastManager.getInstance(this).unregisterReceiver(bReceiver); | ||
} | ||
|
||
public void playPauseBtnClicked(ImageButton btnPlayPause) { | ||
if (mBoundService.isPlaying()) { | ||
mBoundService.pause(); | ||
btnPlayPause.setImageDrawable(ContextCompat.getDrawable( | ||
this, | ||
R.drawable.ic_play_red_48dp)); | ||
} else { | ||
mBoundService.start(); | ||
btnPlayPause.setImageDrawable(ContextCompat.getDrawable( | ||
this, | ||
R.drawable.ic_pause_red_48dp)); | ||
} | ||
} | ||
|
||
private ServiceConnection serviceConnection = new ServiceConnection() { | ||
|
||
@Override | ||
public void onServiceDisconnected(ComponentName name) { | ||
} | ||
|
||
@Override | ||
public void onServiceConnected(ComponentName name, IBinder service) { | ||
MediaPlayerService.MediaPlayerServiceBinder myBinder = (MediaPlayerService.MediaPlayerServiceBinder) service; | ||
mBoundService = myBinder.getService(); | ||
} | ||
}; | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent"> | ||
|
||
<com.chibde.visualizer.BarVisualizer | ||
android:id="@+id/visualizer" | ||
android:layout_width="match_parent" | ||
android:layout_height="250dp"/> | ||
|
||
<include layout="@layout/layout_audio_buttons"/> | ||
</RelativeLayout> |
This file contains 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