【问题标题】:The song stopped to work after adding textview onclick添加 textview onclick 后歌曲停止工作
【发布时间】:2021-09-30 17:53:57
【问题描述】:

添加与播放按钮集成的 TextView 后,歌曲停止工作,所以我需要有关该问题的帮助

我想要的是当我点击播放歌曲应该可以工作并且成功显示的文本同时可以同时工作,我希望我能正确解释我想要什么

MainActivity.java 代码

package com.example.moh;

import android.media.MediaPlayer;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {
    MediaPlayer player;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView mytext;
        Button mybutton;
        mytext = (TextView)findViewById(R.id.TextBoxID);
        mybutton = (Button) findViewById(R.id.ButtonID);
        mybutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mytext.setText("The sound played successfully");
            }
        });
    }


    public void play(View v) {
        if (player == null) {
            player = MediaPlayer.create(this, R.raw.song);
            player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {

                }
            });
        }

        player.start();
    }

    public void pause(View v) {
        if (player != null) {
            player.pause();
        }
    }

}

activite_main.xml 代码:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    tools:context="com.codifying.multilayered.MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="57dp"
        android:background="#9ac4e9"
        android:scrollbarDefaultDelayBeforeFade="300"
        android:text="Play my favorite sound"
        android:textAlignment="center"
        android:textColor="@color/black"
        android:textSize="34sp"
        android:textStyle="bold|normal" />

    <Button
        android:id="@+id/ButtonID"
        android:layout_width="160dp"
        android:layout_height="76dp"
        android:onClick="play"
        android:text="Play"
        android:textSize="34sp" />

    <Button
        android:layout_width="161dp"
        android:layout_height="85dp"
        android:background="@color/white"
        android:onClick="pause"
        android:text="Pause"
        android:textColor="#FFFFFF"
        android:textColorHighlight="@color/white"
        android:textSize="34sp"
        tools:ignore="TextContrastCheck" />

    <TextView
        android:id="@+id/TextBoxID"
        android:layout_width="match_parent"
        android:layout_height="67dp"
        android:text="TextView" />

</LinearLayout>

【问题讨论】:

    标签: java android android-studio android-mediaplayer


    【解决方案1】:

    在您的 XML 布局中,您通过编写 android:onClick="play" 告诉 Android 框架,方法 void play(View) 是按钮 ButtonIDOnClickListener。所以当按钮被点击时该方法被调用。

    但是在您的onCreate 方法中,您将新的OnClickListener 分配给同一个按钮,这导致play 不再分配为OnClickListener 的问题。

    将新 OnClickListener 的内容移动到 void play(View) 方法:

    ...
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            TextView mytext;
            Button mybutton;
            mytext = (TextView)findViewById(R.id.TextBoxID);
            mybutton = (Button) findViewById(R.id.ButtonID);
        }
    
    
        public void play(View v) {
            if (player == null) {
                player = MediaPlayer.create(this, R.raw.song);
                player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer mp) {
    
                    }
                });
            }
    
            player.start();
            mytext.setText("The sound played successfully");
        }
    ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-25
      • 2017-11-25
      • 2020-08-04
      相关资源
      最近更新 更多