handle media player error gracefully - android

I am trying to handle a media player error, when the error would occur, I want to toast a message and then play next song , however whenever the error occurs it would just crash.
here is the code I am trying
public class player1 extends ActionBarActivity implements View.OnClickListener,OnErrorListener{
MediaPlayer mp;
ArrayList<File> mySongs;
int position;
Uri u;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_player);
Intent i = getIntent();
Bundle b = i.getExtras();
mySongs = (ArrayList) b.getParcelableArrayList("songlist");
position = b.getInt("pos", 0);
u = Uri.parse(Uri.encode(mySongs.get(position).toString()));
mp = MediaPlayer.create(getApplicationContext(), u);
mp.start();
}
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
String errorWhat;
switch(what){
case MediaPlayer.MEDIA_ERROR_UNKNOWN:
errorWhat = "MEDIA_ERROR_UNKNOWN";
break;
case MediaPlayer.MEDIA_ERROR_SERVER_DIED:
errorWhat = "MEDIA_ERROR_SERVER_DIED";
break;
default:
errorWhat = "!";
}
String errorExtra;
switch(extra){
case MediaPlayer.MEDIA_ERROR_IO:
errorExtra = "MEDIA_ERROR_IO";
break;
case MediaPlayer.MEDIA_ERROR_MALFORMED:
errorExtra = "MEDIA_ERROR_MALFORMED";
break;
case MediaPlayer.MEDIA_ERROR_UNSUPPORTED:
errorExtra = "MEDIA_ERROR_UNSUPPORTED";
break;
case MediaPlayer.MEDIA_ERROR_TIMED_OUT:
errorExtra = "MEDIA_ERROR_TIMED_OUT";
break;
default:
errorExtra = "!";
}
Toast.makeText(player1.this,
"Error" + "\n"
+ errorWhat + "\n"
+ errorExtra,
Toast.LENGTH_LONG).show();
//release
mp.stop();
mp.release();
initMediaPlayer();
return true;
};
private void initMediaPlayer() {
Toast.makeText(player1.this,
"playomng next song",
Toast.LENGTH_LONG).show();
position = (position + 1)%mySongs.size();
u = Uri.parse(Uri.encode(mySongs.get(position).toString()));
mp = MediaPlayer.create(getApplicationContext(), u);
mp.start();
}
}
How can I manage to handle these errors without forcing the media player to close?

Rather than just putting a band aid on your code it may be wiser to find where the actual problem is. If you can find what line(s) you're receiving an error you can simply wrap the code in a
try{
//code that crashes here
}
catch(Exception e){
Toast.makeText(context, "Media error", LENGTH_SHORT).show();
}
EDIT:
Be aware that simply preventing a crash via a try/catch can cause bugs itself. If you can post your logcat we may be able to figure out what your actual error is.

Without logcat we can not say much, however you did not set the
player to listen to the errors mp.setOnErrorListener(this);.
I think your crash might be i.getExtras(); it can be null, therefore calling b.getParcelableArrayList("songlist"); might just crash your application.

Related

How to execute both Speech Recognition and Audio recording at the same time?

