Sabtu, 15 Juni 2019

Belajar java.....mp3 coding....

yuk berbagi java languange utk android....silahkan compile dan run di aplikasi compiler anda...sekian terima kasih...

***
***


import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.SeekBar;
import android.widget.TextView;


public class Mp3MediaPlayer extends AppCompatActivity implements Runnable {
    
    MediaPlayer mp3player = new MediaPlayer();
    SeekBar seekBar;
    boolean play = false;
    FloatingActionButton fab;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mp3_media_player);
       
        fab = findViewById(R.id.button);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                playSong();
            }
        });

        final TextView seekBarHint = findViewById(R.id.textView);
        seekBar = findViewById(R.id.seekbar);
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                seekBarHint.setVisibility(View.VISIBLE);
            }
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {
                seekBarHint.setVisibility(View.VISIBLE);
                int x = (int) Math.ceil(progress / 1000f);
                if (x < 10)
                    seekBarHint.setText("0:0" + x);
                else
                    seekBarHint.setText("0:" + x);
                double percent = progress / (double) seekBar.getMax();
                int offset = seekBar.getThumbOffset();
                int seekWidth = seekBar.getWidth();
                int val = (int) Math.round(percent * (seekWidth - 2 * offset));
                int labelWidth = seekBarHint.getWidth();
                seekBarHint.setX(offset + seekBar.getX() + val
                        - Math.round(percent * offset)
                        - Math.round(percent * labelWidth / 2));
                if (progress > 0 && mp3player != null && !mp3player.isPlaying()) {
                    clearMediaPlayer();
                    fab.setImageDrawable(ContextCompat.getDrawable(Mp3MediaPlayer.this, android.R.drawable.ic_media_play));
                    Mp3MediaPlayer.this.seekBar.setProgress(0);
               }
            }
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                if (mp3player != null && mp3player.isPlaying()) {
                    mp3player.seekTo(seekBar.getProgress());
                }
            }
        });
    }
    public void playSong() {
        try {
            if (mp3player != null && mp3player.isPlaying()) {
                clearMediaPlayer();
                seekBar.setProgress(0);
                play = true;
                fab.setImageDrawable(ContextCompat.getDrawable(Mp3MediaPlayer.this, android.R.drawable.ic_media_play));
            }
            if (!play) {

                if (mp3player == null) {
                    mp3player = new MediaPlayer();
                }

                fab.setImageDrawable(ContextCompat.getDrawable(Mp3MediaPlayer.this, android.R.drawable.ic_media_pause));
                AssetFileDescriptor descriptor = getAssets().openFd("50_Cal_Shells_Drop.mp3");
                mp3player.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength());
                descriptor.close();
                mp3player.prepare();
                mp3player.setVolume(0.5f, 0.5f);
                mp3player.setLooping(false);
                seekBar.setMax(mp3player.getDuration());
                mp3player.start();
                new Thread(this).start();
            }
            play = false;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void run() {
        int currentPosition = mp3player.getCurrentPosition();
        int total = mp3player.getDuration();
        while (mp3player != null && mp3player.isPlaying() && currentPosition < total) {
            try {
                Thread.sleep(1000);
                currentPosition = mp3player.getCurrentPosition();
            } catch (InterruptedException e) {
                return;
            } catch (Exception e) {
                return;
            }
            seekBar.setProgress(currentPosition);
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        clearMediaPlayer();
    }

    private void clearMediaPlayer() {
        mp3player.stop();
        mp3player.release();
        mp3player = null;
    }
}

Tidak ada komentar:

Posting Komentar

bangsat

 goblok