0% found this document useful (0 votes)
19 views2 pages

Media Player 2

This Java code defines a MainActivity class that controls audio playback and includes methods for playing, pausing, and stopping music. It initializes a MediaPlayer and AudioManager, sets up seek bars to control volume and playback position, and uses a Timer to update the playback position seek bar.

Uploaded by

Hassan Sheikh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views2 pages

Media Player 2

This Java code defines a MainActivity class that controls audio playback and includes methods for playing, pausing, and stopping music. It initializes a MediaPlayer and AudioManager, sets up seek bars to control volume and playback position, and uses a Timer to update the playback position seek bar.

Uploaded by

Hassan Sheikh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

package com.codewithharry.

musicxdev;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.SeekBar;

import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {


MediaPlayer player;
AudioManager audioManager;
// for playing the music
public void play(View view){
player.start();
}

// for pausing the music


public void pause(View view){
player.pause();
}

// for stopping the music


public void stop(View view){
player.stop();
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
player = MediaPlayer.create(this, R.raw.music);

audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);


int maxVol = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
int curVol = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);

SeekBar seekVol = findViewById(R.id.seekVol);


seekVol.setMax(maxVol);
seekVol.setProgress(curVol);

seekVol.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean
fromUser) {
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, progress,
0);

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {

}
});

final SeekBar seekProg = findViewById(R.id.seekProg);


seekProg.setMax(player.getDuration());

new Timer().scheduleAtFixedRate(new TimerTask() {


@Override
public void run() {
seekProg.setProgress(player.getCurrentPosition());
}
}, 0, 900);

seekProg.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean
fromUser) {
player.seekTo(progress);
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {

@Override
public void onStopTrackingTouch(SeekBar seekBar) {

}
});

}
}

You might also like