THIS IS NOT A DUPLICATE, HERE I TALK ABOUT STOPPING AND STARTING THE RECORD PROCESS WHENEVER I WANT. BEFORE MARKING AS DUPLICATES, PLEASE READ THE OTHER ANSWER PROPERLY.
I am developing a Phonegap plugin for Android. This plugin will basically support the Android Speech Recognition and recording the speech. I am capable of starting, stopping etc the speech recognition, but got serious issues with recording. First, the code is posted below.
Below is how I start the speech, end it etc.
public boolean execute(final String action, final JSONArray args, final CallbackContext callbackContext) throws JSONException
{
final int duration = Toast.LENGTH_SHORT;
this.callBackContext = callbackContext;
// Shows a toast
Log.v(TAG,"CoolPlugin received:"+ action);
if(INITIALIZE.equals(action))
{
cordova.getActivity().runOnUiThread(new Runnable() {
public void run() {
init();
}
});
}
else if(PLAY.equals(action))
{
cordova.getActivity().runOnUiThread(new Runnable() {
public void run() {
//startRecording();
play(callbackContext);
timeMilli = System.currentTimeMillis();
timer();
AudioRecorder recorder = AudioRecorder.getInstance();
recorder.startRecording();
}
});
}
else if(STOP.equals(action))
{
cordova.getActivity().runOnUiThread(new Runnable() {
public void run() {
stop();
AudioRecorder recorder = AudioRecorder.getInstance();
recorder.stopRecording();
}
});
}
return true;
}
public void play(CallbackContext callbackContext)
{
//Mute the sound
mAudioManager = (AudioManager) cordova.getActivity().getSystemService(Context.AUDIO_SERVICE) ;
mStreamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC); // getting system volume into var for later un-muting
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, 0); // setting system volume to
//Start listening
speech.stopListening();
speech.cancel();
speech.destroy();
createRecog();
speech.startListening(recognizerIntent);
//adapter.clear();
}
public String getErrorText(int errorCode) {
String message;
switch (errorCode) {
case SpeechRecognizer.ERROR_AUDIO:
message = "Audio recording error";
break;
case SpeechRecognizer.ERROR_CLIENT:
message = "Client side error";
break;
case SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS:
message = "Insufficient permissions";
break;
case SpeechRecognizer.ERROR_NETWORK:
message = "Network error";
break;
case SpeechRecognizer.ERROR_NETWORK_TIMEOUT:
message = "Network timeout";
break;
case SpeechRecognizer.ERROR_NO_MATCH:
message = "No match";
break;
case SpeechRecognizer.ERROR_RECOGNIZER_BUSY:
message = "RecognitionService busy";
break;
case SpeechRecognizer.ERROR_SERVER:
message = "error from server";
break;
case SpeechRecognizer.ERROR_SPEECH_TIMEOUT:
message = "No speech input";
break;
default:
message = "Didn't understand, please try again.";
break;
}
return message;
}
Below is my code for Recording the speech. It is a separated class.
import java.io.FileDescriptor;
import java.io.IOException;
import java.io.File;
import android.media.MediaRecorder;
import android.os.Environment;
import android.util.Log;
public class AudioRecorder {
private static final String LOG_TAG = "AudioRecoder";
private MediaRecorder mRecorder;
String file;
public static AudioRecorder instance;
public static AudioRecorder getInstance()
{
if(instance==null)
{
instance = new AudioRecorder();
}
return instance;
}
private AudioRecorder()
{
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/MyFolder/";
File dir = new File(path);
if(!dir.exists())
dir.mkdirs();
// String myfile = path + "filename" + ".mp4";
file=path + "filename2" + ".3gp";
}
public void startRecording() {
new Thread(new Runnable(){
#Override
public void run() {
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setOutputFile(file);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
try {
mRecorder.prepare();
Log.e(LOG_TAG, "prepare() success");
} catch (IOException e) {
// Log.e(LOG_TAG, "prepare() failed");
Log.d(LOG_TAG, "prepare() failed", e);
}
mRecorder.start();
Log.e(LOG_TAG, "Working");
}}).start();
}
public void stopRecording() {
mRecorder.stop();
mRecorder.release();
mRecorder = null;
}
}
Now, there is an issue. When starting the recognition service, if I didn't use the below code, then speech recognition is working fine.
AudioRecorder recorder = AudioRecorder.getInstance();
recorder.startRecording();
However if I use the above code, then I get the following error
02-03 15:33:21.555: D/VoiceRecognitionActivity(32066): FAILED Network error
02-03 15:33:21.575: E/SpeechRecognizer(32066): not connected to the recognition service
02-03 15:33:21.575: E/SpeechRecognizer(32066): not connected to the recognition service
Basically speaking, if I execute the recording process, then the recognition process is giving errors. What is happening here please?
In the recording process I will have to make functions where the user can start the recording at anytime he wants and stop it at anytime he likes.

SoundPool: sample 1 not READY

