Skip to content

Commit

Permalink
added example with Service issue #5 and #8
Browse files Browse the repository at this point in the history
  • Loading branch information
Gautam committed Apr 4, 2018
1 parent e0b1ec4 commit 907113c
Show file tree
Hide file tree
Showing 14 changed files with 277 additions and 8 deletions.
11 changes: 10 additions & 1 deletion audiovisualizer/src/main/java/com/chibde/BaseVisualizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,17 @@ public void setColor(int color) {
this.paint.setColor(this.color);
}

/**
* @deprecated will be removed in next version use {@link BaseVisualizer#setPlayer(int)} instead
* @param mediaPlayer MediaPlayer
*/
@Deprecated
public void setPlayer(MediaPlayer mediaPlayer) {
visualizer = new Visualizer(mediaPlayer.getAudioSessionId());
setPlayer(mediaPlayer.getAudioSessionId());
}

public void setPlayer(int audioSessionId) {
visualizer = new Visualizer(audioSessionId);
visualizer.setCaptureSize(Visualizer.getCaptureSizeRange()[1]);

visualizer.setDataCaptureListener(new Visualizer.OnDataCaptureListener() {
Expand Down
10 changes: 10 additions & 0 deletions sample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,15 @@
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity"/>
</activity>

<service
android:name=".MediaPlayerService"
android:exported="false" />

<activity android:name=".ServiceExampleActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
</application>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
* Created by gautam chibde on 18/11/17.
*/
abstract public class BaseActivity extends AppCompatActivity {
private static final int AUDIO_PERMISSION_REQUEST_CODE = 102;
public static final int AUDIO_PERMISSION_REQUEST_CODE = 102;

public static final String[] WRITE_EXTERNAL_STORAGE_PERMS = {
Manifest.permission.RECORD_AUDIO
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ public void lineBar(View view) {
startActivity(LineBarVisualizerActivity.class);
}

public void service(View view) {
startActivity(ServiceExampleActivity.class);
}

public void startActivity(Class clazz) {
startActivity(new Intent(this, clazz));
}
Expand Down
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;
}
}
}
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();
}
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ protected void init() {
barVisualizer.setDensity(70);

// Set your media player to the visualizer.
barVisualizer.setPlayer(mediaPlayer);
barVisualizer.setPlayer(mediaPlayer.getAudioSessionId());
}

public void replay(View view) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class CircleBarVisualizerActivity extends BaseActivity {
protected void init() {
CircleBarVisualizer circleBarVisualizer = findViewById(R.id.visualizer);
circleBarVisualizer.setColor(ContextCompat.getColor(this, R.color.custom));
circleBarVisualizer.setPlayer(mediaPlayer);
circleBarVisualizer.setPlayer(mediaPlayer.getAudioSessionId());
}

public void replay(View view) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ protected void init() {
circleVisualizer.setStrokeWidth(1);

// Set your media player to the visualizer.
circleVisualizer.setPlayer(mediaPlayer);
circleVisualizer.setPlayer(mediaPlayer.getAudioSessionId());
}

public void replay(View view) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ protected void init() {
lineBarVisualizer.setDensity(90f);

// Set your media player to the visualizer.
lineBarVisualizer.setPlayer(mediaPlayer);
lineBarVisualizer.setPlayer(mediaPlayer.getAudioSessionId());
}

public void replay(View view) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ protected void init() {
lineVisualizer.setStrokeWidth(1);

// Set you media player to the visualizer.
lineVisualizer.setPlayer(mediaPlayer);
lineVisualizer.setPlayer(mediaPlayer.getAudioSessionId());
}

public void replay(View view) {
Expand Down
15 changes: 14 additions & 1 deletion sample/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,22 @@
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:background="@color/custom"
android:onClick="lineBar"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:text="@string/line_bar_visualizer"
android:textColor="@color/white"/>

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:background="@color/custom"
android:onClick="service"
android:text="@string/activity_with_service"
android:textColor="@color/white"/>
</LinearLayout>
12 changes: 12 additions & 0 deletions sample/src/main/res/layout/activity_service_example.xml
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>
1 change: 1 addition & 0 deletions sample/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
<string name="circle_visualizer">Circle Visualizer</string>
<string name="circle_bar_visualizer">Circle Bar Visualizer</string>
<string name="line_bar_visualizer">Line Bar Visualizer</string>
<string name="activity_with_service">Activity With Service</string>
</resources>

0 comments on commit 907113c

Please sign in to comment.