Skip to content

Commit

Permalink
Add ambient background sound
Browse files Browse the repository at this point in the history
  • Loading branch information
rubo77 committed May 19, 2019
1 parent ceef1a5 commit e6ac2b3
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 2 deletions.
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="roboyard.SoundService" android:enabled="true"></service>
</application>

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Expand Down
40 changes: 40 additions & 0 deletions app/src/main/java/roboyard/SoundService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package roboyard;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;

import roboyard.eclabs.MainActivity;
import roboyard.eclabs.R;

public class SoundService extends Service {
MediaPlayer player;

@Override
public IBinder onBind(Intent intent) {
return null;
}

public void onCreate() {
int MAX_VOLUME = 100;
float soundVolume = 10;
player = MediaPlayer.create(this, R.raw.singing_bowls_in_a_forest); //select music file
player.setLooping(true); //set looping
final float volume = (float) (1 - (Math.log(MAX_VOLUME - soundVolume) / Math.log(MAX_VOLUME)));
player.setVolume(volume, volume);
}

public int onStartCommand(Intent intent, int flags, int startId) {
player.start();
return Service.START_NOT_STICKY;
}

public void onDestroy() {
player.stop();
player.release();
stopSelf();
super.onDestroy();
}

}
25 changes: 23 additions & 2 deletions app/src/main/java/roboyard/eclabs/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package roboyard.eclabs;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.SurfaceTexture;
Expand All @@ -14,6 +15,8 @@
import android.widget.TextView;
import android.widget.Toast;

import roboyard.SoundService;

public class MainActivity extends Activity
implements TextureView.SurfaceTextureListener {
private TextureView mTextureView;
Expand Down Expand Up @@ -78,8 +81,15 @@ public boolean onTouchEvent(MotionEvent e) {
return true;
}

@Override
public void onResume(){
startSound();
super.onResume();
}

@Override
public void onPause(){
stopSound();
super.onPause();
if(mThread != null){
mThread.interrupt();
Expand All @@ -98,13 +108,23 @@ public void onStop(){

@Override
public void onDestroy(){
stopSound();
super.onDestroy();
if(mThread != null){
mThread.interrupt();
mThread = null;
}
}

public void startSound(){
//start service and play music
startService(new Intent(MainActivity.this, SoundService.class));
}
public void stopSound(){
//stop service and stop music
stopService(new Intent(MainActivity.this, SoundService.class));
}

public void draw(Canvas pCanvas) {
synchronized (this.renderManager) {
this.renderManager.setMainTarget(pCanvas);
Expand Down Expand Up @@ -142,8 +162,9 @@ protected void onCreate(Bundle savedInstanceState) {

content.addView(mTextureView, new FrameLayout.LayoutParams(sWidth, sHeight));
setContentView(content);
}

startSound();
}
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
mThread = new RenderingThread(mTextureView);
Expand All @@ -166,8 +187,8 @@ public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
// Ignored
}

private class RenderingThread extends Thread {

private final TextureView mSurface;
private volatile boolean mRunning = true;

Expand Down
Binary file not shown.

0 comments on commit e6ac2b3

Please sign in to comment.