Android:一个曲调重叠在微调器中的另一个曲调上

时间:2011-06-07 06:48:23

标签: android mp3 spinner

我试过通过微调器播放音乐而且我已经完成了。每当我在微调器上选择项目时,应该播放选定的曲调。这工作正常......但我在下面的代码中有一个很大的例外。即,当所选项目的一个曲调正在播放时,如果我选择另一个同时正在播放第一个曲调的项目。我怎么能解决这个问题?有什么想法吗?

总是感谢帮助....!

这是我的代码......

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

2 个答案:

答案 0 :(得分:1)

在代码顶部创建一个MediaPlayer对象(可能是一个成员变量)。在启动单个mp对象之前,请检查它是否为空。
编辑 - 首先调用stop(),然后调用release(),然后将新的MediaPlayer对象分配给您的成员变量 哦,还要确保你的成员变量在方法之外,以便在方法调用之间保持不变 如果这不起作用,请告诉我 编辑2: 我认为,尽管您在顶部定义了媒体播放器,但您仍然使用局部变量来隐藏成员变量,使其变得无用。我为你建立了一个实用的例子。完全按照它,它将适合你。

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();
                }
            }
        });
    }

}

答案 1 :(得分:0)

为什么不在创建mp之前检查mp是否已经实例化,reset()release()呢?