Media Player in android
// load media from indexof soundeffect(URl)
MediaPlayer mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource("http://www.mbuisc.ac.th/media-support/sound%20effect/03.BooDi-LimHyungJoo.MP3");
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
Toast.makeText(MainActivity.this, "music play", Toast.LENGTH_SHORT).show();
mediaPlayer.start();
}
});
mediaPlayer.prepareAsync();
package com.example.musicapp;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
TextView play,stop;
VideoView videoview;
SeekBar seek,seekbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
play = findViewById(R.id.play);
stop = findViewById(R.id.stop);
videoview = findViewById(R.id.videoView);
seek = findViewById(R.id.seekBar);
seekbar = findViewById(R.id.seekBar2);
// MediaPlayer mediaPlayer = MediaPlayer.create(this,R.raw.song);
// load media from indexof soundeffect(URl)
MediaPlayer mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource("http://www.mbuisc.ac.th/media-support/sound%20effect/03.BooDi-LimHyungJoo.MP3");
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
Toast.makeText(MainActivity.this, "music play", Toast.LENGTH_SHORT).show();
mediaPlayer.start();
}
});
mediaPlayer.prepareAsync();
play.setOnClickListener(v->{
if(mediaPlayer.isPlaying()){
mediaPlayer.pause();
}else{
mediaPlayer.start();
}
// videoview.start();
});
stop.setOnClickListener(v->{
mediaPlayer.pause();
});
setVolumeControlStream(AudioManager.STREAM_MUSIC);
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
seek.setMax(audioManager
.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
seek.setProgress(audioManager
.getStreamVolume(AudioManager.STREAM_MUSIC));
seek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
seek.setProgress(i);
Toast.makeText(MainActivity.this, "index"+i, Toast.LENGTH_SHORT).show();
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,i, 0);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
seekbar.setMax(mediaPlayer.getDuration());
new Timer().scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
seekbar.setProgress(mediaPlayer.getCurrentPosition());
}
},0,900);
seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
mediaPlayer.seekTo(i);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
}
Comments
Post a Comment