Change the volume of app music in android - Eclipse - android

I have problem with my application
I have a main layout: main.xml, it includes a button named "Setting"
When I click the Setting button,
I use setContentView(R.layout.setting); to go to the setting.xml
In setting.xml, I have a seekbar to control the volume of app
I have followed this post "Using SeekBar to Control Volume in android?"
I do like this in my activity_main, but nothing happens when i change the seekbar

Here is the code:
//control volume using seek bar in Setting
private AudioManager audiomanager = null;
private SeekBar seekbar = null;
// SoundStatus
private TextView soundstatus = null;
// check Sound on
private boolean soundon = true;
// control Background Sound
private MediaPlayer mp = null;
// Sound icon
private ImageView sound = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
setContentView(R.layout.activity_main);
// Background music when the app launch
// music in raw folder
mp = MediaPlayer.create(this, R.raw.backgroundsound);
mp.start();
mp.setLooping(true);
// Control volume
ControlVolume();
}
...
...
...
public void ControlVolume()
{
seekbar = (SeekBar)findViewById(R.id.seekBar);
audiomanager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
seekbar.setMax(audiomanager
.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
seekbar.setProgress(audiomanager
.getStreamVolume(AudioManager.STREAM_MUSIC));
seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
#Override
public void onStopTrackingTouch(SeekBar arg0)
{
}
#Override
public void onStartTrackingTouch(SeekBar arg0)
{
}
#Override
public void onProgressChanged(SeekBar arg0, int progress, boolean arg2)
{
audiomanager.setStreamVolume(AudioManager.STREAM_MUSIC,
progress, 0);
}
});
}
this is MainActivity.java
main layout include button setting
and the seekbar is in setting layout

Related

How to check a variable in background to do something when it reaches to a value?

I have a simple VideoView that plays a video:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final VideoView videoView=findViewById(R.id.videoView);
videoView.setVideoURI(Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.video));
MediaController mediaController=new MediaController(this);
videoView.setMediaController(mediaController);
videoView.start();
TextView textView = findViewById(R.id.textView);
}
}
By using videoView.getCurrentPosition(), I can get the current elapsed milliseconds of the playing video. Now, I need to continuously check this value in background, and when it reaches to some (for example 10000), do something (updating the TextView's text).
It's important for me to do this is background so that doesn't disturb the video playback.
Is it possible?
You can listen to the MediaController's SeekBar events, by overriding its 3 methods, of course you' re interested in 1 of them onProgressChanged():
final VideoView videoView=findViewById(R.id.videoView);
videoView.setVideoURI(Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.video));
final MediaController mediaController=new MediaController(this);
videoView.setMediaController(mediaController);
final TextView textView = findViewById(R.id.textView);
textView.setText("0");
int topContainerId = getResources().getIdentifier("mediacontroller_progress","id", "android");
final SeekBar seekbar = (SeekBar) mediaController.findViewById(topContainerId);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
int currentValue = Integer.parseInt(textView.getText().toString());
int newValue = progress / 1000;
if (newValue != currentValue) {
textView.setText(String.valueOf(newValue));
}
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
videoView.start();

Separating MediaPlayers Volume

I can't separate the volume on each MediaPlayer. All I need to do is mp1 should have its own volume control as well as mp2. The codes are shown below. I even tried to create a setVolume() on each MediaPlayer but no luck!
Both seekBar1 and seekBar2 volumes works on both mp1 and mp2. I just need to separate them having seekBar1 should only be works for mp1 .
public class MainActivity extends AppCompatActivity {
MediaPlayer mp1;
MediaPlayer mp2;
AudioManager audioManager1;
AudioManager audioManager2;
...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mp1 = MediaPlayer.create(this, R.raw.audio1);
mp2 = MediaPlayer.create(this, R.raw.audio1);
audioManager1 = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
audioManager2 = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
int maxVolume1 = audioManager1.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
int curVolume1 = audioManager1.getStreamVolume(AudioManager.STREAM_MUSIC);
int maxVolume2 = audioManager2.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
int curVolume2 = audioManager2.getStreamVolume(AudioManager.STREAM_MUSIC);
SeekBar volumeControl1 = (SeekBar) findViewById(R.id.seekBar1);
SeekBar volumeControl2 = (SeekBar) findViewById(R.id.seekBar2);
volumeControl1.setMax(maxVolume1);
volumeControl2.setMax(maxVolume2);
volumeControl1.setProgress(curVolume1);
volumeControl2.setProgress(curVolume2);
volumeControl1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean b) {
audioManager1.setStreamVolume(AudioManager.STREAM_MUSIC, progress, 0);
}
});

