Android 2.2 cutting off MediaPlayer sound playback? - android

After some Googling it appears this is a common problem but I have yet to find an actual solution. I haven't tested on an actual device, but the emulator is cutting off my sound clips at around 80% done. I am playing back .wav files.
Does anyone know a programmatic solution to these problems?
edit:
public void play(Context context){
if (soundPlayer != null){
soundPlayer.release();
}
int rId = 0;
switch(aIndex){
case 0: rId = R.raw.c0; break;
case 1: rId = R.raw.c1; break;
case 2: rId = R.raw.c2; break;
case 3: rId = R.raw.c3; break;
case 4: rId = R.raw.c4; break;
case 5: rId = R.raw.c5; break;
case 6: rId = R.raw.c6; break;
case 7: rId = R.raw.c7; break;
case 8: rId = R.raw.c8; break;
case 9: rId = R.raw.c9; break;
case 10: rId = R.raw.c10; break;
case 11: rId = R.raw.c11; break;
case 12: rId = R.raw.c12; break;
case 13: rId = R.raw.c13; break;
case 14: rId = R.raw.v14; break;
case 15: rId = R.raw.v15; break;
case 16: rId = R.raw.v16; break;
case 17: rId = R.raw.v17; break;
case 18: rId = R.raw.v18; break;
case 19: rId = R.raw.v19; break;
case 20: rId = R.raw.v20; break;
case 21: rId = R.raw.v21; break;
case 22: rId = R.raw.v22; break;
case 23: rId = R.raw.v23; break;
default: rId = R.raw.error; break;
}
soundPlayer = MediaPlayer.create(context, rId);
if (soundPlayer != null){
soundPlayer.start();
}
}

Just discovered this question after having the same problem. The simple answer in my case was to make MediaPlayer a class variable. If you make it local to your method the garbage collector apparently sweeps it up and kills the sound play.

Well for starters, I would try to make sure MediaPlayer.create() function is called prior to the start(). If it's a game, load the sounds when they start a new game (creating a new media player for each sound). The reason why is because the create function will effectively load the sound file there and then, ready for smooth playback when you call start. If you load the file every time before running it, not only are you doing more work than you need to, but it may have undesired effects on the emulator. I'm not sure if you've noticed, but the emulator isn't exact the quickest tool out the shed compared to virtually any actual physical device. As a result, what I think may be happening is the emulator is 'playing' the sound and thinks that it is complete before the sound actually plays, mainly due to the emulator's slow speed.
Try it out on an actual device and I think you won't have any problems.

I think you need to do a release before to start ... example
if( mPlayer!= null ) mPlayer.release();
mPlayer = MediaPlayer.create(this, listaMP3[contador]);
mPlayer.start();

for soem reason if I define the mediaPlayer outside of the method I use to play a song, it works.
Even though I only call the playSong() method once;
mediaPlayer = MediaPlayer.create(mContext, R.raw.overworld);
mediaPlayer.setVolume(musicVolume, musicVolume);
playSong();
playSong Method:
private void playSong() {
if (!mediaPlayer.isPlaying())
mediaPlayer.start(); // no need to call prepare(); create() does that for you
}

Related

How to show progressbar for streaming video on surfaceView with libvlc?

I used libvlc (de.mrmaffen:vlc-android-sdk:1.0.6) library for http video streaming. Everything works fine just one issue.
I used progress-bar before my playmovie() function call,and stop the progress-bar with using libvlc.isPlaying() boolean function so at this time my guess is video is loaded and we will stop the progressbar.
How to get exact time for buffering the video and streaming start to stop the progressbar?
You need to implement EventListener.Like this way
LibVLC vlcInstance = new LibVLC(context, new VlcOptions().getDefaultOptions());
org.videolan.libvlc.MediaPlayer player = new org.videolan.libvlc.MediaPlayer(vlcInstance);
player.setEventListener(new org.videolan.libvlc.MediaPlayer.EventListener() {
#Override
public void onEvent(org.videolan.libvlc.MediaPlayer.Event event) {
switch (event.type) {
case org.videolan.libvlc.MediaPlayer.Event.Opening:
//Video Opening
break;
case org.videolan.libvlc.MediaPlayer.Event.Playing:
//Video Playing
break;
case org.videolan.libvlc.MediaPlayer.Event.Buffering:
//Video Buffering
break;
case org.videolan.libvlc.MediaPlayer.Event.Stopped:
//Video Stopped
break;
case org.videolan.libvlc.MediaPlayer.Event.EndReached:
//Video EndReached/Completed
break;
case org.videolan.libvlc.MediaPlayer.Event.EncounteredError:
//Video EncounteredError/Failed
break;
default:
break;
}
}
});
Media media = new Media(vlcInstance, videoUri);
media.addOption(":fullscreen");
media.setHWDecoderEnabled(true, false);
player.setMedia(media);
IVLCVout vlcOut = player.getVLCVout();

Get system time in millisecond using Buttons while recording video

