Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何创建按钮来控制背景声音。_Android_Android Mediaplayer_Android Button - Fatal编程技术网

Android 如何创建按钮来控制背景声音。

Android 如何创建按钮来控制背景声音。,android,android-mediaplayer,android-button,Android,Android Mediaplayer,Android Button,我正在尝试将背景音乐添加到我的应用程序中。我所需要的只是按下btnoptn按钮时播放声音,其文本转换为“音乐关闭”。音乐应在任何活动中继续播放,直到返回到设置页面并再次按下相同的按钮。然后音乐停止,按钮文本变为“音乐开启” 到目前为止,这是我的代码: package hello.english; import hello.english.R; import android.app.Activity; import android.content.Intent; import android.v

我正在尝试将背景音乐添加到我的应用程序中。我所需要的只是按下
btnoptn
按钮时播放声音,其文本转换为“音乐关闭”。音乐应在任何
活动
中继续播放,直到返回到设置页面并再次按下相同的
按钮
。然后音乐停止,按钮
文本变为“音乐开启”

到目前为止,这是我的代码:

package hello.english;

import hello.english.R;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.media.MediaPlayer;
import android.os.Bundle;

public class welcome extends Activity implements OnClickListener{
    private Button btnoptn;
    private Button btnmenu;
    public static MediaPlayer mp2;

    private void btnoptn(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);     
        final Button testButton = (Button) findViewById(R.id.btnoptn);
        testButton.setTag(1);

        testButton.setText("Musik Of");
        mp2=MediaPlayer.create(this, R.raw.guitar_music);
        mp2.start();
        mp2.setLooping(true);
        testButton.setOnClickListener( new View.OnClickListener() {
            public void onClick (View v) {
                final int status =(Integer) v.getTag();

                if(status == 1) {
                    mp2.start();
                    mp2.setLooping(true);
                    testButton.setText("Musik Of");
                    v.setTag(0); //pause
                } else {
                    mp2.pause();
                    testButton.setText("Musik On");
                    v.setTag(1);
                } //pause
            }
        });
    }

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

        btnoptn=(Button)findViewById(R.id.btnoptn);
        btnmenu=(Button)findViewById(R.id.btnmenu);

        btnoptn.setOnClickListener( new View.OnClickListener() {
        public void onClick(View view) {
            btnoptn(null);
        }

        });

        btnmenu.setOnClickListener( new View.OnClickListener() {
            public void onClick(View view2) {
            btnmenu();
        }
        });
    }

    protected void btnmenu() {
        try {
            Intent btnmenu= new Intent (this, menuenglish.class);
            startActivity(btnmenu);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void onStart() {
        super.onStart();
        btnoptn.setTag(0);
    }

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

这是一个非常简单的例子。我不知道如果你在不同的活动之间切换它是如何工作的,我从来没有真正使用过MediaPlayer类

public class MainActivity extends Activity 
{
    private MediaPlayer mediaPlayer;
    private Button musicButton;
    private OnClickListener listener = new OnClickListener()
    {
        // a boolean value in the names Onclicklistener class. 
        // this will help us to know, if the music is playing or not.
        boolean isPlaying = false;

        @Override
        public void onClick(View arg0)
        {
            if(isPlaying)
            {
                mediaPlayer.pause(); //stop the music
                musicButton.setText("Start"); //change the buttons text
            }
            else
            {
                mediaPlayer.start(); //start the music
                musicButton.setText("Stop"); //change the text
            }
            //changing the boolean value
            isPlaying = !isPlaying;
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        //Creating the mediaplayer with a desired soundtrack.
        mediaPlayer = MediaPlayer.create(this, R.raw.my_music);

        //Getting the Button reference from the xml
        musicButton = (Button) findViewById(R.id.music_button);

        //Setting the listener
        musicButton.setOnClickListener(listener);
    }
}

巴里182:谢谢你试着给出答案,真的很感激:)你很好!这就是你要找的吗?还是我完全误解了你?不知道,这让我头痛,每次我试着总是迫不及待地接近我遇到的那个人