简体   繁体   中英

How to set duration time to play song in Android?

I have song which duration is 15min. But I would like to play this song for 30 minutes. Also when the user will download PRO version of the application, the song should play for 2 hours. How can I set the time?

I am new to Java and Android.

Thanks for your help.

public class MainActivity extends Activity {

Button start, stop;
// Button btn_play, btn_stop, btn_pause;
MediaPlayer mp;
TextView display, comment;
int length;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    length = 0;

    start = (Button) findViewById(R.id.bStart);
    start.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            mp = MediaPlayer.create(MainActivity.this, R.raw.splahsound);
            mp.seekTo(length);
            mp.start();

            mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

                @Override
                public void onCompletion(MediaPlayer mp) {

                    mp.stop();
                    mp.reset();
                    mp.release(); // free up memory
                    mp = null;
                    length = 0;
                }

            });

        }
    });

    stop = (Button) findViewById(R.id.bDur);
    stop.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            if (mp != null) {
                try {
                    mp.stop();
                    mp.reset();
                    mp.release();
                    mp = null;
                    length = 0;
                } catch (Exception e) {
                    Log.d("error", e.toString());
                }
            }
        }
    });
}

}

Use TimerTask. A similar question has already been answered here , so pointing to that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM