开发者

Android: One tune overlapping on the other tune in spinner

i have tried to play music via spinner and i have done it. Whenever i am selecting item on spinner selected tune should be played. That is working fine...But i got one big exception in my below code. i.e., while one tune for the selected item is playing and if I select another item that is also playing with the first tune simultaneously. how could i fix this? Any Idea?

Help is always appreciated....!

This is my code...

import java.util.concurrent.Callable;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Spinner;

public class SettingsActivity extends Activity {
    //FrameLayout maininclude;
    RelativeLayout settingspage;
    RelativeLayout loginpage;
    RelativeLayout registerpage;
    RelativeLayout forgotpasswordpage;
    MediaPlayer mp; 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settingspage);
        final Button login = (Button) findViewById(R.id.setloginbutton);
        final Button register = (Button) findViewById(R.id.setregisterbutton);
        final Button forgotpassword1 = (Button) findViewById(R.id.setforgotpasswordbutton);
        final Spinner tunes = (Spinner) findViewById(R.id.tunesspinner);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                this, R.array.tunes, R.layout.spinnerlayout);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        tunes.setAdapter(adapter);
        tunes.setOnItemSelectedListener(new OnItemSelectedListener() 
        {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long i) {
                if(position == 0) {
                    //First Entry
                    MediaPlayer mp =  MediaPlayer.create(getBaseContext(), R.raw.dumy);
                    mp.start();
                }
                if(position == 1) {
                    MediaPlayer mp =  MediaPlayer.create(getBaseContext(), R.raw.akko_echo);
                    mp.start();

                }
                if(position == 2) {
                    MediaPlayer mp =  MediaPlayer.create(getBaseContext(), R.raw.crystal_cling);
                    mp.start();

                }
                if(position == 3) {
                    MediaPlayer mp =  MediaPlayer.create(getBaseContext(), R.raw.glocken_jing);
                    reset();
                    mp.start();
                }
                if(position == 4) {
                    MediaPlayer mp =  MediaPlayer.create(getBaseContext(), R.raw.bang_drum);
                    reset();
                    mp.start();
                }
                if(position == 5) {
                    MediaPlayer mp =  MediaPlayer.create(getBaseContext(), R.raw.grand_piano);
                    reset();
                    mp.start();
                }
                if(position == 6) {
                    MediaPlayer mp =  MediaPlayer.create(getBaseContext(), R.raw.hard_rock);
       开发者_JAVA百科             reset();
                    mp.start();
                }
                if(position == 7) {
                    MediaPlayer mp =  MediaPlayer.create(getBaseContext(), R.raw.sonic_bloom);
                    mp.start();
                }
                if(position == 8) {
                    MediaPlayer mp =  MediaPlayer.create(getBaseContext(), R.raw.bass_boost);
                    reset();
                    mp.start();
                }
                if(position == 9) {
                    MediaPlayer mp =  MediaPlayer.create(getBaseContext(), R.raw.spooky_freaky);
                    reset();
                    mp.start();
                }
                if(position == 10) {
                    MediaPlayer mp =  MediaPlayer.create(getBaseContext(), R.raw.akko_echo);
                    reset();
                    mp.start();
                }
                if(position == 11) {
                    MediaPlayer mp =  MediaPlayer.create(getBaseContext(), R.raw.shan_zung);
                    reset();
                    mp.start();
                }
                if(position == 12) {
                    MediaPlayer mp =  MediaPlayer.create(getBaseContext(), R.raw.yo_drum);
                    reset();
                    mp.start();
                }
            }
            private void reset() {
                // TODO Auto-generated method stub

            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
            }
        });
//maininclude = (FrameLayout) findViewById(R.id.mainlayout);
       //settingspage = (RelativeLayout) findViewById(R.id.settingsxml);
        // loginpage = (RelativeLayout) findViewById(R.id.loginxml);
         //registerpage = (RelativeLayout) findViewById(R.id.registrationxml);
         //forgotpasswordpage = (RelativeLayout) findViewById(R.id.forgotpasswordxml);
        login.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view) {
                // TODO Auto-generated method stub
                Intent myIntent = new Intent(view.getContext(),
                        LoginActivity.class);
                startActivityForResult(myIntent, 0);
                overridePendingTransition(R.anim.fadein, 0);
                // settingspage.setVisibility(View.GONE);
                 //registerpage.setVisibility(View.GONE);
                 //forgotpasswordpage.setVisibility(View.GONE);
                 //loginpage.setVisibility(View.VISIBLE);
            }
        });
        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // TODO Auto-generated method stub
                Intent myIntent = new Intent(view.getContext(),
                        RegisterActivity.class);
                startActivityForResult(myIntent, 1);
                overridePendingTransition(R.anim.fadein, 0);
            }
        });
        forgotpassword1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // TODO Auto-generated method stub
                Intent myIntent = new Intent(view.getContext(),
                        ForgotPasswordActivity.class);
                startActivityForResult(myIntent, 2);
                overridePendingTransition(R.anim.fadein, 0);
            }
        });
    }
}


Create one MediaPlayer object at the top of your code (a member variable perhaps). Before you start your single mp object, check if it's null or not.
EDIT - first call stop() , then call release(), then assign the new MediaPlayer object to your member variable.
Oh, also, make sure your member variable is outside the method so it persists between method calls.
If this doesn't work, let me know.
EDIT 2: I think that despite you defining your media player at the top, you persisted with local variables that shadowed the member variable, rendering it useless. I have built a working example for you. Follow it exactly and it will work for you.

package com.example.sounds;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class SoundActivity extends Activity {

    MediaPlayer mp;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        final Spinner tunes = (Spinner) findViewById(R.id.spinner1);

        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.sounds_array, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        tunes.setAdapter(adapter);

        tunes.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long i) {

                if (mp != null){
                    mp.stop();
                    mp.release();
                }

                switch (position) {
                    case 0:
                        mp = MediaPlayer.create(getBaseContext(), R.raw.sound_click);
                        mp.start();
                        break;
                    case 1:
                        mp = MediaPlayer.create(getBaseContext(), R.raw.sound_lotus);
                        mp.start();
                        break;
                    case 2:
                        mp = MediaPlayer.create(getBaseContext(), R.raw.sound_tear);
                        mp.start();
                        break;
                }

            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {

                if (mp != null){
                    mp.stop();
                    mp.release();
                }
            }
        });
    }

}


Why don't you check to see if mp has been instantiated already and reset() or release() it before creating it again?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