so im trying to make a sound play through android. ive created a mediaplayer like this
MediaPlayer player; (outside onCreate())
player = MediaPlayer.create(this, R.raw.theninth); (inside onCreate())
and then i reference this player throughout the Activity. .start() on shake. .pause onclick etc.. it looks like this
if(mag >= 500) {
if(!started) {
mainView.setText(R.string.waitforit);
player.start();
started = true;
if(doThisOnce) {
timer.schedule(changeText, 26000);
doThisOnce = false;
}
}
player.setVolume((float)(2*mag/3.0),(float)(2*mag/3.0));
}
and
public void stopAudio(View v) {
mainView.setText(R.string.shakeme);
player.pause();
started = false;
}
and so the problem is the player doesnt pause when i tap the screen, instead it goes to the starting text of hte app, and and its like i reloaded the entire app, and i can shake it and then another intance of the same file from the same player will start playing.. what do?
Related
I have a list of songs i put manually inside my app and it currently have 3 functions: start, pause and stop (using ImageViews).
The problem is that i can play multiple songs at the same time that is not supposed to be like that. I really cant find the issue here and hope someone can help.
This is a demonstration on my problem
I want it to STOP currently playing song when another song is being clicked on.
Here is my code for my play button:
viewholder.playB.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (currentSong == null || song != currentSong) {
currentSong = song;
mediaPlayer = MediaPlayer.create(context, song.getSong());
}
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
viewholder.playB.setImageResource(R.drawable.play_black);
} else {
mediaPlayer.start();
viewholder.playB.setImageResource(R.drawable.pause_black);
}
}
});
Instead of always creating a new instance of MediaPlayer every time the event onClick is called, you should create a single instance and reused it.
To play a new song don't forget to reset your player first. Here is an example:
mPlayer.reset();
mPlayer.setDataSource(context, song.getSong());
i create an app, and in one layout (just call it layout A), i play the media player, then when i went to another layout (just call it Layout B), i want the sound from the Layout A is continue playing in Layout B, and when i went back to the Layout A, i also want the media player is still continuing the music that was played before.
In Layout A, i set this code in onCreate:
player = MediaPlayer.create(this, R.raw.sound);
if(!isMuted())
{
player.setLooping(true);
player.start();
}
...
btn.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View arg0)
{
// TODO Auto-generated method stub
Intent intent = new Intent(A.this, B.class);
stop=1;
startActivity(intent);
}
});
And this code:
#Override
protected void onPause()
{
super.onPause();
if (stop!=1)
{
//stop=1 if i go to another layout, for example when i want to go to Layout B
//if the device is automatically locked, i want the media player is paused and it resumed when i unlock the device, so i use stop!=1 to know whether the sound should be paused or not
player.pause();
length = player.getCurrentPosition();
}
}
#Override
protected void onResume()
{
super.onResume();
if(!isMuted())
{
//isMuted is method to know whether the sound is muted or not, if it isn't muted, then the sound is resumed
player.seekTo(length);
player.setLooping(true);
player.start();
}
}
In layout B, i used this code in onCreate:
player = MediaPlayer.create(this, R.raw.sound);
....
And this code:
protected void onPause()
{
super.onPause();
player.pause();
length = player.getCurrentPosition();
}
#Override
protected void onResume()
{
super.onResume();
if(!isMuted())
{
if(player.isPlaying())
{
}
else
{
player.seekTo(length);
player.setLooping(true);
player.start();
}
}
}
But this is the problem.
When i went to Layout B, the Media Player from Layout A and Media Player from Layout B is played in the same time, so the sound is played simultaneously at one time.
When i went back to the Layout A, the Media Player in Layout B is stopped and the Media Player in Layout A is stopped and played from the beginning again, it didn't continue the Media Player that was played before.
When the device is locked, the media player is still played although i have used the indicator whether is should be paused or not. Any correction to the code?
I would recommend that you use a Service to play and pause/stop your music. It is not recommended to use Activity to handle music that way. You can start a Service and then play music in it. Services run in background and don't get destroyed automatically too often as compared to Activity. Here's a sample app code that does similar to what you need media player sound continue in all activities
In Activity MediaPlayer can play far.
You should use Service to play MediaPlayer and control from anywhere in application.
I used this tutorial.
https://thenewcircle.com/s/post/60/servicesdemo_using_android_service
I am in learning process of the Android app development, and trying to create one songs app.
Problem:
I have two activities, A:songslist & B:MediaPlayer
B plays remote song via mp.prepareAsync(); and starts player when its ready.
I want to keep running the song even though I move to other activities or if I open any other app.
But problem comes, when song is running and I go back to select new song, then oncreate activity B it starts streaming and playing new song, since old song is still running.
My Code:
public void Play() {
if(mp.isPlaying())
{
releaseMediaPlayer();
Log.d("MediaPlayer", "Player is already running release it first");
}
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
URL = BASE_URL + album_files[songIndex];
mp.setDataSource(URL);
mp.prepareAsync();
btnPlay.setImageResource(R.drawable.btn_pause);
songTitleLabel.setText("Loading track, please wait....");
}
private void releaseMediaPlayer() {
if (mp != null) {
if(mp.isPlaying()) {
mp.stop();
}
Log.d("MediaPlayer", "Player is released");
mp.release();
mp = null;
}
}
You should have a service for playing music beside playlists and mediaplayer activities. service plays the music even if you exit program and you can have a widget to control playing music.
Try this. Using the answer you can make a background service and also with out background service.
Good luck
I am trying to play two sound items, one after the other
MediaPlayer mp = null;
protected void produceErrorSound(int index) {
if (mp != null) {
mp.reset();
mp.release();
}
mp = MediaPlayer.create(this, index);
mp.start();
}
public void correctAnswerAndNext(){
produceErrorSound(R.raw.right1) ;
produceErrorSound(R.raw.right1) ;
}
but only second sound is produced.
is there any alternative approach?
I can't see any wait mechanism in your code.
You can use an onCompletionListener to receive a callback when your first MediaPlayer has finished playback. At that point you can start the second MediaPlayer.
An alternative way in Jellybean (and later versions) is to use the audio chaining functionality (i.e. setNextMediaPlayer) to automatically start another MediaPlayer as soon as the current one has finished playing. Something like this (I've omitted the calls to setDataSource etc for brevity):
mediaPlayer1.prepare();
mediaPlayer2.prepare();
mediaPlayer1.start();
mediaPlayer1.setNextMediaPlayer(mediaPlayer2);
I want to play a sound on button click in game.
I can easily do this using following code
mp = MediaPlayer.create(getApplicationContext(), R.raw.sound);
mp.start();
Issue is, my clicking rate is faster then playing duration of my sound clip.
How do I stop it and play again on my second or third click and so on.
I want to match sound play to match my clicking rate.
Thanks
Abhinav Tyagi
Firstly, you should use MediaPlayer only for large files, like music. For playing sound effects, SoundPool is the way to go.
An excellent tutorial on playing back media is available HERE.
On the click of your button, call soundPool.stop() and soundPool.play() immediately in sequence to stop and start your audio playback.
You can call MediaPlayer.Stop to stop it playing sounds. You can also call MediaPlayer.Reset to return it to the Idle state.
public class MainActivity extends AppCompatActivity {
MediaPlayer mplayer;
public void startPlay(View view) {
mplayer.start();
}
public void stopPlay(View view) {
mplayer.pause();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mplayer = MediaPlayer.create(this, R.raw.laugh);
}
}