Change the volume of a specific player and get his current volume

I have this class (my media player class), and I need to create 2 methods on it, one to change the volume and the other to get the current volume. (I create a breaker to change the volume of the player), normal.

public class LoopMediaPlayer { private Context ctx = null; private int rawId = 0; private MediaPlayer currentPlayer = null; private MediaPlayer nextPlayer = null; public static LoopMediaPlayer create(Context ctx, int rawId) { return new LoopMediaPlayer(ctx, rawId); } private LoopMediaPlayer(Context ctx, int rawId) { this.ctx = ctx; this.rawId = rawId; currentPlayer = MediaPlayer.create(this.ctx, this.rawId); currentPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { @Override public void onPrepared(MediaPlayer mediaPlayer) { currentPlayer.start(); } }); createNextMediaPlayer(); } private void createNextMediaPlayer() { nextPlayer = MediaPlayer.create(ctx, rawId); currentPlayer.setNextMediaPlayer(nextPlayer); currentPlayer.setOnCompletionListener(onCompletionListener); } private MediaPlayer.OnCompletionListener onCompletionListener = new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mediaPlayer) { mediaPlayer.release(); currentPlayer = nextPlayer; createNextMediaPlayer(); } }; public void stopPlayer() { if (currentPlayer != null && currentPlayer.isPlaying()) { currentPlayer.stop(); currentPlayer.release(); } if (nextPlayer != null && nextPlayer.isPlaying()) { nextPlayer.stop(); nextPlayer.release(); } } public void setVolume(float vol) { //todo currentPlayer.setVolume(vol, vol);//don't work } public int getVolume(){ //todo return 0; } } 

and my progress and seekBar listener

 seekBar.setProgress(player.getVolume());//use the method that retrieve the current volume seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, final int i, boolean b) { runOnUiThread(new Runnable() { @Override public void run() { player.setVolume(i);//use the method that change the volume } }); } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { } }); 

I tried mp.setVolume () but didn't change anything. And I can’t get the current volume to set as search engine progress before showing it.

Details All audio files are in the raw file (and not in the stream). At the same time, there will be more than one player , they will be saved, so I can get any of them at any time, and I want to change and get only the volume of a certain player, not all of them at the same time .

Does anyone know how I should apply both of these methods because I have no idea.

thanks

+1
source share

Source: https://habr.com/ru/post/951336/


All Articles