I want to play sound while save a note - android-studio

I want to play a sound while user save note in database here i'm using Media player to play sound and i have save the sounds file in raw folder.. Here is my Code
mediaPlayer = MediaPlayer.create(this,R.raw.save_effects);
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId())
{
case R.id.note_save:
mediaPlayer.start();
if (isEdit) {
updateNote(id,editText1.getText().toString(),editText2.getText().toString(),getDateTime(),Colors);
}else {
saveNote(editText1.getText().toString(),editText2.getText().toString(),getDateTime(),Colors);
break;
}
case R.id.color:
mediaPlayer.start();
openColorPicker();
break;
case android.R.id.home:
finish();
break;
}
return true;
}
Here i'm getting the problem while clicking on save button not playing the sound but selecting color it play sounds please some one help me out of this problem..

Related

How to bundle sounds with Codename One?

I want to include sounds in my Codename One app for effects like clicking a button, transitions, etc. I prefer not to download them from URL, since they are very small and I want them to be played even when the device is not connected to the internet. It looks like I cannot include the source files in the theme. What should I do?
Put the sound file in the "src" folder inside your project folder and reference it like below:
private Media MEDIA = null;
public void playAudio(String fileName) {
try {
if (MEDIA == null) {
final InputStream is = Display.getInstance().getResourceAsStream(getClass(), "/" + fileName);
MEDIA = MediaManager.createMedia(is, "audio/mp3", new Runnable() {
#Override
public void run() {
MEDIA = null;
}
});
}
if (MEDIA != null && MEDIA.isPlaying() == false) {
MEDIA.setVolume(100);
MEDIA.play();
}
} catch (IOException ioe) {
}
}
...
playAudio("my_sound.mp3");

Using a button as a boolean switch

I'm trying to make a button in android function as a boolean switch. I want it to do something on the first click and do something else on the second click. On the third click, I want it to do the same thing on the first click et cetera. The main reason I want this due to the text element on the button that changes with each click. Switches and checkboxes have no texts to change.
I've tried finding documentations on doing this online but can't seem to find any previous examples of doing this. Would appreciate if anyone has any ideas or just tell me outright that this is not workable.
yourButton
.setOnClickListener(new View.OnClickListener() {
private boolean state = false;
public void onClick(View v) {
if ( state ) {
state = false;
yourButton.setText("False");
} else {
state = true;
yourButton.setText("True");
}
}
});
You can try something like this, by the way i DIDN'T test this code i'm just trying to show you a way you could try to do it
1. set button to change drawable background and remove
button.setOnClickListener ( new View.OnClickListener () {
private boolean state=false;
#Override
public void onClick(View v) {
if (state){
state=false;
textView.setBackground ( getDrawable(R.drawable.led_mode ) );
}
else{
state=true;
textview.setbackground(null);
}
}
});

Android - Back button behavior

I have a project with 2 activities, the first one is the "SplashActivity" - where I load some network data - the second one, the MainActivity.
Inside of my MainActivity I have a fragment and inside of this fragment a webview. My first point is, when the user clicks on back button, the SplashScreen is open again.
The back button should behave like:
When the user doesn't navigate inside of my webview, close the app.
When the user navigates in webview, use the back history of the browswer.
I read about back stack here: http://developer.android.com/training/implementing-navigation/temporal.html#back-webviews
I didn't understand at all how it should work, because I have all cases "mixed". Anyone knows what should I do to fix this problem?
Any idea or sample code will be appreciate!
Define Webview wb as a global variable. Then try this;
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(wb.canGoBack() == true){
wb.goBack();
}else{
new AlertDialog.Builder(this).setIcon(android.R.drawable.ic_dialog_alert).setTitle("Application will be closed")
.setMessage("Close app?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
finish();
System.exit(0);
}
}).setNegativeButton("No", null).show();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
}

disabling sound in a single application in blackberry

I just did a dictionary application in blackberry along with a speech to text conversion support .Everything is working fine. Now i wanted to disable the sound when the user needs So how can i do it programmatically .Please help me
Try this
use the flag value as reference
if flag value is true then user click on item then it will play the sound
else sound wont play and display one dialog that Do you want enable sound with two options yes or no
if user click on yes then make flag value as true and item.setText("Voice Disable"); otherwise no action means no changes in flag
in your list item click listener write condition as following
if(flag==true)
{
write your logic to play
}
sample code is
public class app extends UiApplication{
public static void main(String[] args) {
new app().enterEventDispatcher();
}
public app() {
pushScreen(new SampleScreen());
}
}
class SampleScreen extends MainScreen
{
static boolean flag=true;
MenuItem item=null;
public SampleScreen() {
// use the flag value as reference
// if flag value is true then user click on item then it will play the sound
// else sound wont play and display one dialog that Do you want enable sound with two options yes or no
// if user click on yes then make flag value as true and item.setText("Voice Disable"); otherwise no action means no changes in flag
// in your list item click listner write condition as following
// if(flag==true)
// {
// write your logic to play
// }
// you already implement
item=new MenuItem("Voice Disable",0,100) {
public void run() {
if(flag)
{
flag=false;
item.setText("Voice Enable");
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
Dialog.inform("Voice Disable succesfully");
}
});
}else{
flag=true;
item.setText("Voice Disable");
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
Dialog.inform("Voice Enable succesfully");
}
});
}
}
};
addMenuItem(item);
}
}

creating a play / pause toggle switch

I am using silverlight 4 with smf and I want to create a play , pause toggle button. So if the media is playing and if the button is clicked it will pause the video. But if the media is paused, it will restart playing video.
How can I do that
private void btnPlay_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
}
I don't know anything about silverlight, but you could use a boolean.
private void btnPlay_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
if(isPlaying)
{
music.pause();
isPlaying = FALSE;
return;
}
music.play();
isPlaying = TRUE;
return;
}

Resources