如何正确更改音量?

时间:2015-11-06 03:11:01

标签: java android audio countdowntimer

我已经坚持了太长时间(昨天4个小时,今天3个小时!)现在。在我的主菜单屏幕中,我有一个静音按钮。我希望它在我的后台服务中调用一个方法,该方法将所有将在我的游戏背景中播放的MediaPlayer音频静音。

Full code here

以下内容可能导致此问题:

每当音频播放(播放正常)时我都会收到错误:

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++;
        }

        __________________________________________________________________

这是堆栈跟踪:

 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)

我真的很感激任何反馈(积极或消极)! 需要帮助请叫我。非常感谢所有事情,我希望我能解决这个问题... :)

0 个答案:

没有答案