想要将android MediaPlayer附加到隐形按钮

时间:2017-03-13 00:07:11

标签: java xml android-studio button android-mediaplayer

首先,我创建了一个简单的程序,当您单击按钮时播放媒体。

在我的主要活动课程中,我有:

 MediaPlayer mySound;    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mySound = MediaPlayer.create(this, R.raw.sleepnk);
    }

然后我创建了以下内容:

public void playMusic(View view)
    {
        mySound.start();
    }

然后在我的XML文件中,我创建了一个按钮并添加了:

android:onClick="playMusic"

现在我正在尝试将媒体添加到应用中,但它不具备以下内容:

<Button
        android:id="@+id/button"
        .

我的目标是为此添加媒体文件&#34;点击开始&#34;这个新应用程序中的隐形按钮,但由于xml文件中没有按钮,我不知道将playMusic方法附加到Tap to Start按钮的位置。我包括点按开始按钮的实例,以便您可以看到它如何充当按钮 -

values文件夹下有一个strings.xml,其中包含:

<?xml version="1.0" encoding="utf-8"?>

<string name="app_name">Panoramik</string>
<string name="instruction_tap_start">Tap to start</string>

然后在MainActivity.java文件中我们有:

private View.OnClickListener mCameraOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mIsCapturing) {
                //clear the flag to prevent the screen of being on
                getWindow().clearFlags(android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

                if (mDMDCapture.finishShooting()) {
                    mIsStitching = true;
                    mTextViewInstruction.setVisibility(View.INVISIBLE);
                }
                mIsCapturing = false;
                setInstructionMessage(R.string.instruction_tap_start);

我还包括&#34; setInstructionMessage&#34;的代码。方法:

private void setInstructionMessage(int msgID)
{
    if (mCurrentInstructionMessageID == msgID)
        return;

    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(mDisplayMetrics.widthPixels, RelativeLayout.LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.CENTER_HORIZONTAL);

    if (msgID == R.string.instruction_empty || msgID == R.string.instruction_hold_vertically || msgID == R.string.instruction_tap_start
            || msgID == R.string.instruction_focusing) {
        params.addRule(RelativeLayout.CENTER_VERTICAL);
    } else {
        params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    }

    mTextViewInstruction.setLayoutParams(params);
    mTextViewInstruction.setText(msgID);
    mCurrentInstructionMessageID = msgID;
}

有谁能告诉我如何将我的媒体文件sleepnk附加到Tap to Start隐藏按钮?

编辑:我基本上希望应用说“&#34;点击开始&#34;因为该应用程序是为视障人士创建的。因此,如果有任何其他建议让应用与用户交谈,请随时发表评论

1 个答案:

答案 0 :(得分:0)

您应该让操作系统和TalkBack读取“android:contentDescription”属性,方法是将字符串“Tap to Start”分配给您希望用户触摸的内容。 (记住要使用字符串资源,以便可以翻译/本地化。)