How to control the audio using Android Media Player seek bar?

I am able to use the android media player to play audio files. I can display the progress bar and attached 2 buttons to pause and play the audio track. My problem is I cannot control the track using the progress bar. I want to playing audio track to update depending of where I drag the progress bar to.
Below is my code
public class MyMediaPlayerView extends AppCompatActivity implements View.OnClickListener {
/**
* var for controller
* restore timestamp today 12:11
*/
SeekBar seek_bar;
Button play_button, pause_button;
MediaPlayer player;
TextView text_shown;
Handler seekHandler = new Handler();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launch_webview);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getInit();
seekUpdation();
}
Runnable run = new Runnable() {
#Override public void run() {
seekUpdation();
}
};
public void seekUpdation() {
seek_bar.setProgress(player.getCurrentPosition());
seekHandler.postDelayed(run, 1000);
onProgressChanged(seek_bar, seek_bar.getProgress());
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.play_button: text_shown.setText("Playing...");
player.start();
break; case R.id.pause_button: player.pause();
text_shown.setText("Paused...");
}
}
public void onProgressChanged(SeekBar seek_Bar, int progress,
boolean fromUser) {
player.seekTo(progress);
}
}
Im doing it like this
seek_bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if(fromUser)
player.seekTo(progress);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
u forget to add listener
put this in your onCreate() :
seek_bar = (SeekBar)findViewById(R.id.yourseekbarhere);
seek_bar.setOnSeekBarChangeListener(this);
Should help u out

onProgressChanged() not called after moving progress button manually

I have the following code to add a SeekBar to a video:
final VideoView videoView = (VideoView) findViewById(R.id.videoView);
final MediaController mediaController = new MediaController(this) {
...
// Commented out irrelevant code.
}
mediaController.show();
mediaController.setAnchorView(videoView);
videoView.setMediaController(mediaController);
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
final int topContainerId1 = getResources().getIdentifier("mediacontroller_progress",
"id", "android");
final SeekBar seekbar = (SeekBar) mediaController.findViewById(topContainerId1);
seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
int overall_progress = 0;
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
...
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
videoView.seekTo((int) (videoView.getDuration() * (seekBar.getProgress() / 1000.0)));
}
});
}
});
Everything works fine, and the seeker indicator is aligned with the video while it is playing. When I move the seek button manually, the video goes to the right place.
My Problem: If I pause the video and then start playing again, the seekbar progress indicator no longer moves even though the video plays again. I checked to see if onProgressChanges() was called in this situation, but it is never even called! Does anyone know a fix for this? It appears that this person also had the problem, but nobody answered their post: Seekbar OnProgressChanged doesn't get fired when moving manually

using only one button to play and pause a Mediaplayer in Android

