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);
}
});
Related
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();
Is this the correct way to use the MediaPlayer?
public class MainActivity extends AppCompatActivity{
MediaPlayer sound1, sound2, sound3, sound4;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Sound MediaPlayers
sound1= MediaPlayer.create(this, R.raw.sound1);
sound2= MediaPlayer.create(this, R.raw.sound2);
sound3= MediaPlayer.create(this, R.raw.sound3);
sound4= MediaPlayer.create(this, R.raw.sound4);
}
//sound onClick's
public void sound1(View view){
sound1.start();
}
public void sound2(View view){
sound2.start();
}
public void sound3(View view){
sound3.start();
}
public void sound4(View view){
sound4.start();
}
}
I have to clean up the MediaPlayer, but I don't know how I can do that.
I know that I have to use sound1.release() but it doesn't work if I write:
public void sound1(View view){
sound1.start();
sound1.release();
}
Please give me an example.
This should do the work for you.
public class MainActivity extends AppCompatActivity{
MediaPlayer mediaPlayer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//sound onClick's
public void sound1(View view){
cleanUpMediaPlayer();
mediaPlayer = MediaPlayer.create(this, R.raw.sound1);
mediaPlayer.start();
}
public void sound2(View view){
cleanUpMediaPlayer();
mediaPlayer = MediaPlayer.create(this, R.raw.sound2);
mediaPlayer.start();
}
public void sound3(View view){
cleanUpMediaPlayer();
mediaPlayer = MediaPlayer.create(this, R.raw.sound3);
mediaPlayer.start();
}
public void sound4(View view){
cleanUpMediaPlayer();
mediaPlayer = MediaPlayer.create(this, R.raw.sound4);
mediaPlayer.start();
}
public void cleanUpMediaPlayer(){
if(mediaPlayer != null) {
if(mediaPlayer.isPlaying()) {
mediaPlayer.stop();
}
mediaPlayer.release();
}
}
}
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
First I am using soundpool But with soundpool its difficult to do this task. So I am now using media player. In my app I am playing sound if user press on layout. But the problem is if user press layout again and again. Sound repeating again and again. I want layout become disable if sound is playing and enable when sound finish so that user able to play sound again when its finish.
Code-
private LinearLayout layout1;
MediaPlayer mp;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.boy);
mp = MediaPlayer.create(this, R.raw.test);
layout1 = (LinearLayout) findViewById (R.id.layout1);
layout1.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
mp.start();
});
}
private LinearLayout layout1;
MediaPlayer mp;
boolean flag=false;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.boy);
mp = MediaPlayer.create(this, R.raw.test);
layout1 = (LinearLayout) findViewById (R.id.layout1);
layout1.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
if(!flag){
mp.start();
layout1.setEnabled(false);
} else{flag =true ;}
});
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
flag=false;
layout1.setEnabled(true);
}
}); }
EDIT
You can do like this in onClickListener:
boolean flag;
layout1.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
if (flag == false){
flage=true;
layout1.setEnabled(false);
mp.start();
}
});
The value of flag is dependant on state of sound file.
The callback public void onCompletion(MediaPlayer mp) gives you a reference to the MediaPlayer.
public void onCompletion(MediaPlayer mp) {
flag = false;
layout1.setEnabled(true);
}
I have a simple player.
To create a 'connection' between SeekBar and MediaPlayer and I was touched seekbar anywhere where played and Sync seekbar with player. In your opinion, where is the problem?
what can i do ?
If you have an example in this respect share it
my code:
public class MediaPlaybackActivity extends Activity implements OnClickListener {
SeekBar seek_bar;
Button play_button, pause_button;
MediaPlayer player;
Handler seekHandler = new Handler();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getInit();
seekUpdation();
}
public void getInit() {
seek_bar = (SeekBar) findViewById(R.id.seek_bar);
play_button = (Button) findViewById(R.id.play_button);
pause_button = (Button) findViewById(R.id.pause_button);
play_button.setOnClickListener(this);
pause_button.setOnClickListener(this);
player = MediaPlayer.create(this, R.raw.win8);
seek_bar.setMax(player.getDuration());
seek_bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar seek_bar) {
}
#Override
public void onStartTrackingTouch(SeekBar seek_bar) {
}
#Override
public void onProgressChanged(SeekBar seek_bar, int progress, boolean fromUser) {
if(player != null && fromUser){
player.seekTo(progress * 1000);
}
}
});
}
Runnable run = new Runnable() {
#Override
public void run() {
seekUpdation();
}
};
public void seekUpdation() {
seek_bar.setProgress(player.getCurrentPosition());
seekHandler.postDelayed(run, 1000);
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.play_button:
player.start();
break;
case R.id.pause_button:
player.pause();
}
}}
Use the below method
AudioManager audioManager;
private int maxVolume;
private int curVolume;
/**
* To initialize the seek bar
*/
private void initialiseSeekBar() {
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
curVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
mSeekBarVolume.setMax(maxVolume);
mSeekBarVolume.setProgress(curVolume);
mSeekBarVolume
.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar arg0) {
}
#Override
public void onStartTrackingTouch(SeekBar arg0) {
}
#Override
public void onProgressChanged(SeekBar arg0, int arg1,
boolean arg2) {
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
arg1, 0);
}
});
}