package com.jrvermeer.psalter;

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;

/**
 * Created by Jonathan on 4/3/2017.
 */

public class MediaService extends Service {
    MediaBinder mediaBinder;
    MediaPlayer mediaPlayer;
    IMediaCallbacks mediaCallbacks;

    @Override
    public void onCreate() {
        super.onCreate();
        mediaBinder = new MediaBinder();
    }

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

    public class MediaBinder extends Binder{
        public void setCallbacks(IMediaCallbacks callbacks){
            mediaCallbacks = callbacks;
        }

        public boolean playMedia(int psalterNumber){
            try{
                int resID = getResources().getIdentifier("_" + psalterNumber, "raw", getPackageName());
                stopMedia();
                mediaPlayer = MediaPlayer.create(getApplicationContext(), resID);
                mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                                                        @Override
                                                        public void onCompletion(MediaPlayer mediaPlayer) {
                                                            if (mediaCallbacks != null) {
                                                                mediaCallbacks.playerFinished();
                                                            }
                                                        }
                                                    });
                mediaPlayer.start();
                return true;
            } catch (Exception ex){
                return  false;
            }
        }

        public void stopMedia(){
            if(isPlaying()){
                mediaPlayer.stop();
                if(mediaCallbacks != null){
                    mediaCallbacks.playerFinished();
                }
            }
        }

        public boolean isPlaying(){
            return mediaPlayer != null && mediaPlayer.isPlaying();
        }
    }

    public interface IMediaCallbacks {
        void playerFinished();
    }
}

@@ -84,16 +84,64 @@
 Intent;%0A
+import android.content.res.AssetFileDescriptor;%0A
 import a
@@ -211,32 +211,75 @@
 oid.os.IBinder;%0A
+import android.support.annotation.NonNull;%0A
 import android.s
@@ -404,172 +404,113 @@
     
-MediaBinder mediaBinder;%0A    MediaPlayer mediaPlayer;%0A    IMediaCallbacks mediaCallbacks;%0A%0A    @Override%0A    public void onCreate() %7B%0A        super.onCreate
+@Nullable%0A    @Override%0A    public IBinder onBind(Intent intent) %7B%0A        return new MediaBinder
 ();%0A    
     
@@ -509,80 +509,105 @@
     
+%7D%0A%0A
     
-mediaBinder = new MediaBinder();%0A    %7D%0A%0A    @Nullable%0A    @Override%0A
+public class MediaBinder extends Binder implements MediaPlayer.OnCompletionListener %7B%0A    
     
@@ -617,119 +617,166 @@
 lic 
-I
+Media
 Binder
- onBind(Intent intent) %7B%0A        return mediaBinder
+()%7B%0A            mediaPlayer = new MediaPlayer()
 ;%0A    
-%7D%0A%0A    public class MediaBinder extends Binder%7B
+    %7D%0A        private MediaPlayer mediaPlayer;%0A        private IMediaCallbacks mediaCallbacks;
 %0A   
@@ -805,16 +805,31 @@
 llbacks(
+@NonNull final 
 IMediaCa
@@ -880,32 +880,87 @@
 ks = callbacks;%0A
+            mediaPlayer.setOnCompletionListener(this);%0A
         %7D%0A%0A     
@@ -1024,16 +1024,46 @@
    try%7B%0A
+                stopMedia();%0A%0A
         
@@ -1178,730 +1178,271 @@
     
-stopMedia();%0A                mediaPlayer = MediaPlayer.create(getApplicationContext(), resID);%0A                mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() %7B%0A                                                        @Override%0A                                                        public void onCompletion(MediaPlayer mediaPlayer) %7B%0A                                                            if (mediaCallbacks != null) %7B%0A                                                                mediaCallbacks.playerFinished();%0A                                                            %7D%0A                                                        %7D%0A                                                    %7D
+AssetFileDescriptor afd = getApplicationContext().getResources().openRawResourceFd(resID);%0A                mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());%0A                afd.close();%0A                mediaPlayer.prepare(
 );%0A 
@@ -1716,35 +1716,34 @@
     
-if(mediaCallbacks != null)%7B
+onCompletion(mediaPlayer);
 %0A   
@@ -1747,32 +1747,34 @@
             
+%7D%0A
         
 mediaCallbac
@@ -1765,40 +1765,46 @@
     
-mediaCallbacks.playerFinished();
+%7D%0A%0A        public boolean isPlaying()%7B
 %0A   
@@ -1808,34 +1808,79 @@
             
-    %7D%0A    
+return mediaPlayer != null && mediaPlayer.isPlaying();%0A
         %7D%0A  
@@ -1873,32 +1873,33 @@
 ;%0A        %7D%0A
+%0A
         
 %7D%0A%0A        p
@@ -1878,34 +1878,41 @@
      %7D%0A%0A        
-%7D%0A
+@Override
 %0A        public 
@@ -1915,95 +1915,188 @@
 lic 
-boolean isPlaying()%7B%0A            return mediaPlayer != null && mediaPlayer.isPlaying();
+void onCompletion(MediaPlayer mediaPlayer) %7B%0A            mediaPlayer.reset();%0A            if(mediaCallbacks != null)%7B%0A                mediaCallbacks.playerFinished();%0A            %7D
 %0A   

fix bug where audio wouldn't play after a while