I am new to Android, my requirement is to use only one button for playing and pause using media player class?
Once you have your MediaPlayer set up, I would just set up in your onCreate() and onResume() methods, a check to see if the MediaPlayer is currently playing (MediaPlayer's isPlaying() method should work for you), and if it is playing, set the button's image and click handler to change it to a pause button. If the MediaPlayer isn't playing, then set it to be a play button.
You'll also need to handle listening for events such as when the MediaPlayer stops (finishes playing the audio file), as well as inverting the button state when you press it (i.e. pressing play changes button to pause, and vice versa).
I would use 2 buttons and hide one of them:
public class MyActivity extends Activity implements View.OnClickListener {
Button playBtn;
Button pauseBtn;
public void onCreate() {
playBtn = (Button) findViewById(R.id.playButton);
pauseBtn = (Button) findViewById(R.id.pauseButton);
playBtn.setOnClickListener(this);
pauseBtn.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.playButton:
// play music here
playBtn.setVisibility(Button.GONE);
pauseBtn.setVisibility(Button.VISIBLE);
break;
case R.id.pauseButton:
// pause music here
pauseBtn.setVisibility(Button.GONE);
playBtn.setVisibility(Button.VISIBLE);
break;
}
}
}
you can use a single Imagebutton and change the drawable image , in conjuction with a toggling boolean variable to check the state of the button (play/pause).
This is how I implemented it
ImageButton playButton;
private boolean playOn;
#Override
protected void onCreate(Bundle savedInstanceState) {
// some code here
playButton = (ImageButton)findViewById(R.id.btn_play);
//other specifications and your code
}
public void play(View view){
myplayfunction();
}
public void myplayfunction(){
if (playOn){
playOn=false;
playButton.setImageResource(R.drawable.icn_pause);
//your mediaplayer functions
}else{
playOn=true;
playButton.setImageResource(R.drawable.icn_play);
//pause the mediaplayer
}
}
Also, don't forget to toggle your imagebutton at the end, onCompletionListener() method of the mediaplayer
The following code worked for me. The value of the "playedLength" variable should also be initialize to zero, and set the "mediaPlaying" boolean value to false in the media player stop function to avoid errors.
public class MainActivity extends AppCompatActivity {
private Button btnPlay;
private MediaPlayer mPlayer;
private String mFileName = null;
private int playedLength; //variable for the CurrentPosition of audio when paused
private boolean mediaPlaying; // boolean variable for toggling play, pause and resume actions
// some custom codes for my app...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// some custom codes for my app.....
btnPlay = (Button) findViewById(R.id.button_play);
btnPlay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mediaPlaying){
mediaPlaying = false;
//pause the media player
mPlayer.pause();
playedLength = mPlayer.getCurrentPosition();
btnPlay.setBackgroundResource(R.mipmap.ic_pause_focused_background);
}else{
mediaPlaying = true;
if(mPlayer == null){
// create the media player
mPlayer = new MediaPlayer();
try {
mPlayer.setDataSource(mFileName);
mPlayer.prepare();
mPlayer.start();
} catch (IOException e) {
Log.e(LOG_TAG, "prepare() failed");
}
btnPlay.setBackgroundResource(R.mipmap.ic_play_focused_background);
} else
// resume playing
mPlayer.seekTo(playedLength);
mPlayer.start();
btnPlay.setBackgroundResource(R.mipmap.ic_play_focused_background);
}
}
});
}
final Button bPlay = (Button)findViewById(R.id.bPlay);
MediaPlayer song1 = MediaPlayer.create(tutorialFour.this, R.raw.fluet);
Button bStop = (Button)findViewById(R.id.bStop);
bPlay.setWidth(10);
song1.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
bPlay.setText("Play");
}
});
bPlay.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
b=true;
if(bPlay.getText().equals("Play") && b==true)
{
song1.start();
bPlay.setText("Pause");
b=false;
}
else if(bPlay.getText().equals("Pause"))
{
x=song1.getCurrentPosition();
song1.pause();
bPlay.setText("Resume");
Log.v("log",""+x);
b=false;
}
else if(bPlay.getText().equals("Resume") && b==true)
{
song1.seekTo(x);
song1.start();
bPlay.setText("Pause");
b=false;
}
}
});
Button
android:id="#+id/buttonPlay"
android:onClick="Play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="160dp"
android:layout_marginTop="243dp"
android:layout_marginEnd="163dp"
android:layout_marginBottom="100dp"
android:text="#string/play"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
//-----------------------------------------------------------------------
public class MainActivity extends AppCompatActivity {
MediaPlayer mediaPlayer;
boolean isPlaing = true;
Button buttonPlay;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonPlay = findViewById(R.id.buttonPlay);
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.stuff);
}
public void Play(View view) {
if(isPlaing) {
mediaPlayer.start();
buttonPlay.setText("Pause");
isPlaing = false;
}else {
mediaPlayer.pause();
buttonPlay.setText("Play");
isPlaing = true;
}
}
}

Categories

Resources