0

I have been stuck on this for way too long (4 hours yesterday, 3 hours today!) now. In my main menu screen, I have a mute button. I want it to call a method in my background service, which mutes all of the MediaPlayer audio that would be played in the background of my game.

Full code here

Here is something that might be causing the issue:

I am getting the error whenever the audio plays (which plays fine):

QCMediaPlayer mediaplayer NOT present
E/MediaPlayer﹕ Should have subtitle controller already set


package com.example.USERNAME.buttonsmasher;


import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.CountDownTimer;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;


public class TwentySeconds extends Service {

    static MediaPlayer ten;
    static MediaPlayer three;
    final String TAG = "MyCountdown";
    static CountDownTimer cdt;
    double millisUntilFinishedRounded;

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

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {


        Context context = this;
        three = MediaPlayer.create(TwentySeconds.this, R.raw.threetwoone);

        ten = MediaPlayer.create(TwentySeconds.this, R.raw.tenmoreseconds);


     //   ten = MediaPlayer.create(TwentySeconds.this, R.raw.tenmoreseconds);

       // three = MediaPlayer.create(TwentySeconds.this, R.raw.threetwoone);

        Log.v(TAG, "In start command");
        cdt = new CountDownTimer(20000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {

                Log.v(TAG, millisUntilFinished + "left");

                millisUntilFinishedRounded = (millisUntilFinished / 1000) * 1000;

                if (millisUntilFinishedRounded == 10000) { //TEN SECONDS

                   // ten = MediaPlayer.create(TwentySeconds.this, R.raw.tenmoreseconds);

                   // while(ten == null){
                        ten = MediaPlayer.create(TwentySeconds.this, R.raw.tenmoreseconds);
                  //  }

                    ten.start();

                }

                if (millisUntilFinishedRounded == 3000) {

                   /* Context context = this;
                    three = MediaPlayer.create(TwentySeconds.this, R.raw.threetwoone);
*/

                   // while(three == null){
                        three = MediaPlayer.create(TwentySeconds.this, R.raw.tenmoreseconds);

                    //}
                    three.start();
                }
            }

            @Override
            public void onFinish() {
                Log.v(TAG, "Finished");


                Intent intent = new Intent(TwentySeconds.this, GameOver.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                Log.v(TAG, "About to start activity");
                startActivity(intent);
            }
        };
        cdt.start();
        return START_STICKY;
    }

    public static void stopTimer() {
        cdt.cancel();
    }

    public static void myMute() {
        ten.setVolume(0, 0);
            three.setVolume(0, 0);
    }
    public static void unMute() {
        ten.setVolume(1, 1);
            three.setVolume(1, 1);
    }


    @Override
    public void onDestroy() {
        stopSelf();
        super.onDestroy();
    }

}


_______________________________________________________________________________________________________________________________


Here is the method in Main_Menu (x starts as zero)


 if ((x%2) == 0) { //If it's even
            TwentySeconds.unMute();
            Toast.makeText(Main_Menu.this, "UNMUTED", Toast.LENGTH_SHORT).show();
            x++;
        } else { //If its odd
            TwentySeconds.myMute();
            Toast.makeText(Main_Menu.this, "MUTED", Toast.LENGTH_SHORT).show();
            x++;
        }

        __________________________________________________________________

Here is the stack trace:

 java.lang.IllegalStateException: Could not execute method of the activity
            at android.view.View$1.onClick(View.java:3842)
            at android.view.View.performClick(View.java:4457)
            at android.view.View$PerformClick.run(View.java:18491)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5272)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:883)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at android.view.View$1.onClick(View.java:3837)
            at android.view.View.performClick(View.java:4457)
            at android.view.View$PerformClick.run(View.java:18491)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5272)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:883)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.example.USERNAME.buttonsmasher.TwentySeconds.myMute(TwentySeconds.java:106)
            at com.example.USERNAME.buttonsmasher.Main_Menu.mute(Main_Menu.java:103)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at android.view.View$1.onClick(View.java:3837)
            at android.view.View.performClick(View.java:4457)
            at android.view.View$PerformClick.run(View.java:18491)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5272)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:883)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
            at dalvik.system.NativeStart.main(Native Method)

I would really appreciate any feedback (positive or negative)! Let me know if you need any thing else. Thanks so much for everything, I hope I can solve this issue... :)

mehrdad khosravi
  • 2,228
  • 9
  • 29
  • 34
Ruchir Baronia
  • 7,406
  • 5
  • 48
  • 83
  • @ElliottFrisch TwentySeconds is the name of the class. I am calling that method by doing: Classname.MethodName (TwentySeconds.unMute/myMute) – Ruchir Baronia Nov 06 '15 at 03:16
  • Sorry. `ten` and `three` are `null`. You declared them as `static` fields, but you never *initialized* them. – Elliott Frisch Nov 06 '15 at 03:18
  • @ElliottFrisch Does this count as initializing? – Ruchir Baronia Nov 06 '15 at 03:23
  • @ElliottFrisch three = MediaPlayer.create(TwentySeconds.this, R.raw.threetwoone); – Ruchir Baronia Nov 06 '15 at 03:23
  • @ElliottFrisch ten = MediaPlayer.create(TwentySeconds.this, R.raw.tenmoreseconds); – Ruchir Baronia Nov 06 '15 at 03:24
  • @ElliottFrisch Because that is in my code – Ruchir Baronia Nov 06 '15 at 03:24
  • Yes. **Where** in your code? And please **edit** your question (don't add details as comments). The goal is to get you the help you want, the way to do that is to improve your question. As is, you've given many off-site links and a huge amount of code. But there is a very common [repeat question](http://stackoverflow.com/q/218384/2970947) that is very similar to this. Anyway, you can copy and paste then select and press ctrl-k to format the pasted code. – Elliott Frisch Nov 06 '15 at 03:27
  • Might be a good idea to take a break. You seem to have confused question with answer. – Elliott Frisch Nov 06 '15 at 03:32
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/94381/discussion-between-rich-and-elliott-frisch). – Ruchir Baronia Nov 06 '15 at 03:33

0 Answers0