Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

set countdown timer to play audio file android studio

public class MainActivity extends AppCompatActivity {

    private CountdownTimer timer = null;

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

        final SeekBar sb = (SeekBar) findViewById(R.id.seekBar);
        sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {}

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {}

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                restartTimer(seekBar);
            }
        }

    final Button bt_stop = (Button) findViewById(R.id.btn1);
    bt_stop.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(timer != null) {
                timer.cancel();
            }
        }
    });


    }    

    private void restartTimer(SeekBar sb) {
        if(timer!=null) {
            timer.cancel();
        }
        final long millis = TimeUnit.SECONDS.toMillis(sb.getProgress());
        final long interval =  TimeUnit.SECONDS.toMillis(1);
        timer = new CountdownTimer(millis, interval) {
            @Override
            public void onTick(long millisUntilFinished) {
                tv.setText(String.format("Seconds Remaining: %d", TimeUnit.MILLIS.toSeconds(millisUntilFinished));
            }

            @Override
            public void onFinish() {
                tv.setText("done!");
            }
        }
        timer.start();
    }

}
Comment

PREVIOUS NEXT
Code Example
Java :: ring check if the operating system is Android or not 
Java :: problemi arrayList javafx 
Java :: set background drawable programmatically android 
Java :: simple text formatter as in textbook 
Java :: java using the segment Information already before the for-loop 
Java :: java private access modifier 
Java :: how to put all words from a file in an array java 
Java :: spring media part max size 
Java :: trivers json node as node type 
Java :: turn array to image java 
Java :: java var keyword with example 
Java :: Java Creating ConcurrentHashMap from Other Maps 
Java :: java list sort comparator date descending lambda 
Java :: xml definition file for spring 
Java :: java.lang.ArrayIndexOutOfBoundsException: -1 
Java :: set attribute java 
Java :: expandablelistview android 
Java :: jav convert array as list to array 
Java :: how to scroll down chrome browser in selenium java 
Java :: es java painless source int value increase 
Java :: string stack in java 
Java :: fibonancci using dynamic programming in java 
Java :: Which one of the following values can a Java variable NOT have? 
Java :: how to add a command to a button java 
Java :: ferrari class in java 
Java :: Java double Keyword 
Java :: RedisCacheManager json serializer 
Java :: java get parent process name 
Java :: declar 2-d arraylist 
Java :: can abstract class have constructor in java 
ADD CONTENT
Topic
Content
Source link
Name
6+8 =