Payday Loan Payday Loan

The following function will show you how to set a sound as ringtone on android.
You need the following permissions set on Manifest file:

<uses-permission android:name="android.permission.WRITE_SETTINGS" ></uses-permission>

The xml below contains a button. On pressing this button will call the function to save
the sound on sdcard and set it as ringtone. Code for ringtone.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  android:orientation="vertical"    android:layout_width="fill_parent"   
 android:layout_height="fill_parent"    android:background="@drawable/bg">
<ImageButton android:id="@+id/ImageButton01" 
android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:background="@drawable/transparent" 
android:layout_marginLeft="180dip" 
android:layout_marginTop="200dip"></ImageButton>
<TableLayout android:id="@+id/TableLayout01" 
android:layout_height="wrap_content" android:layout_marginTop="20dip" 
android:layout_width="fill_parent"><TableRow android:id="@+id/TableRow01" 
android:layout_height="wrap_content" 
android:layout_width="fill_parent">
<Button android:id="@+id/Button01" 
android:layout_height="wrap_content"
 android:layout_gravity="center"
 android:layout_width="fill_parent" android:layout_marginLeft="65dip"
 android:text="Set as Ringtone!"></Button></TableRow>
</TableLayout></LinearLayout>

The following code is the function and the Ringtone class

package com.airhorn;
 
import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
 
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.ContentValues;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.media.MediaPlayer.OnCompletionListener;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;
 
public class Ringtone extends Activity {
MediaPlayer mp=new MediaPlayer();
 
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ringtone);
Button a=(Button)findViewById(R.id.Button01);
 
a.setOnClickListener(new OnClickListener(){
 
@Override
public void onClick(View v) {
if(saveas(R.raw.sound)){
Toast.makeText(Ringtone.this, "The sound was set as ringtone!", Toast.LENGTH_LONG).show();
};
 
}});
ImageButton w=(ImageButton)findViewById(R.id.ImageButton01);
w.setOnClickListener(new OnClickListener(){
 
@Override
public void onClick(View arg0) {
 
playSound(R.raw.sound);
}});
}
 
public void playSound(int fileId) {
MediaPlayer mp;
 
try {
mp = MediaPlayer.create(this, fileId);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
 
@Override
public void onCompletion(MediaPlayer mp) {
mp.release();
mp = null;
 
}
});
} catch (Exception exception) {
}
 
}
public boolean saveas(int ressound){
byte[] buffer=null;
InputStream fIn = getBaseContext().getResources().openRawResource(ressound);
int size=0;
 
try {
size = fIn.available();
buffer = new byte[size];
fIn.read(buffer);
fIn.close();
} catch (IOException e) {
// TODO Auto-generated catch block
return false;
}
 
String path="/sdcard/sounds/";
String filename="mysound"+".mp3";
 
boolean exists = (new File(path)).exists();
if (!exists){new File(path).mkdirs();}
 
FileOutputStream save;
try {
save = new FileOutputStream(path+filename);
save.write(buffer);
save.flush();
save.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
return false;
} catch (IOException e) {
// TODO Auto-generated catch block
return false;
}
 
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://"+path+filename)));
 
File k = new File(path, filename);
 
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, "exampletitle");
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
values.put(MediaStore.Audio.Media.ARTIST, "cssounds ");
values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
values.put(MediaStore.Audio.Media.IS_ALARM, false);
values.put(MediaStore.Audio.Media.IS_MUSIC, false);
 
//Insert it into the database
Uri newUri= this.getContentResolver().insert(MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath()), values);
 
RingtoneManager.setActualDefaultRingtoneUri(
this,
RingtoneManager.TYPE_RINGTONE,
newUri
);
return true;
}
 
 
 
}

I hope this can help you, so that you can set as ringtone any sound that you want in Android. If you see in this code, there are two buttons, one for playing the sound, and one for setting the sound as ringtone, so use both of functions for whatever you need.

||||| 1 I Like It! |||||

So, let’s continue with the logic of stopping a sound when is called a second sound from the soundboard. I will start directly by showing what you will need to write in the OnClickListener of every button, so you will not get a Force Close error.

ImageButton a=(ImageButton)findViewById(R.id.ImageButton01);
ImageButton b=(ImageButton)findViewById(R.id.ImageButton02);
a.setOnClickListener(new OnClickListener(){
	@Override
	public void onClick(View v) {
if(mp==null){playSound(R.raw.sound1);}//if is not playin any sound, then we start this sound
else{mp.release(); 
playSound(R.raw.sound1);} //else if there is a sound playing, we release it and then play the current sound
 
		}});
b.setOnClickListener(new OnClickListener(){
	@Override
	public void onClick(View v) {
		if(mp==null){playSound(R.raw.sound2);}
 
		else{
			mp.release();
		playSound(R.raw.sound2);}
		}});

Also, you can get an error when you press the hard key “back” on your phone, if the sound still playing. For this we override the finish() method like this:

@Override
		public void finish() {
		if(mp==null){}
		else{mp.release();}
			super.finish();
		}
||||| 0 I Like It! |||||

 
I will show you a function that allows you to play a sound in Android.
I implemented this function to be easier and just to call it at OnClickListener.
So the function is:

 public void playSound(String fileId) {
 
try {
 
mp.reset();
mp.setDataSource(fileId);
 
mp.prepare();
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
 
@Override
public void onCompletion(MediaPlayer mp) {
mp.release();
mp = null;
 }
});
} catch (Exception exception) {
}}

Now let’s say you want to create a soundboard:
we need to attach this function to a click listener of a Button.
Firstly you need to create a subfolder in the res folder named raw…Here you will put your sound…let’s say
you call it sound.mp3. So the code is:

Button x=(Button)findViewById(R.id.Button01);
x.setOnClickListener(new OnClickListener(){
 
 @Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
playSound(R.raw.sound);
}});

Now i will show you how it looks whole program:

import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.widget.Button;
import android.view.MenuItem;
import android.view.View;
 
import java.io.IOException;
 
public class SoundBoard extends Activity
{
@Override
public void onCreate(Bundle icicle)
{ super.onCreate(icicle);
setContentView(R.layout.soundboard);
Button x=(Button)findViewById(R.id.Button01);
x.setOnClickListener(new OnClickListener(){
 
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
playSound(R.raw.sound);
}});
 
}
public void playSound(String fileId) {
 
try {
mp.reset();
mp.setDataSource(fileId);
 
mp.prepare();
mp.start();
 
mp.setOnCompletionListener(new OnCompletionListener() {
 
@Override
public void onCompletion(MediaPlayer mp) {
mp.release();
mp = null;
 
}
});
} catch (Exception exception) {
}}
}

I will come back with completion of this tutorial…I will show you how to play and stop, and how to play a sound on pressing one button and how to stop current sound and play second sound on pressing on other button without force closing

 

||||| 2 I Like It! |||||

Search

Popular

Sponsors