Вопрос

So what I am trying to create is an activity that displays an image button. The background for the image button points to an xml in the drawable folder to show the different pictures for on focus and click. That all works fine. I have music in my main activity that is set to loop. By default the image button is set to say Music On. What I want to happen is when the button is clicked the main sound will pause and the button background will change to a different xml drawable layout that says Music Off. When it is clicked again the music will resume where it left off and again switch back to Music On.

One problem I am having is pausing the main sound. Since I'm new to android can a media player variable I reference in my main activity be changed in a different activity? Also, in my options activity I have two if statements under the on click for the image button to check whether the sound is playing or isn't and then will either pause or resume the music depending on which one it is. I am not sure how to do the second if statement but I have the first one that I think might be right.

Sorry that there are a lot of different things I am trying to do, but I tried to break it down. Also, I am getting force closes as of now when I start the optionsActivity and I will put everything underneath including the main activity because that is where I establish the mainSound. Thanks for any help you can give me.

MainActivity:

package com.crazycastles;


import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;



public class MainActivity extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
    private MediaPlayer mainSound;


    @Override 


    public boolean onKeyDown(int keyCode, KeyEvent event) { if ((keyCode == KeyEvent.KEYCODE_BACK)) { //Back key pressed //Things to Do 
        if(mainSound!= null) { mainSound.pause(); mainSound=null; } finish(); return true; } return super.onKeyDown(keyCode, event); }



    public void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
        WindowManager.LayoutParams.FLAG_FULLSCREEN);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mainSound = MediaPlayer.create(MainActivity.this, R.raw.mainscreen);
        mainSound.setLooping(true);
        mainSound.start();



        //CREATE BUTTON 1 & SOUND
        final MediaPlayer buttonSound = MediaPlayer.create(
                MainActivity.this, R.raw.swords);

        ImageButton button1 = (ImageButton) findViewById(R.id.button1);
        button1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                buttonSound.start();
                startActivity(new Intent(MainActivity.this,
                        button1Activity.class));
            }
        }); 

        ImageButton multiplayerbutton = (ImageButton) findViewById(R.id.multiplayerbutton);
        multiplayerbutton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                buttonSound.start();
                startActivity(new Intent(MainActivity.this,
                        multiplayerbuttonActivity.class));
            }
        }); 

        ImageButton optionsbutton = (ImageButton) findViewById(R.id.optionsbutton);
        optionsbutton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                buttonSound.start();
                startActivity(new Intent(MainActivity.this,
                        optionsActivity.class));
            }
        }); 

        ImageButton creditbutton = (ImageButton) findViewById(R.id.creditbutton);
        creditbutton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                buttonSound.start();
                startActivity(new Intent(MainActivity.this,
                        creditsActivity.class));
            }
        }); 
        ImageButton exitbutton = (ImageButton) findViewById(R.id.exitbutton);
        exitbutton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                buttonSound.start();
                finish();
                mainSound.stop();
                System.exit(0);

            }
        }); 



        //END OF BUTTON1 & SOUND



        }
    public void onClick(View v) {
        // TODO Auto-generated method stub

    }

}

optionsActivity:

package com.crazycastles;

import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;

public class optionsActivity extends Activity {
    /** Called when the activity is first created. */
    ImageButton musicbutton, musicbutton2;
    private MediaPlayer mainSound;
    final MediaPlayer buttonSound = MediaPlayer.create(
            optionsActivity.this, R.raw.swords);


    @Override

        public void onCreate(Bundle savedInstanceState) {
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
            WindowManager.LayoutParams.FLAG_FULLSCREEN);

            super.onCreate(savedInstanceState);
            setContentView(R.layout.options);



            final ImageButton musicbutton = (ImageButton) findViewById(R.id.musicbutton);
            musicbutton.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {

            if(mainSound.isPlaying()) {
                musicbutton.setBackgroundResource(R.drawable.musicbutton2);
                buttonSound.start();
                mainSound.pause();
            }
            }
            });








}
}

LogCat:

01-15 16:10:55.059: E/AndroidRuntime(7319): FATAL EXCEPTION: main
01-15 16:10:55.059: E/AndroidRuntime(7319): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.crazycastles/com.crazycastles.optionsActivity}: java.lang.NullPointerException
01-15 16:10:55.059: E/AndroidRuntime(7319):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2659)
01-15 16:10:55.059: E/AndroidRuntime(7319):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2753)
01-15 16:10:55.059: E/AndroidRuntime(7319):     at android.app.ActivityThread.access$2500(ActivityThread.java:129)
01-15 16:10:55.059: E/AndroidRuntime(7319):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2107)
01-15 16:10:55.059: E/AndroidRuntime(7319):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-15 16:10:55.059: E/AndroidRuntime(7319):     at android.os.Looper.loop(Looper.java:143)
01-15 16:10:55.059: E/AndroidRuntime(7319):     at android.app.ActivityThread.main(ActivityThread.java:4701)
01-15 16:10:55.059: E/AndroidRuntime(7319):     at java.lang.reflect.Method.invokeNative(Native Method)
01-15 16:10:55.059: E/AndroidRuntime(7319):     at java.lang.reflect.Method.invoke(Method.java:521)
01-15 16:10:55.059: E/AndroidRuntime(7319):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-15 16:10:55.059: E/AndroidRuntime(7319):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-15 16:10:55.059: E/AndroidRuntime(7319):     at dalvik.system.NativeStart.main(Native Method)
01-15 16:10:55.059: E/AndroidRuntime(7319): Caused by: java.lang.NullPointerException
01-15 16:10:55.059: E/AndroidRuntime(7319):     at android.content.ContextWrapper.getResources(ContextWrapper.java:80)
01-15 16:10:55.059: E/AndroidRuntime(7319):     at android.media.MediaPlayer.create(MediaPlayer.java:641)
01-15 16:10:55.059: E/AndroidRuntime(7319):     at com.crazycastles.optionsActivity.<init>(optionsActivity.java:17)
01-15 16:10:55.059: E/AndroidRuntime(7319):     at java.lang.Class.newInstanceImpl(Native Method)
01-15 16:10:55.059: E/AndroidRuntime(7319):     at java.lang.Class.newInstance(Class.java:1429)
01-15 16:10:55.059: E/AndroidRuntime(7319):     at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
01-15 16:10:55.059: E/AndroidRuntime(7319):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2651)
Это было полезно?

Решение

I also have developed a music player in Android

One problem I am having is pausing the main sound. Since I'm new to android can a media player variable I reference in my main activity be changed in a different activity?

I could say it yes, if you declare it as a static Object

Also, in my options activity I have two if statements under the on click for the image button to check whether the sound is playing or isn't and then will either pause or resume the music depending on which one it is. I am not sure how to do the second if statement but I have the first one that I think might be right.

I think you have to look at Android Media Player lifecycle, you could re-use your object but there is some conditions : http://developer.android.com/reference/android/media/MediaPlayer.html

Другие советы

Since I'm new to android can a media player variable I reference in my main activity be changed in a different activity?

No. If you create a MediaPlayer in an activity, it should only be used while that activity is in the foreground. Your MediaPlayer most likely should be managed by a Service, if you are planning on it continuing to play once the user has left the activity.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top