Update: Soundboard Application Source Code - android

OK I got it to work. This will save the file as a ringtone, notification, or alarm based on the context menu. (Only ringtone function is shown due to space conservation)
Need help with:
For some reason no sound plays after awhile.(about 20 or so presses and won't play again until you back out of the app and launch it again) Also I've been told "/sdcard/media/etc" isn't "the correct way" to do it.
If anyone has any suggestions on how to
1.release/pause/stop the sound from playing when the home button is pressed, a text is recieved, or the back button is pressed to exit the app, etc
and
2.the correct way to get the sdcard. I'd appreciate it.
MediaPlayer mp1;
MediaPlayer mp2;
MediaPlayer mp3;
MediaPlayer mp4;
MediaPlayer mp5;
protected void onDestroy() {
super.onDestroy();
if(mp1 != null){
mp1.release();
}
if(mp2 != null){
mp2.release();
}
if(mp3 != null){
mp3.release();
}
if(mp4 != null){
mp4.release();
}
if(mp5 != null){
mp5.release();
}
}
protected void onPause() {
super.onPause();
if(mp1 != null){
mp1.stop();
}
if(mp2 != null){
mp2.stop();
}
if(mp3 != null){
mp3.stop();
}
if(mp4 != null){
mp4.stop();
}
if(mp5 != null){
mp5.stop();
}
}
}
protected void onResume() {
super.onResume();
}

Answer to Q1: Add a onPause method to your activity and call MediaPlayer's stop() method. You should also add a onDestroy method and call release to free the resources used by the mediaplayer.
Answer to Q2: Take a look at this post. For more details read the Android Developers info on Data Storage

Related

Sometimes microphone doesnt get released for other apps

Im working on a personal assistant using pocketsphinx speech recognizer in android. This is the way my app works every time a special word is heard the personal assistant will answer back and do a task. I have been having some problems with the releasing of the microphone. I dont know if it is a bug. It only happens sometimes when I close the application the microphone is still looking for that word and answering using text to speech. Even though screen is off. When I tried to record a video it says microphone is used by another application. So I have to open my app again and close the app to release the microphone. In my knowledge the only lifecycles to release resources are onStop, onPause, and onDestroy. It cannot be my phone that is malfunctioning I have tested the app with two different phones and still sometimes it happens in bot of them. Any help would be appreciated.
This is the way Im releasing the microphone, camera and the text to speech. Thanks in advance
private edu.cmu.pocketsphinx.SpeechRecognizer recognizer;
#Override
public void onPause() {
super.onPause();
if (tts != null) {
tts.shutdown();
}
if (camera != null) {
camera.release();
camera = null;
}
if (recognizer != null) {
recognizer.stop();
recognizer.cancel();
recognizer.shutdown();
recognizer = null;
}
}
#Override
protected void onStop() {
super.onStop();
if (tts != null) {
tts.shutdown();
}
if (camera != null) {
camera.release();
camera = null;
}
if (recognizer != null) {
recognizer.cancel();
recognizer.shutdown();
recognizer = null;
}
}
#Override
public void onDestroy() {
super.onDestroy();
if (tts != null) {
tts.shutdown();
}
if (camera != null) {
camera.release();
camera = null;
}
if (recognizer != null) {
recognizer.cancel();
recognizer.shutdown();
}
}
Try overriding the onbackpressed method and post ur recorder stop code there. And then finally add in the end youractivity.finish;
This shall destroy ur activity as soon as back is pressed and should stop the recorder

Releasing Media Player while preparing is causing app to freeze

I have two activities a main activity which has Recyclerview and a detailedActivity which is launched every time the user clicks on one of the items of the Recyclerview. The detailedActivity has a mediaplayer component that is being created everytime a detailedActivity is created. Now in the onDestroy method I always free the resources taken by the mediaPlayer by this code:
#Override
protected void onDestroy() {
if (mMediaPlayer != null) {
if (mMediaPlayer.isPlaying()) {
mMediaPlayer.stop();}
mMediaPlayer.release();
mMediaPlayer=null;
}
super.onDestroy();
}
The app freezes for a while every time I click the back button while the mediaplayer is still preparing. The message that I get in the logcat is this:
I/Choreographer: Skipped 112 frames! The application may be doing too much work on its main thread.
So this freezing only happens if I destroyed the activity while it is preparing but if it is already in the prepared state it won't happen. I use prepreAsync to fetch the media from the internet.
Thanks. Any help is highly appreciated. I have been stuck in this problem for days!
OK. I've kind of worked around the problem. I am writing this for anyone who might encounter the same situation as I did. I made two boolean flags in the scope of the class like this:
boolean prepared = false;
boolean cancel = false;
After that in the onpreapred method I set prepared to true.
mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
prepared = true;
}
});
In the onDestroy method I check whether the mediaplayer is already prepared or not if prepared I release it from the method its self.
#Override
protected void onDestroy() {
if (mMediaPlayer != null) {
if (mMediaPlayer.isPlaying()) {
mMediaPlayer.stop();
}
if (prepared) {
mMediaPlayer.release();
mMediaPlayer = null;
}
cancel = true;
}
super.onDestroy();
}
Otherwise, I set cancel to true and implement on OnBufferingUpdateListener interface and override its method and release the mediaplayer from there.
#Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
if (cancel) {
mp.reset();
mp.release();
mMediaPlayer = null;
Log.i("msg", " mp released");
}
}
Try to remove rechecking if mediaplayer is running or not.
#Override
protected void onDestroy() {
if (mMediaPlayer != null) {
mMediaPlayer.stop();
mMediaPlayer.release();
}
super.onDestroy();
}
If it still happens, try to remove checking mediaplayer in your onDestroy().
-- UPDATE --
This could be related with this bug:
https://code.google.com/p/android/issues/detail?id=959
This could be a help:
Android MediaPlayer reset freezes UI

