How do I play a sound only while an animated TextView is playing?

1

The sound I am using is from a keyboard being keyboard and was taken from this video = > link

Duration = > 2.577 seconds

I added it to the RAW folder of the project in question and would like to know how to make it play and repeat as long as the animated TextView was 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>
    
asked by anonymous 31.03.2017 / 01:57

1 answer

1

Hello, before calling the animation try

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

then according to the settings this question in the same block where we left the button visible you can stop the sound with teclado_02.stop();

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

  

If you were to use the code this answer would just add an AnimatorListener and in the onAnimationStart () method start the sound and the onAnimationEnd () method stops the sound.

    
17.05.2017 / 00:28