Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.ring;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import android.app.Activity;
- import android.content.ContentValues;
- import android.content.Intent;
- import android.content.res.AssetFileDescriptor;
- import android.content.res.Resources;
- import android.media.MediaPlayer;
- import android.media.RingtoneManager;
- import android.net.Uri;
- import android.os.Bundle;
- import android.os.Environment;
- import android.provider.MediaStore;
- import android.view.ContextMenu;
- import android.view.ContextMenu.ContextMenuInfo;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.ImageButton;
- import android.widget.Toast;
- public class Ring extends Activity {
- public int count = 0;
- int selectedSoundId;
- final MediaPlayer player = new MediaPlayer();
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- final Resources res = getResources();
- final int[] buttonIds = { R.id.ibTh, R.id.ibTLA, R.id.ibNah, R.id.ibOh, R.id.ibTB, R.id.ibYa, R.id.ibTYE};
- final int[] soundIds = { R.raw.th, R.raw.tla, R.raw.tnah, R.raw.toh, R.raw.tra, R.raw.tya, R.raw.tye};
- count = 0;
- View.OnClickListener listener = new View.OnClickListener() {
- public void onClick(View v) {
- for(int i = 0; i < buttonIds.length; i++) {
- if(v.getId() == buttonIds[i]) {
- selectedSoundId = soundIds[i];
- AssetFileDescriptor afd = res.openRawResourceFd(soundIds[i]);
- player.reset();
- try {
- player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
- } catch (IllegalArgumentException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IllegalStateException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- try {
- player.prepare();
- } catch (IllegalStateException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- if(count == 0){
- player.start();
- count = 1;
- } else {
- player.pause();
- count = 0;
- }
- break;
- }
- }
- }
- };
- for(int i = 0; i < buttonIds.length; i++) {
- ImageButton soundButton = (ImageButton)findViewById(buttonIds[i]);
- registerForContextMenu(soundButton);
- soundButton.setOnClickListener(listener);
- }
- }
- @Override
- public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
- super.onCreateContextMenu(menu, v, menuInfo);
- menu.setHeaderTitle("Save as...");
- menu.add(0, v.getId(), 0, "Ringtone");
- menu.add(0, v.getId(), 0, "Notification");
- }
- @Override
- public boolean onContextItemSelected(MenuItem item) {
- if(item.getTitle()=="Ringtone"){function1(item.getItemId());}
- else if(item.getTitle()=="Notification"){function2(item.getItemId());}
- else {return false;}
- return true;
- }
- public void function1(int id){
- if
- (savering(selectedSoundId)){
- // Code if successful
- Toast.makeText(this, "Saved as Ringtone", Toast.LENGTH_SHORT).show();
- }
- else
- {
- // Code if unsuccessful
- Toast.makeText(this, "Failed - Check your SDCard", Toast.LENGTH_SHORT).show();
- }
- }
- public void function2(int id){
- if
- (savenot(selectedSoundId)){
- // Code if successful
- Toast.makeText(this, "Saved as Notification", Toast.LENGTH_SHORT).show();
- }
- else
- {
- // Code if unsuccessful
- Toast.makeText(this, "Failed - Check your SDCard", Toast.LENGTH_SHORT).show();
- }
- }
- //Save into RingTone Folder
- public boolean savering(int ressound){
- byte[] buffer=null;
- InputStream fIn = getBaseContext().getResources().openRawResource(ressound);
- int size=50;
- 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=Environment.getExternalStorageDirectory().getPath()+"/sdcard/media/ringtone/";
- String filename="name"+".ogg";
- 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, "name");
- values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/ogg");
- values.put(MediaStore.Audio.Media.ARTIST, "weee");
- values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
- values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
- values.put(MediaStore.Audio.Media.IS_ALARM, true);
- values.put(MediaStore.Audio.Media.IS_MUSIC, false);
- Uri uri = MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath());
- getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + k.getAbsolutePath() + "\"", null);
- Uri newUri = getContentResolver().insert(uri, values);
- RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_RINGTONE, newUri);
- return true;
- }
- //Save in Notification Folder
- public boolean savenot(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=Environment.getExternalStorageDirectory().getPath()+"/media/notification/";
- String filename="name"+".ogg";
- 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, "name");
- values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/ogg");
- values.put(MediaStore.Audio.Media.ARTIST, "weee");
- values.put(MediaStore.Audio.Media.IS_RINGTONE, false);
- values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
- values.put(MediaStore.Audio.Media.IS_ALARM, true);
- values.put(MediaStore.Audio.Media.IS_MUSIC, false);
- Uri uri = MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath());
- getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + k.getAbsolutePath() + "\"", null);
- Uri newUri = getContentResolver().insert(uri, values);
- RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_NOTIFICATION, newUri);
- return true;
- }
- @Override
- protected void onDestroy() {
- // TODO Auto-generated method stub
- super.onDestroy();
- if(player != null) {
- player.pause();
- player.stop();
- player.release();
- count = 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement