Trouble with sound effects at Onclick (Null Pointer and MediaPlayer Warning - android

I have created a simple Ukulele tuner (The market I think lacks a visually pleasing and extremely simple tuner).
Any how, firstly, through the developer console I can see that there is a Null Pointer Exception at the Button Onclick Events. I have not been able to recreate this, however it has been reported four times.
Secondly, looking at the log while using the app I can see this warning;
E/MP3Extractor(68): Unable to resync. Signalling end of stream.
This entry here MediaPlayer array causing null pointer in Android seems to be along the same lines.
How it works.
Through the use of radio buttons the user selects either to play a single note or a continuous note. I have created a subroutine called StopMediaPlayer that stops, resets and instantiates the MediaPlayers again. This was used because I could never seem to stop the continuous play back but only pause it.
Is the warning and the NullPointerException related? Is there a more efficient/better means of stopping MediaPlayer that will mean that I wont have to re instantiate the notes every time.
Thank You
One of the offending Onclicks
Button gButton = (Button) findViewById(R.id.gButton);
gButton.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
if (singleRadio.isChecked() == true)
{
StopMediaPLayer();
gNote.setLooping(false);
gNote.start();
}
else if (contRadio.isChecked() == true)
{
StopMediaPLayer();
gNote.setLooping(true);
gNote.start();
}
}
});
Stop Media Player Subroutine
public void StopMediaPLayer()
{
Log.i("UkuleleTuner", "Stop Media Player");
gNote.setLooping(false);
cNote.setLooping(false);
eNote.setLooping(false);
aNote.setLooping(false);
gNote.stop();
cNote.stop();
eNote.stop();
aNote.stop();
gNote.reset();
cNote.reset();
eNote.reset();
aNote.reset();
gNote = MediaPlayer.create(this, R.raw.g_note);
cNote = MediaPlayer.create(this, R.raw.c_note);
eNote = MediaPlayer.create(this, R.raw.e_note);
aNote = MediaPlayer.create(this, R.raw.a_note);
}

Related

MediaPlayer, Android: After a while, eventhandler fires automatically, even when event has not occured

I have in my app a media player, to play music. When the users presses "NEXT" the next song is build and played. This all works fine until about 20-30 "NEXT" clicks the eventhandler from the media player goes haywire and keeps firing even though the event "COMPLETITION" has not occured.
Here is my function to play music:
void PlayMusic(Android.Net.Uri uri)
{
CurrentSongObject = WriteMetaDataToFileList(uri.ToString());
txt_CurrentSong.Text = CurrentSongObject.SongName;
txt_CurrentArtist.Text = CurrentSongObject.ArtistName;
if (mediaPlayer.IsPlaying)
{
mediaPlayer.Stop();
mediaPlayer = MediaPlayer.Create(this, uri);
btn_StartOrPause.SetImageResource(Resource.Drawable.btn_play);
}
else
{
mediaPlayer.Stop();
mediaPlayer = MediaPlayer.Create(this, uri);
Activity_Player.btn_StartOrPause.SetImageResource(Resource.Drawable.btn_pause);
}
if (specialMode)
{
SeekToSongMillis(uri.ToString());
}
StartMediaPlayer();
mediaPlayer.Completion += delegate
{
if (rndMode)
{
ChooseRandomNewSongAndPlay(true);
}
else
{
ChoosesNonRandomNextSongAndPlay(true);
}
};
}
So about the last part, I sign an event handler to the event of the media player having completed the playback (the song is simply over.) But after calling this function about 25 times, the event handler keeps caling and therefore auto calling the next and next and next song. Even though no song ever completes, it just keeps skipping to the next song. Am I maybe using the handler wrong?
Please help me! :)
Thanks.
EDIT:
I have noticied something else.
after the first IF I unsigned from the event (also put the event into its own function) and this fixed the issue a bit. After a while he still goes into the event, even though he shoundlt, but then immediately leave it again and continues normally. So there is still something fishy going on...
Well, turns out there is this beautiful reset property on the mediaplayer...
mediaPlayer.Stop();
mediaPlayer.Completion -= EventForMediaPlayBackCompletition;
mediaPlayer.Reset();

Should I release or reset the MediaPlayer?