I am recording a video using camera2api with a button to start the recording.Now when the recording starts, i need to get system time in milliseconds only if i press a specific button(other than the start rec button
) .
If not the system time, then at least the record time of the video being recorded.
The time should be stored only if i press the specific button. I have tried using system.currentTimeMillis() and systemclock.elapsedtime(), but once the recording starts and then i press the specific button to record time without stopping recorded ,it doesn't store any time details and shows 0.
Below is the code which i am using to store time:
case R.id.video: {
if (mIsRecordingVideo) {
stopRecordingVideo();
} else {
startTSRecordingVideo();
starttime = System.currentTimeMillis();
Log.d(TAG, "onClick:time "+starttime);
switch (view.getId()){
case R.id.stop:{
stoptime= SystemClock.elapsedRealtime()-starttime;
Log.d(TAG, "onClick:timefor slowstop "+stoptime);
}
break;
case R.id.timeshift:{
slstarttime=SystemClock.uptimeMillis()-starttime;
Log.d(TAG, "onClick:timefor slowstart"+slstarttime);
}
break;
}
}
Please tell me, how to get the time without affect the recording.
You have misplaced the stop and timeshift button event and it will not get called. It should be directly on the switch case.
Try this.
case R.id.video:
if (mIsRecordingVideo) {
stopRecordingVideo();
} else {
startTSRecordingVideo();
starttime = System.currentTimeMillis();
Log.d(TAG, "onClick:time "+starttime);
}
break;
case R.id.stop:
stoptime= SystemClock.elapsedRealtime()-starttime;
Log.d(TAG, "onClick:timefor slowstop "+stoptime);
break;
case R.id.timeshift:
slstarttime=SystemClock.uptimeMillis()-starttime;
Log.d(TAG, "onClick:timefor slowstart"+slstarttime);
break;

Android MediaPlayer not working after multiple starts

I am having trouble with MediaPlayer. In the enclosed code, when a button is touched, a voice cue is activated when the option to do so is selected. However, the voice works for approximately 31 times and then the sound goes off. Using the .release() method (which I have commented out for now) causes the sound to not work at all.
How can I correct this function?
Thank You
....
import android.media.MediaPlayer;
....
MediaPlayer mp = null;
........
// Causes the name of the appropriate cell to be announced
public void sayCellName() {
if(voiceChoice == false) return;
else{
switch(cellName){
case 1:
mp = MediaPlayer.create(this, R.raw.poly);
break;
case 2:
mp = MediaPlayer.create(this, R.raw.lymph);
break;
case 3:
mp = MediaPlayer.create(this, R.raw.mono);
break;
case 4:
mp = MediaPlayer.create(this, R.raw.eo);
break;
case 5:
mp = MediaPlayer.create(this, R.raw.baso);
break;
case 6:
mp = MediaPlayer.create(this, R.raw.band);
break;
default:
break;
} // end switch statement
} // end else statement;
mp.start();
// mp.release();
} // end of sayCellName
} // end of main
The Android OS has a hard-coded limit of audio track resources that may play back sound simultaneously. As it so happens that limit is 32 tracks that have to be shared by all running applications. What you are doing is wasting these tracks by creating fresh MediaPlayers on each button click without releasing them.
For your scenario you should probably create and reuse a single MediaPlayer instance and release it when your Activity stops. It is possible to reset a MediaPlayer and set a different stream data source afterwards.
You have to release the player OnPause or OnDestroy method.
#Override
public void onPause() {
if (mp!= null) mp.release();
}

play an audio clip that is stored in Phone Memory during the phone call

How to play an Audio clip that is stored in Phone Memory during the Phone call, and the same Audio clip should hear to Call receiver...
I am using simple code:
public void onCallStateChanged(int state, String incomingNumber) {
String stateString = "N/A";
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
stateString = "Idle";
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
stateString = "Off Hook";
break;
case TelephonyManager.CALL_STATE_RINGING:
stateString = "Ringing";
break;
}
Really I don't know how to write code to play audio file automatically while receiver will pick my call only for a particular number.....
There's no API in place in Android for playing audio over the voice call uplink. There may be phones out there that support it (I have not tested every single Android phone) but in general it's not going to work.
The best you could to is route the music to the loudspeaker, set the media volume to max and hope that it gets picked up by the microphone. That would sound awful though, so it's not something I'd recommend.

Android MediaPlayer SDK

I have a program with a function copied below it plays a sound upon clicking a button. If you click the button 10 times 10 different media players play the same sound that is the way i want it but how can i assign a button to stop all 10 media players at a time like a "STOP ALL BUTTON"
public void onClick(View v){
int resId = 0;
int stopped = 0;
switch (v.getId()){
case R.id.Wail:
resId = R.raw.fedsigwail;
break;
case R.id.Yelp:
resId = R.raw.fedsigyelp;
break;
case R.id.HiLow:
resId = R.raw.hilow;
break;
case R.id.FederalQ:
resId = R.raw.federalq;
break;
case R.id.Horn:
resId = R.raw.fedsignhorn;
break;
case R.id.STOPALL:
mp.stop();
mp.release();
stopped = 1;
break;
}
if (stopped != 1){
mp = MediaPlayer.create(this, resId);
mp.start();
}
}
The code above only stops the last instance of mp.
Any Input would be appreciated
mp.stop(); only stops one instance of a media player, since mp can only be one mediaplayer instance.
If mp is one of your media players, where are the other ones? You mentioned you have
10 different media players
so I would expect something like mp1, mp2, mp3, ..., mp10. But right now it seems you actually only using one media player instance, or at least stopping it.
With each call of
mp = MediaPlayer.create(this, resId)
you lose the reference to the previous created media player, since you 'override' it with a newly created instance.
You need to keep a reference to all your created media players, i.e. via an ArrayList or HashMap or similar.

Categories

Resources