How to play a sound only while an animated Textview is playing?

Asked

Viewed 70 times

1

The sound I’m using is from a keyboard being keyboard and was taken from this video => https://www.youtube.com/watch?v=pcNlc0zTMuU

Duration => 2,577 seconds

I added it to the RAW folder of the project in question and would like to know how to make this sound play and repeat as long as the animated Textview is not fully played.

Typewriter:

package genesysgeneration.animatedtext;

import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.widget.TextView;

public class TypeWriter extends TextView {

    private CharSequence mText;
    private int mIndex;
    private long mDelay = 1;

    public TypeWriter(Context context){

        super(context);

    }

    public TypeWriter(Context context, AttributeSet attrs){

        super(context, attrs);

    }

    private Handler mHandler = new Handler();
    private Runnable characterAdder = new Runnable() {
        @Override
        public void run() {
            setText(mText.subSequence(0, mIndex++));
            if (mIndex<=mText.length()){

                mHandler.postDelayed(characterAdder, mDelay);

            }
        }
    };

    public void animatedText(CharSequence text){

        mText=text;
        mIndex=0;

        setText("");
        mHandler.removeCallbacks(characterAdder);
        mHandler.postDelayed(characterAdder, mDelay);

    }

    public void setCharacterDelay(long millis){

        mDelay=millis;

    }

}

Mainactivity:

package genesysgeneration.animatedtext;

import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        MediaPlayer teclado_02 = MediaPlayer.create(MainActivity.this, R.raw.teclado_02);

        TypeWriter tv = (TypeWriter)findViewById(R.id.tv);
        tv.setCharacterDelay(1);
        tv.animatedText("(nova execução) Pirulito ki bate bate, pirulito ki jah bateu!!!\n(nova execução) Pirulito ki bate bate, pirulito ki jah bateu!!!\n(nova execução) Pirulito ki bate bate, pirulito ki jah bateu!!!\n(nova execução) Pirulito ki bate bate, pirulito ki jah bateu!!!\n(nova execução) Pirulito ki bate bate, pirulito ki jah bateu!!!\n(nova execução) Pirulito ki bate bate, pirulito ki jah bateu!!!\n(nova execução) Pirulito ki bate bate, pirulito ki jah bateu!!!\n");

    }
}

XML (Mainactivity):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="genesysgeneration.animatedtext.MainActivity">

    <genesysgeneration.animatedtext.TypeWriter
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:text="TextView" />
</RelativeLayout>

  • If I was using this one’s code reply simply add an Animatorlistener and the method onAnimationStart() start the sound and method onAnimationEnd() stop the sound.

  • @I need you to help me, the code I used isn’t quite the same as your answer, can it come from chat? http://chat.stackexchange.com/rooms/56769/animation-on-textview

  • I wonder what part of the code exactly (put in the chat) I need to insert the sound

1 answer

1


Hello, before calling the animation try

teclado_02.setLooping(true);
teclado_02.start();

after according to the adjustments of this question in the same block where we left the visible button Voce can stop the sound with teclado_02.stop();

However it is not the best solution, I recommend improving your code with the link

If I was using the code of this answer would just add an Animatorlistener and in the method onAnimationStart() start the sound and in the method onAnimationEnd() stop the sound.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.