I haave my own Custom Adapter Class called WordAdapter, and I am using a Media Player(named pronounce-global variable in the WordAdapter class). I have different activities in which each list item has a linear layout(named as linearLayout). I am setting onClickListener to it so that when the Linear Layout is clicked, a sound file is played. On completion of playing that sound, I want to free off any unwanted memory. But I do not know if I should use release() or reset(). I have checked previous questions asked on SO before, but I don't think it provides precise explanation for my situation so as to use which method.
NOTE: I should be able to play other audio files after this one too (After completing playing this audio file, when I click on other items in the same activity, I should be able to get the sound.)
linearLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
pronounce = MediaPlayer.create(context, currentWord.getPronounceResourceID());
pronounce.start();
pronounce.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer player) {
//pronounce.release();
//pronounce.reset();
}
});
}
});
Do a reset before a release, but I suspect that only release is needed.
This might be easier to manage:
public void onClick(View view) {
if (pronounce != null) {
pronounce.reset();
pronounce.release();
pronounce = null;
}
pronounce = MediaPlayer.create(context, currentWord.getPronounceResourceID());
pronounce.start();
}
The reset method will simply stop any media and send the MediaPlayer instance back to the idle state. Exactly in the same state when it was created.
The release method destroys the media player and frees the majority of the unmanaged resources. When you call release, you should set the instance variable to null so that the remainder of the object is a candidate for garbage collection.
You might have some better performance if you just use reset and then reuse the existing mediaplayer instance on subsequent clicks.

Android Mediplayer not playing music

I am trying to play a song that is in remote server and is in this link. You also can check the song. but as per what i have coded the song is not getting played from the remote server.
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
try{
mySong = new MediaPlayer();
mySong.setDataSource("http://www.hrupin.com/wp-content/uploads/mp3/testsong_20_sec.mp3");
mySong.setAudioStreamType(AudioManager.STREAM_MUSIC);
mySong.prepareAsync();
mySong.start();
}
catch(Exception ee){
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText(ee.getMessage());
}
finally{
mySong.reset();
mySong.release();
}
}
});
are you serious?
You are starting it just to reset and release it instantly?find the wrong logic!
Or do you think the finally statement will be executed after the song is played through?
You call prepareAsync() in your code. Because you are preparing asynchronously, you're going to receive a callback called onPrepared(MediaPlayer) after you've declared that your Activity implements MediaPlayer.OnPreparedListener. It is here that you should be calling mySong.start(). Calling it right after prepareAsync would most likely cause an IllegalStateException to occur because the MediaPlayer isn't prepared yet. Finally, you'll want to set a MediaPlayer.OnCompletionListener so you can release the MediaPlayer there instead of the finally block. Also, resetting the MediaPlayer and releasing is redundant. If you're going to release it right away, there's no reason to reset it.

Why android application is not responding after a while loop by setting MediaPlayer to wait to finish?

I am not really sure if the error is because of I have an alarm or while loop:
MediaPlayer mpChange;
MediaPlayer saved;
mpChange = MediaPlayer.create(this, R.raw.change);
saved = MediaPlayer.create(this, R.raw.saved);
When I add a user I call a function and inside I informed the user message has saved by:
saved.start();
After that I check if user has special meaning or not and again inform the user if it does not:
else
{
/*Implement Audio Change*/
while(saved.isPlaying() == true)
{
//Nothing except wait
System.out.println("looop");
}
//saved.stop();
System.out.println("finished");
mpChange.start();
}
Code is working fine and I don't get any error or conflict between the voices. However, I get an error with in my application which says:
Application is not responding. Would you like to close?
I was not sure if it is the while loop or MediaPlayer.
Last output was:
looop
looop
looop
looop
finished
Thanks in advance for any help.
saved.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mpChange.start();
}
});
try this code in else part remove while loop

MediaPlayer force close after consecutive clicks

I've made a button and when you click it, it gives away a short sound(Max one second-sound). But after I've clicked the button about 20 times in a row I get force close..
The code is:
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Perform action on clicks
MediaPlayer mp = MediaPlayer.create(getBaseContext(), R.raw.buzzer);
mp.start();
}
});
I've tried with mp.stop(); but then my sound stops after it have been played half of the time...
One more thing, does anyone know how to "prepare" the sound when I click? Because the sound gets delayed with some milliseconds the first time I press the button.
Create a MediaPlayer member variable and initialize it in onCreate() the same way you are doing in the listener. Then in the listener just use this code:
if(mPlayer.isPlaying()) {
mPlayer.stop();
}
mPlayer.start();
Then call mPlayer.release() in your finish() Activity. My guess is that since none of your MediaPlayer instances are being released, it's running out of memory to use.
The official document for MediaPlayer is actually incredibly descriptive and helpful:
http://developer.android.com/reference/android/media/MediaPlayer.html

Categories

Resources