Twitter Facebook Delicious Digg Stumbleupon Favorites More

Sunday, 10 December 2017

basic play mp3 part 3

package com.example.viraj.mediaplayerapp;

import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.Toast;

//import static com.example.viraj.mediaplayerapp.R.raw.ashique;

public class MainActivity extends AppCompatActivity {
    private MediaPlayer mediaPlayer;
    private Button playbutton;
    private SeekBar M_seekBar;
    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        M_seekBar = (SeekBar) findViewById(R.id.id_seekbar);
        mediaPlayer = new MediaPlayer();
        mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.mercy);
        M_seekBar.setMax(mediaPlayer.getDuration());
//-------seekbar        M_seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override            public void onProgressChanged(SeekBar seekBar, int i_879, boolean b_123) {
                if (b_123) {
                    mediaPlayer.seekTo(i_879);
                }
            }

            @Override            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        //----------------seekbar        //set on completion method-----------------        mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override            public void onCompletion(MediaPlayer mediaPlayer_mp) {
                int duration = mediaPlayer_mp.getDuration();
                String mduration = String.valueOf(duration / 1000);
                Toast.makeText(getApplicationContext(), "Duration" + mduration, Toast.LENGTH_LONG).show();
            }
        });

        //----------------------------        playbutton = (Button) findViewById(R.id.MusicPlay);
        playbutton.setOnClickListener(new View.OnClickListener() {
            @Override            public void onClick(View view) {
                if (mediaPlayer.isPlaying()) {
                    //stop music and give option                    pausemusic();
                } else {
                    startmusic();
                }
            }
        });

    }

    //create method in side of our class    public void pausemusic() {
        if (mediaPlayer != null)
            mediaPlayer.pause();
        playbutton.setText("PLAY MUSIC");
    }

    public void startmusic()

    {
        if (mediaPlayer != null) {
            mediaPlayer.start();
            playbutton.setText("PAUSE MUSIC");
        }

    }

    //memory consume karta he iss liye    @Override    protected void onDestroy() {
        if (mediaPlayer != null && mediaPlayer.isPlaying()) {
            mediaPlayer.stop();
            mediaPlayer.release();
            mediaPlayer = null;
        }
        super.onDestroy();
    }
}
Share:

0 comments:

Post a Comment

Blogger Tutorials

Blogger Templates

Sample Text

Copyright © ANDROID TUTORIAL CODE | Powered by Blogger
Design by SimpleWpThemes | Blogger Theme by NewBloggerThemes.com & Distributed By Protemplateslab