Audio is playing twice in Android Media Player

In my application I'm having a method responsible for playing file placed under raw directory. But when Ever I call that function in my onResume() method, sound is played twice. Even I have googled and tried different solutions. even by checking mediaPlayer.isPlaying() and then stopping the MediaPlayer instance but still didn't get any help.
private void EnglishSound(){
if(mediaPlayer1!=null){
if(mediaPlayer1.isPlaying()){
mediaPlayer1.stop();
}
mediaPlayer1.reset();
mediaPlayer1.release();
}
mediaPlayer1 = MediaPlayer.create(this, R.raw.p012);
mediaPlayer1.start();
}
public void onResume() {
super.onResume();
EnglishSound();
}
and EnglishSound() is called no where else in the whole activity. Even have tried debugging but it never enters the if block containing isPlaying().
Try to release onPause()
public void onPause() {
super.onPause();
if(mediaPlayer1 != null)
mediaPlayer1.release();
}

How to release media player while preparing

I am working on music player app where I am doing streaming of music using MediaPlayer. Streaming is working fine but now I want If user press back button of the activity then it should stop preparing & release media player immediately.
Currently when it is preparing and if activity is being destroyed then I am releasing the MediaPlayer like this but when it release it then it hangs the application & show ANR.
#Override
protected void onDestroy() {
super.onDestroy();
if(mediaPlayer!=null) {
mediaPlayer.stop();
mediaPlayer.release();
}
}
I am initializing MediaPlayer like below
mediaPlayer = null;
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource("http://www.samisite.com/sound/cropShadesofGrayMonkees.mp3");
Now I want when it start preparing then on back press when I am releasing in onDestory then it should not hang the app & release the media player smoothly.
Please help me what is the best way to do this. Thanks in advance
mediaPlayer.prepareAsync();
Check the answer here. And the MediaPlayer State Diagram from android code.
Edit:
#TGMCians I showed him the provided link, So, if the playback is still not ready or preparing, he can not call stop() until it's called onPrepared. I'm not sure that onPrepared keep called after the app onDestroy called. So, The full snip I think is:
private boolean mPrepared = false;
private boolean mCancel = false;
public void onPrepared(MediaPlayer player){
mPrepared = true;
if(mCancel){
player.release();
mPrepared = false;
mCancel = false;
//nullify your MediaPlayer reference
mediaPlayer = null;
}
}
private void cancelMedia(){
mCancel = true;
}
#Override
protected void onDestroy() {
super.onDestroy();
cancelMedia();
if (mediaPlayer != null && mPrepared) {
mediaPlayer.stop();
mediaPlayer.release();
mPrepared = false;
mediaPlayer = null;
}
}

force close if mediaplayer is not used and back button pushed

In an activity I'm using two media players for different sounds, if both sounds are played and the back button is pushed there is no problem, it works fine. the media players stop and release.(mp is set to loop, mps just plays a short sound)
#Override
protected void onPause() {
// TODO Auto-generated method stub
mp.stop();
super.onPause();
}
#Override
protected void onStop() {
// TODO Auto-generated method stub
mp.release();
mps.release();
super.onStop();
}
But, if one and/or both sounds are not played I get a forced close when the back button is pushed and a null pointer exception. How would you write code to check if the mediaplayers were ever used and therefore need to stop and release them?
If you are getting a null pointer in your onPause and onStop you need to check if mp is null.
#Override
protected void onPause() {
if (mp != null)
mp.stop();
super.onPause();
}
#Override
protected void onStop() {
if (mp != null)
mp.release();
if (mps != null)
mps.release();
super.onStop();
}
You can keep a count if you would like to but that means keeping track of another variable when you already have access to the variables you need
could you use a count and add one to it if there has been a play then check if it equals 0 or not.

Categories

Resources