The problem SoundPool when I call it in fragments onCreateView, onStart or onResume application to slow down.
And when I try to get the ID by pressing the button, the sound does not appear Logcat writes "SoundPool: sample 1 not READY"
please help me,Any ideas? Thanks!
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_carnivore, container, false);
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
Sound.createOldSoundPool();
} else {
Sound.createNewSoundPool();
}
mAlligatorSound = Sound.loadSound(getContext(),"alligator.wav");
mBatSound = Sound.loadSound(getContext(), "bat.mp3");
mBearSound = Sound.loadSound(getContext(),"bear.wav");
mBobcatSound=Sound.loadSound(getContext(),"bobcat.mp3");
mCatSound= Sound.loadSound(getContext(),"cat.wav");
mCheetahSound= Sound.loadSound(getContext(),"cheetah.wav");
mCoyoteSound= Sound.loadSound(getContext(),"coyote.wav");
mCrocodileSound= Sound.loadSound(getContext(),"crocodile.wav");
mDogSound= Sound.loadSound(getContext(),"dog.wav");
mFoxSound= Sound.loadSound(getContext(),"fox.mp3");
mHyenaSound= Sound.loadSound(getContext(),"hyena.wav");
mJaguarSound= Sound.loadSound(getContext(),"jaguar.wav");
mKittenSound= Sound.loadSound(getContext(),"kitten.wav");
mLeopardSound= Sound.loadSound(getContext(),"leopard.wav");
mLionSound= Sound.loadSound(getActivity(),"lion.wav");
mPuppySound= Sound.loadSound(getActivity(),"puppy.wav");
mRattleSnakeSound= Sound.loadSound(getActivity(),"rattlesnake.mp3");
mSnakeSound= Sound.loadSound(getActivity(),"snake.wav");
mTigerSound= Sound.loadSound(getActivity(),"tiger.wav");
mWolfSound= Sound.loadSound(getActivity(),"wolf.wav");
int idList[]={R.id.imageButtonAlligator,R.id.imageButtonBat,R.id.imageButtonBear,R.id.imageButtonBobCat,R.id.imageButtonCat,
R.id.imageButtonCheetah,R.id.imageButtonCoyote,R.id.imageButtonCrocodile,R.id.imageButtonDog,R.id.imageButtonFox,
R.id.imageButtonHyena,R.id.imageButtonJaguar,R.id.imageButtonKitten,R.id.imageButtonLeopard,R.id.imageButtonLion,
R.id.imageButtonPuppy,R.id.imageButtonRattlesnake,R.id.imageButtonSnake,R.id.imageButtonTiger,R.id.imageButtonWolf};
for (int id :idList){
ImageButton imageButton = (ImageButton)view.findViewById(id);
imageButton.setOnClickListener(onClickListener);
}
return view;
}
View.OnClickListener onClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.imageButtonAlligator:
Sound.playSound(mAlligatorSound);
Toast.makeText(view.getContext(),R.string.Alligator, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonBat:
Sound.playSound(mBatSound);
Toast.makeText(view.getContext(),R.string.Bat, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonBear:
Sound.playSound(mBearSound);
Toast.makeText(view.getContext(),R.string.Bear, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonBobCat:
Sound.playSound(mBobcatSound);
Toast.makeText(view.getContext(),R.string.BobCat, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonCat:
Sound.playSound(mCatSound);
Toast.makeText(view.getContext(),R.string.Cat, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonCheetah:
Sound.playSound(mCheetahSound);
Toast.makeText(view.getContext(),R.string.Cheetah, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonCoyote:
Sound.playSound(mCoyoteSound);
Toast.makeText(view.getContext(),R.string.Coyote, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonCrocodile:
Sound.playSound(mCrocodileSound);
Toast.makeText(view.getContext(),R.string.Crocodile, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonDog:
Sound.playSound(mDogSound);
Toast.makeText(view.getContext(),R.string.Dog, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonFox:
Sound.playSound(mFoxSound);
Toast.makeText(view.getContext(),R.string.Fox, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonHyena:
Sound.playSound(mHyenaSound);
Toast.makeText(view.getContext(),R.string.Hyena, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonJaguar:
Sound.playSound(mJaguarSound);
Toast.makeText(view.getContext(),R.string.Jaguar, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonKitten:
Sound.playSound(mKittenSound);
Toast.makeText(view.getContext(),R.string.Kitten, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonLeopard:
Sound.playSound(mLeopardSound);
Toast.makeText(view.getContext(),R.string.Leopard, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonLion:
Sound.playSound(mLionSound);
Toast.makeText(view.getContext(),R.string.Lion, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonPuppy:
Sound.playSound(mPuppySound);
Toast.makeText(view.getContext(),R.string.Puppy, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonRattlesnake:
Sound.playSound(mRattleSnakeSound);
Toast.makeText(view.getContext(),R.string.RattleSnake, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonSnake:
Sound.playSound(mSnakeSound);
Toast.makeText(view.getContext(),R.string.Snake, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonTiger:
Sound.playSound(mTigerSound);
Toast.makeText(view.getContext(),R.string.Tiger, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonWolf:
Sound.playSound(mWolfSound);
Toast.makeText(view.getContext(),R.string.Wolf, Toast.LENGTH_SHORT).show();
break;
}
}
};
When you click on the sound does not work when the method is called Sound.loadSound (..., ...), and Logcat writes "SoundPool: sample 1 not READY"
switch (v.getId()) {
case R.id.imageButtonAlligator:
mAlligatorSound = Sound.loadSound(getContext(),"alligator.wav");
Sound.playSound(mAlligatorSound);
Toast.makeText(view.getContext(),R.string.Alligator, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonBat:
mBatSound = Sound.loadSound(getContext(), "bat.mp3");
Sound.playSound(mBatSound);
Toast.makeText(view.getContext(),R.string.Bat, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonBear:
mBearSound = Sound.loadSound(getContext(),"bear.wav");
Sound.playSound(mBearSound);
Toast.makeText(view.getContext(),R.string.Bear, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonBobCat:
mBobcatSound=Sound.loadSound(getContext(),"bobcat.mp3");
Sound.playSound(mBobcatSound);
Toast.makeText(view.getContext(),R.string.BobCat, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonCat:
mCatSound= Sound.loadSound(getContext(),"cat.wav");
Sound.playSound(mCatSound);
Toast.makeText(view.getContext(),R.string.Cat, Toast.LENGTH_SHORT).show();
break;
this is th Static Method Sound
public class Sound {
public static SoundPool mSoundPool;
public static AssetManager mAssetManager;
public static int mStreamID;
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static void createNewSoundPool() {
AudioAttributes attributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_GAME)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.build();
mSoundPool = new SoundPool.Builder()
.setAudioAttributes(attributes)
.build();
}
#SuppressWarnings("deprecation")
public static void createOldSoundPool() {
mSoundPool = new SoundPool(3, AudioManager.STREAM_MUSIC, 0);
}
public static int playSound(int sound) {
if (sound > 0) {
mStreamID = mSoundPool.play(sound, 1, 1, 1, 0, 1);
}
return mStreamID;
}
public static int loadSound(Context context , String fileName) {
mAssetManager = context.getAssets();
AssetFileDescriptor afd;
try {
afd = mAssetManager.openFd(fileName);
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(context, "Не могу загрузить файл " + fileName,
Toast.LENGTH_SHORT).show();
return -1;
}
return mSoundPool.load(afd, 1);
}
}
Before doing anything with the soundpool, after you instantiate it, you need to set a listener to know when its actually ready to be used, it takes time to load the sound into memory: do this:
boolean loaded = false;//class variable
//in onCreate:
soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
#Override
public void onLoadComplete(SoundPool soundPool, int sampleId,
int status) {
loaded = true;
}
});
then before you use the soundPool you need to check if Loaded is true.
For example:
if (loaded) {
soundPool.play(soundID, volume, volume, 1, 0, 1f);
Log.e("Test", "Played sound");
}

Android Handler.postDelayed interrupts sound playing

I am using this code to play a sound
final MediaPlayer mp = MediaPlayer.create(this, R.raw.sound);
mp.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
It works fine on its own, however there was a problem after I added an animation that extends ImageView, which refreshes(by calling handler.postDelayed) the image resource at an interval about 30ms to create animation. The problem is that when the animation starts, it terminates the the playing of the sound. Here is the code for the Runnable that refreshes the ImageView.
private Runnable runnable = new Runnable () {
public void run() {
String name = "frame_" + frameCount;
frameCount ++;
int resId = mContext.getResources().getIdentifier(name, "drawable", mContext.getPackageName());
imageView.setImageResource(resId);
if(frameCount < totalFrameCount) {
mHandler.postDelayed(runnable, interval);
}
}
};
I also tried to use a thread that calls the anmiationView.postInvalidate to do the animation, however it has the same problem. Please help. Thanks
Edit:
It looks like the problem is due to WHEN the animation is called. Previously I called it in the onActivityResult of the activity. Looks like this is not the right place to call. Now I put the animation view in a popupWindow and play it there, it works properly. Not sure exactly why.
in handler's comments :
"A Handler allows you to send and process {#link Message} and Runnable
objects associated with a thread's {#link MessageQueue}. Each Handler
instance is associated with a single thread and that thread's message
queue. When you create a new Handler, it is bound to the thread /
message queue of the thread that is creating it -- from that point on,
it will deliver messages and runnables to that message queue and execute
them as they come out of the message queue."
so, the problem may be caused by both of animation and media playing operations are in
the same message queue own by which thread create the handler (let's say the main thread).
if the animation loops for ever, then the media player will hardly get any chance to run.
you could take it a try with HandlerThread, the thread will contain a new looper for the
handler created from it, all the runnables added to that handler will be running in another
individual thread.
the animation thread and the media play thread should be running in the different threads not
scheduling in the same one.
hope, it helps.
the HandlerThread usage and some discuss looks like this :
How to create a Looper thread, then send it a message immediately?
maybe it is caused by your miss arranged codes, i take it a try on my nexus 4 with android version 4.4.2, even no any cache tech, the animation and music works like a charm...
here is the major codes :
public class MainActivity extends Activity implements View.OnClickListener {
protected static final String TAG = "test002" ;
protected static final int UPDATE_ANI = 0x0701;
protected static final int UPDATE_END = 0x0702;
protected static final int[] ANI_IMG_IDS = {R.raw.img1, R.raw.img2, R.raw.img3, R.raw.img4,
R.raw.img5, R.raw.img6, R.raw.img7};
protected static final int[] BTN_IDS = {R.id.btnStart, R.id.btnStop};
protected android.os.Handler aniHandler = null; // async update
protected boolean isAniRunning = false ;
protected int aniImgIndex = 0 ;
protected ImageView aniImgView = null ;
protected MediaPlayer mediaPly = null ;
// animation timer
class AniUpdateRunnable implements Runnable {
public void run() {
Message msg = null ;
while (!Thread.currentThread().isInterrupted() && isAniRunning) {
msg = new Message();
msg.what = UPDATE_ANI;
aniHandler.sendMessage(msg);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
break ;
}
}
msg = new Message() ;
msg.what = UPDATE_END ;
aniHandler.sendMessage(msg) ;
}
}
protected void prepareMediaPlayer(MediaPlayer mp, int resource_id) {
AssetFileDescriptor afd = getResources().openRawResourceFd(resource_id);
try {
mp.reset();
mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getDeclaredLength());
afd.close();
mp.prepare();
} catch (IllegalArgumentException e) {
Log.d(TAG, "IlleagalArgumentException happened - " + e.toString()) ;
} catch(IllegalStateException e) {
Log.d(TAG, "IllegalStateException happened - " + e.toString()) ;
} catch(IOException e) {
Log.d(TAG, "IOException happened - " + e.toString()) ;
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// init : buttons onclick callback
{
Button btn;
int i;
for (i = 0; i < BTN_IDS.length; i++) {
btn = (Button) findViewById(BTN_IDS[i]);
btn.setOnClickListener(this);
}
}
// init : update animation handler callback
{
aniHandler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case UPDATE_ANI:
updateAniImages();
break ;
case UPDATE_END:
updateAniEnd();
break ;
default:
break;
}
}
};
}
// init : prepare image view
{
aniImgView = (ImageView)findViewById(R.id.imgAni) ;
mediaPly = MediaPlayer.create(this, R.raw.happyny) ;
mediaPly.setLooping(true);
}
}
protected void updateAniImages() {
if(aniImgIndex >= ANI_IMG_IDS.length) {
aniImgIndex = 0 ;
}
InputStream is = getResources().openRawResource(ANI_IMG_IDS[aniImgIndex]) ;
Bitmap bmp = (Bitmap) BitmapFactory.decodeStream(is) ;
aniImgView.setImageBitmap(bmp);
aniImgIndex++ ;
}
protected void updateAniEnd() {
aniImgIndex = 0 ;
aniImgView.setImageBitmap(null);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnStart:
isAniRunning = true ;
// no re-enter protectiion, should not be used in real project
new Thread(new AniUpdateRunnable()).start();
mediaPly.start();
break;
case R.id.btnStop:
isAniRunning = false ;
mediaPly.stop();
prepareMediaPlayer(mediaPly, R.raw.happyny);
break;
default:
break;
}
}
}
the major project codes and test apk should be find here :
apk installer
source code

Stop tts if the call is in offHook

I am doing an application which speaks the caller name after 4 sec of the ring tone.The problem is it speaks the caller Name after two or three rings. The problem is it keeps speaking the caller name even if the call is in offhook or idle I don't want so.
the code in onreceive of broadcast receiver is
state = bundle.getString(TelephonyManager.EXTRA_STATE);
if(state.equals( TelephonyManager.CALL_STATE_OFFHOOK))
{
System.out.println("fjkerj");
}
else if(state.equals( TelephonyManager.CALL_STATE_IDLE))
{
}
else if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING))
{
System.out.println("Entered Receiver");
final String phonenumber = bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
// String info = "Detect Calls sample application\nIncoming number: " + phonenumber;
AudioManager amanager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
switch (amanager.getRingerMode()) {
case AudioManager.RINGER_MODE_SILENT:
System.out.println("Silent");
break;
case AudioManager.RINGER_MODE_VIBRATE:
System.out.println("Vibrate");
break;
case AudioManager.RINGER_MODE_NORMAL:
amanager.setStreamVolume(AudioManager.STREAM_RING,1, 0);
Handler handler=new Handler();
Runnable r=new Runnable()
{
public void run()
{
if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING))
{
Intent IncomingNumberIntent = new Intent(context1,SpeakOut.class);
IncomingNumberIntent.putExtra("PhoneNumber", phonenumber);
context1.startService(IncomingNumberIntent);
}
}
};
handler.postDelayed(r, 3000);
If the call is in ringing and profile is normal, it will go to a service and talks the name but
the trouble that app speaks the name even if the call is offhook.
Change your code to
int state = bundle.getInt(TelephonyManager.EXTRA_STATE);
switch (state)
{
case TelephonyManager.CALL_STATE_OFFHOOK:
System.out.println("fjkerj");
break;
case TelephonyManager.CALL_STATE_IDLE:
break;
case TelephonyManager.CALL_STATE_RINGING:
// Your code goes here
}
Remove from the run() method the if statement
if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING))

android audio - soundpool alternatives

I like the Android Soundpool class for its simplicity and it works well with the standard audio files I am using in my app. Now I want to make it possible for the user to specify certains sounds by specifying audio files on the sd card. Unfortunately I run into limitations of Soundpool, when the sound file is too big i get a
AudioFlinger could not create track. status: -12
response. It seems I have to switch to MediaPlayer yet before getting into the complexity of MediaPlayer again I wanted to ask if there is an audio library available for android which
has the simplicity of Soundpool for playing various sounds
doesnt have the limitations of Soundpool regarding the size of the files.
Thank you very much.
martin
For now I came up with a very simple AudioPool class which plays audio added to it subsequently with the MediaPlayer class. This implementation is for sure not mature yet I just thought to share it as it at least gives some idea how this can be approached easily. If you see any problems with this class please let us know.
Usage:
AudioPool ap = new AudioPool();
File root = Environment.getExternalStorageDirectory() ;
int id1 = ap.addAudio(root + "/gong1.mp3");
int id2 = ap.addAudio(root + "/gong2.mp3");
int id3 = ap.addAudio(root + "/gong3.mp3");
ap.playAudio(id1);
ap.playAudio(id3);
ap.playAudio(id3);
ap.playAudio(id2);
which will play gong1 -> gong3 -> gong3 -> gong1 subsequently. As this is basically what I need I leave it here ...
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.util.Log;
public class AudioPool {
static String TAG = "AudioPool";
MediaPlayer mPlayer;
int mAudioCounter;
int mCurrentId;
HashMap<Integer, String> mAudioMap;
LinkedList<Integer> mAudioQueue;
public AudioPool() {
mAudioMap = new HashMap<Integer, String>();
mAudioQueue = new LinkedList<Integer>();
mAudioCounter = 0;
}
public int addAudio(String path) {
Log.d(TAG, "adding audio " + path + " to the pool");
if (mAudioMap.containsValue(path)) {
return getAudioKey(path);
}
mAudioCounter++;
mAudioMap.put(mAudioCounter, path);
return mAudioCounter;
}
public boolean playAudio(int id) {
if (mAudioMap.containsKey(id) == false) {
return false;
}
if (mPlayer == null) {
setupPlayer();
}
if (mPlayer.isPlaying() == false) {
return prepareAndPlayAudioNow(id);
} else {
Log.d(TAG, "adding audio " + id + " to the audio queue");
mAudioQueue.add(id);
}
return true;
}
public Integer[] getAudioIds() {
return (Integer[]) mAudioMap.keySet().toArray(
new Integer[mAudioMap.keySet().size()]);
}
public void releaseAudioPlayer() {
if (mPlayer != null) {
mPlayer.release();
mPlayer = null;
}
}
private boolean prepareAndPlayAudioNow(int id) {
mCurrentId = id;
try {
Log.d(TAG, "playing audio " + id + " now");
mPlayer.reset();
mPlayer.setDataSource(mAudioMap.get(id));
mPlayer.prepare();
mPlayer.start();
return true;
} catch (Exception e) {
Log.d(TAG, "problems playing audio " + e.getMessage());
return false;
}
}
private boolean playAudioAgainNow() {
try {
mPlayer.seekTo(0);
mPlayer.start();
return true;
} catch (Exception e) {
Log.d(TAG, "problems playing audio");
return false;
}
}
private void setupPlayer() {
mPlayer = new MediaPlayer();
mPlayer.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
audioDone();
}
});
}
private void audioDone() {
if (mAudioQueue.size() > 0) {
Log.d(TAG, mAudioQueue.size() + " audios in queue");
int nextId = mAudioQueue.removeFirst();
if (mCurrentId == nextId) {
playAudioAgainNow();
} else {
prepareAndPlayAudioNow(nextId);
}
} else {
releaseAudioPlayer();
}
}
private int getAudioKey(String path) {
for (Map.Entry<Integer, String> map : mAudioMap.entrySet()) {
if (map.getValue().compareTo(path) == 0) {
return map.getKey();
}
}
return -1;
}
}
Thanks to dorjeduck for the solution, but his class based on MediaPlayer, which has huge latency.
What does it mean? It means that when you call these:
mPlayer.prepare();
mPlayer.start();
and actually hear the sound the delay is very noticable. For example when you need to play one track and immediately play another, you will hear delay even on high-end hardware.
The solution to load all bytes into memory before playing, and use AudioTrack to play that sound bytes.
I have written SoundPoolCompat which uses AudioTrack under the hood. You could pass custom bufferSize, and all data within that buffer will be loaded into memory and played with small latency like SoundPool does. All data that exceed that bufferSize will be loaded on demand (which adds latency, similar to MediaPlayer). Api is very similart to SoundPool, also it is added a feature to load sounds from Uri (for example gdrive). And there is playOnce method, all resources will be unloaded after file is played.
implementation 'com.olekdia:sound-pool:3.0.2'
https://gitlab.com/olekdia/common/libraries/sound-pool

Categories

Resources