Advertisement
Guest User

Error

a guest
Mar 5th, 2012
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.73 KB | None | 0 0
  1. package com.ring;
  2.  
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8.  
  9. import android.app.Activity;
  10. import android.content.ContentValues;
  11. import android.content.Intent;
  12. import android.content.res.AssetFileDescriptor;
  13. import android.content.res.Resources;
  14. import android.media.MediaPlayer;
  15. import android.media.RingtoneManager;
  16. import android.net.Uri;
  17. import android.os.Bundle;
  18. import android.os.Environment;
  19. import android.provider.MediaStore;
  20. import android.view.ContextMenu;
  21. import android.view.ContextMenu.ContextMenuInfo;
  22. import android.view.MenuItem;
  23. import android.view.View;
  24. import android.widget.ImageButton;
  25. import android.widget.Toast;
  26.  
  27.  
  28. public class Ring extends Activity {
  29.    
  30.     public int count = 0;
  31.     int selectedSoundId;
  32.     final MediaPlayer player = new MediaPlayer();
  33.     @Override
  34.     public void onCreate(Bundle savedInstanceState) {
  35.         super.onCreate(savedInstanceState);
  36.         setContentView(R.layout.main);
  37.    
  38.        
  39.         final Resources res = getResources();
  40.  
  41.     final int[] buttonIds = { R.id.ibTh, R.id.ibTLA, R.id.ibNah, R.id.ibOh, R.id.ibTB, R.id.ibYa, R.id.ibTYE};
  42.     final int[] soundIds = { R.raw.th, R.raw.tla, R.raw.tnah, R.raw.toh, R.raw.tra, R.raw.tya, R.raw.tye};
  43.     count = 0;
  44.  
  45.     View.OnClickListener listener = new View.OnClickListener() {
  46.         public void onClick(View v) {
  47.             for(int i = 0; i < buttonIds.length; i++) {
  48.                 if(v.getId() == buttonIds[i]) {
  49.                     selectedSoundId = soundIds[i];
  50.                     AssetFileDescriptor afd = res.openRawResourceFd(soundIds[i]);
  51.                     player.reset();
  52.                     try {
  53.                         player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
  54.                     } catch (IllegalArgumentException e) {
  55.                         // TODO Auto-generated catch block
  56.                         e.printStackTrace();
  57.                     } catch (IllegalStateException e) {
  58.                         // TODO Auto-generated catch block
  59.                         e.printStackTrace();
  60.                     } catch (IOException e) {
  61.                         // TODO Auto-generated catch block
  62.                         e.printStackTrace();
  63.                     }
  64.                     try {
  65.                         player.prepare();
  66.                     } catch (IllegalStateException e) {
  67.                         // TODO Auto-generated catch block
  68.                         e.printStackTrace();
  69.                     } catch (IOException e) {
  70.                         // TODO Auto-generated catch block
  71.                         e.printStackTrace();
  72.                     }
  73.                     if(count == 0){
  74.                         player.start();
  75.                       count = 1;
  76.                     } else {
  77.                         player.pause();          
  78.                         count = 0;
  79.                     }
  80.                     break;
  81.                 }
  82.             }
  83.         }
  84.     };
  85.  
  86.  
  87.     for(int i = 0; i < buttonIds.length; i++) {
  88.         ImageButton soundButton = (ImageButton)findViewById(buttonIds[i]);
  89.         registerForContextMenu(soundButton);
  90.         soundButton.setOnClickListener(listener);
  91.  
  92.     }
  93.  
  94.  
  95. }
  96.  
  97. @Override
  98. public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
  99.  super.onCreateContextMenu(menu, v, menuInfo);
  100.  menu.setHeaderTitle("Save as...");
  101.  menu.add(0, v.getId(), 0, "Ringtone");
  102.  menu.add(0, v.getId(), 0, "Notification");
  103. }
  104. @Override  
  105. public boolean onContextItemSelected(MenuItem item) {
  106.  if(item.getTitle()=="Ringtone"){function1(item.getItemId());}  
  107.   else if(item.getTitle()=="Notification"){function2(item.getItemId());}  
  108.   else {return false;}
  109.  return true;
  110. }
  111.  
  112. public void function1(int id){
  113.  
  114.     if
  115.      (savering(selectedSoundId)){  
  116.       // Code if successful  
  117.       Toast.makeText(this, "Saved as Ringtone", Toast.LENGTH_SHORT).show();
  118.      }          
  119.      else          
  120.      {
  121.       // Code if unsuccessful  
  122.       Toast.makeText(this, "Failed - Check your SDCard", Toast.LENGTH_SHORT).show();
  123.      }
  124.  
  125.     }
  126.     public void function2(int id){  
  127.      if
  128.      (savenot(selectedSoundId)){  
  129.       // Code if successful  
  130.       Toast.makeText(this, "Saved as Notification", Toast.LENGTH_SHORT).show();
  131.      }          
  132.      else          
  133.      {
  134.       // Code if unsuccessful  
  135.       Toast.makeText(this, "Failed - Check your SDCard", Toast.LENGTH_SHORT).show();
  136.      }
  137.     }
  138.  
  139.  
  140.  
  141. //Save into RingTone Folder
  142.  
  143. public boolean savering(int ressound){
  144.  byte[] buffer=null;
  145.  InputStream fIn = getBaseContext().getResources().openRawResource(ressound);
  146.  int size=50;
  147.  
  148.  try {
  149.    size = fIn.available();  
  150.    buffer = new byte[size];  
  151.    fIn.read(buffer);  
  152.    fIn.close();
  153.  } catch (IOException e) {
  154.   // TODO Auto-generated catch block  
  155.   return false;      }
  156.  
  157.  String path=Environment.getExternalStorageDirectory().getPath()+"/sdcard/media/ringtone/";
  158.  
  159.  
  160.  String filename="name"+".ogg";
  161.  
  162.  
  163.  boolean exists = (new File(path)).exists();  
  164.  if (!exists){new File(path).mkdirs();}  
  165.  
  166.  FileOutputStream save;
  167.  try {
  168.   save = new FileOutputStream(path+filename);  
  169.   save.write(buffer);  
  170.   save.flush();  
  171.   save.close();  
  172.  } catch (FileNotFoundException e) {
  173.   // TODO Auto-generated catch block  
  174.   return false;  
  175.  } catch (IOException e) {
  176.   // TODO Auto-generated catch block  
  177.   return false;
  178.  }
  179.  sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://"+path+filename)));
  180.  
  181.  File k = new File(path, filename);  
  182.  ContentValues values = new ContentValues();  
  183.  values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());  
  184.  values.put(MediaStore.MediaColumns.TITLE, "name");  
  185.  values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/ogg");  
  186.  values.put(MediaStore.Audio.Media.ARTIST, "weee");  
  187.  values.put(MediaStore.Audio.Media.IS_RINGTONE, true);  
  188.  values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);  
  189.  values.put(MediaStore.Audio.Media.IS_ALARM, true);  
  190.  values.put(MediaStore.Audio.Media.IS_MUSIC, false);    
  191.  
  192.  Uri uri = MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath());
  193.  getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + k.getAbsolutePath() + "\"", null);
  194.  Uri newUri = getContentResolver().insert(uri, values);
  195.  RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_RINGTONE, newUri);
  196.  
  197.  
  198.  
  199.  
  200.  return true;
  201. }
  202.  
  203. //Save in Notification Folder
  204.  
  205. public boolean savenot(int ressound){
  206.  byte[] buffer=null;
  207.  InputStream fIn = getBaseContext().getResources().openRawResource(ressound);
  208.  int size=0;
  209.  
  210.  try {
  211.    size = fIn.available();  
  212.    buffer = new byte[size];  
  213.    fIn.read(buffer);  
  214.    fIn.close();
  215.  } catch (IOException e) {
  216.   // TODO Auto-generated catch block  
  217.   return false;      }
  218.  
  219.  String path=Environment.getExternalStorageDirectory().getPath()+"/media/notification/";
  220.  
  221.  
  222.  String filename="name"+".ogg";
  223.  
  224.  boolean exists = (new File(path)).exists();  
  225.  if (!exists){new File(path).mkdirs();}  
  226.  
  227.  FileOutputStream save;
  228.  try {
  229.   save = new FileOutputStream(path+filename);  
  230.   save.write(buffer);  
  231.   save.flush();  
  232.   save.close();  
  233.  } catch (FileNotFoundException e) {
  234.   // TODO Auto-generated catch block  
  235.   return false;  
  236.  } catch (IOException e) {
  237.   // TODO Auto-generated catch block  
  238.   return false;
  239.  }
  240.  sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://"+path+filename)));
  241.  
  242.  File k = new File(path, filename);  
  243.  ContentValues values = new ContentValues();  
  244.  values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());  
  245.  values.put(MediaStore.MediaColumns.TITLE, "name");  
  246.  values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/ogg");  
  247.  values.put(MediaStore.Audio.Media.ARTIST, "weee");  
  248.  values.put(MediaStore.Audio.Media.IS_RINGTONE, false);  
  249.  values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);  
  250.  values.put(MediaStore.Audio.Media.IS_ALARM, true);  
  251.  values.put(MediaStore.Audio.Media.IS_MUSIC, false);    
  252.  
  253.  Uri uri = MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath());
  254.  getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + k.getAbsolutePath() + "\"", null);
  255.  Uri newUri = getContentResolver().insert(uri, values);
  256.  RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_NOTIFICATION, newUri);
  257.  
  258.  
  259.  return true;
  260.  
  261.  
  262.  
  263. }
  264. @Override
  265. protected void onDestroy() {
  266.     // TODO Auto-generated method stub
  267.     super.onDestroy();
  268.     if(player != null) {
  269.         player.pause();
  270.         player.stop();
  271.         player.release();
  272.         count = 0;
  273.    }
  274. }
  275. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement