Set a resource .mp3 for notification or ringtone

All your problems with Audio, Video and Images.

Set a resource .mp3 for notification or ringtone

Postby stece » Wed Jun 02, 2010 3:30 pm

I'm trying to develop some sort of soundboard.
I want to implement the feature that when you long press a button you can save that particular sound for a rintone or notification.

The long press is no problem, but I can't find a code snippet to help me implement for the ringtone/notification part.

I got my sounds as a resource in the /raw map from my application.

Does anyone know a good tutorial or can anyone give me a code snippet?

Would be really helpfull :(

I tried googleing and searching on the forum but didn't find anything relevant. :s

Grtz

Stece
User avatar
stece
Experienced Developer
Experienced Developer
 
Posts: 62
Joined: Wed Apr 21, 2010 10:50 am
Location: Belgium

Top

Re: Set a resource .mp3 for notification or ringtone

Postby sevenrechlin » Thu Jun 03, 2010 12:48 am

Hello,

I found the information from the site listed below. I have included part of my source from my ATHF Soundboard.

int toCopy is the R.raw, toName is the filename (without .mp3). For the path, I setup a simple dialog that asks whether the user wants to add as a ringtone or notification. If a ringtone, it sends the save path "/sdcard/media/audio/ringtones/" and for alert tones "/sdcard/media/audio/notifications/".

The code sets the mp3 as ok for ringtone or notification, but not for music. You can also change the mime type and extension if you want .wav or something else. You also might want to change the artist from "ATHF" :).

Code: Select all
   public boolean saveas(int toCopy, String toName, String path){
       byte[] buffer=null;
       InputStream fIn = getBaseContext().getResources().openRawResource(toCopy);
       int size=0;

       try {
        size = fIn.available();
        buffer = new byte[size];
        fIn.read(buffer);
        fIn.close();
       } catch (IOException e) {
        // Auto-generated catch block
        return false;
       }

      
       String filename=toName+".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) {
        // Auto-generated catch block
        return false;
       } catch (IOException e) {
        // 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, toName);
       values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mpeg");
       values.put(MediaStore.Audio.Media.ARTIST, "ATHF ");
       values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
       values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
       values.put(MediaStore.Audio.Media.IS_ALARM, true);
       values.put(MediaStore.Audio.Media.IS_MUSIC, false);

       //Insert it into the database
       this.getContentResolver().insert(MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath()), values);

       return true;
      }


http://www.stealthcopter.com/blog/2010/01/android-saving-a-sound-file-to-sd-from-resource-and-setting-as-ringtone/
sevenrechlin
Freshman
Freshman
 
Posts: 3
Joined: Sat May 22, 2010 1:14 am

Re: Set a resource .mp3 for notification or ringtone

Postby stece » Thu Jun 03, 2010 6:52 am

Thans for the help :D

Will try that out this evening after work :P
User avatar
stece
Experienced Developer
Experienced Developer
 
Posts: 62
Joined: Wed Apr 21, 2010 10:50 am
Location: Belgium

Re: Set a resource .mp3 for notification or ringtone

Postby brybam » Sat Jun 26, 2010 9:05 pm

would anyone possibly be able to see what i'm doing wrong here?

Just trying to mak a simple press to play. long press to save as...

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.mypackage.here;
  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.os.Bundle;
  13. import android.provider.MediaStore;
  14. import android.view.View;
  15. import android.view.View.OnClickListener;
  16. import android.widget.Button;
  17. import android.view.ContextMenu;  
  18. import android.view.MenuItem;  
  19. import android.view.ContextMenu.ContextMenuInfo;
  20. import android.widget.Toast;
  21.  
  22.  
  23. public class One extends Activity {
  24.         private SoundManager mSoundManager;
  25.  
  26.        
  27.    @Override
  28.     public void onCreate(Bundle savedInstanceState) {
  29.         super.onCreate(savedInstanceState);
  30.         setContentView(R.layout.one);
  31.      
  32.         mSoundManager = new SoundManager();
  33.         mSoundManager.initSounds(getBaseContext());
  34.         mSoundManager.addSound(1, R.raw.awesomesound);
  35.  
  36.  
  37.  
  38.        
  39.         Button SoundButton1 = (Button)findViewById(R.id.Sound1);
  40.         registerForContextMenu(SoundButton1);
  41.         SoundButton1.setOnClickListener(new OnClickListener() {
  42.        
  43.         public void onClick(View v) {
  44.                 mSoundManager.playSound(1);
  45.                                
  46.         }
  47.        
  48.         public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {  
  49.     super.onCreateContextMenu(menu, v, menuInfo);  
  50.         menu.setHeaderTitle("Ringtone Actions");  
  51.         menu.add(0, v.getId(), 0, "Save as Ringtone");  
  52.         menu.add(0, v.getId(), 0, "Save as Notification");  
  53.         }
  54.         public boolean onContextItemSelected(MenuItem item) {  
  55.         if(item.getTitle()=="Save as Ringtone"){function1(item.getItemId());}  
  56.         else if(item.getTitle()=="Save as Notification"){function2(item.getItemId());}  
  57.         else {return false;}  
  58.     return true;  
  59.     }  
  60.  
  61.     public void function1(int id){  
  62.         public boolean saveas(int ressound){  
  63.                  byte[] buffer=null;  
  64.                  InputStream fIn = getBaseContext().getResources().openRawResource(ressound);  
  65.                  int size=0;  
  66.                  
  67.                  try {  
  68.                   size = fIn.available();  
  69.                   buffer = new byte[size];  
  70.                   fIn.read(buffer);  
  71.                   fIn.close();  
  72.                  } catch (IOException e) {  
  73.                   // TODO Auto-generated catch block  
  74.                   return false;  
  75.                  }  
  76.                  
  77.                  String path="/sdcard/media/audio/ringtones/";  
  78.                  String filename="mysoundboard"+".ogg";  
  79.                  
  80.                  boolean exists = (new File(path)).exists();  
  81.                  if (!exists){new File(path).mkdirs();}  
  82.                  
  83.                  FileOutputStream save;  
  84.                  try {  
  85.                   save = new FileOutputStream(path+filename);  
  86.                   save.write(buffer);  
  87.                   save.flush();  
  88.                   save.close();  
  89.                  } catch (FileNotFoundException e) {  
  90.                   // TODO Auto-generated catch block  
  91.                   return false;  
  92.                  } catch (IOException e) {  
  93.                   // TODO Auto-generated catch block  
  94.                   return false;  
  95.                  }      
  96.                  
  97.                  sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://"+path+filename)));  
  98.                  
  99.                  File k = new File(path, filename);  
  100.                  
  101.                  ContentValues values = new ContentValues();  
  102.                  values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());  
  103.                  values.put(MediaStore.MediaColumns.TITLE, "exampletitle");  
  104.                  values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/.ogg");  
  105.                  values.put(MediaStore.Audio.Media.ARTIST, "cssounds ");  
  106.                  values.put(MediaStore.Audio.Media.IS_RINGTONE, true);  
  107.                  values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);  
  108.                  values.put(MediaStore.Audio.Media.IS_ALARM, true);  
  109.                  values.put(MediaStore.Audio.Media.IS_MUSIC, false);  
  110.                  
  111.                  //Insert it into the database  
  112.                  this.getContentResolver().insert(MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath()), values);  
  113.                  
  114.                  return true;  
  115.                 }  
  116.     }  
  117.    
  118.         public void function2(int id){  
  119.                 public boolean saveas(int ressound){  
  120.                          byte[] buffer=null;  
  121.                          InputStream fIn = getBaseContext().getResources().openRawResource(ressound);  
  122.                          int size=0;  
  123.                          
  124.                          try {  
  125.                           size = fIn.available();  
  126.                           buffer = new byte[size];  
  127.                           fIn.read(buffer);  
  128.                           fIn.close();  
  129.                          } catch (IOException e) {  
  130.                           // TODO Auto-generated catch block  
  131.                           return false;  
  132.                          }  
  133.                          
  134.                          String path="/sdcard/media/audio/notifications/";  
  135.                          String filename="mysoundboard"+".ogg";  
  136.                          
  137.                          boolean exists = (new File(path)).exists();  
  138.                          if (!exists){new File(path).mkdirs();}  
  139.                          
  140.                          FileOutputStream save;  
  141.                          try {  
  142.                           save = new FileOutputStream(path+filename);  
  143.                           save.write(buffer);  
  144.                           save.flush();  
  145.                           save.close();  
  146.                          } catch (FileNotFoundException e) {  
  147.                           // TODO Auto-generated catch block  
  148.                           return false;  
  149.                          } catch (IOException e) {  
  150.                           // TODO Auto-generated catch block  
  151.                           return false;  
  152.                          }      
  153.                          
  154.                          sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://"+path+filename)));  
  155.                          
  156.                          File k = new File(path, filename);  
  157.                          
  158.                          ContentValues values = new ContentValues();  
  159.                          values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());  
  160.                          values.put(MediaStore.MediaColumns.TITLE, "exampletitle");  
  161.                          values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/.ogg");  
  162.                          values.put(MediaStore.Audio.Media.ARTIST, "cssounds ");  
  163.                          values.put(MediaStore.Audio.Media.IS_RINGTONE, true);  
  164.                          values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);  
  165.                          values.put(MediaStore.Audio.Media.IS_ALARM, true);  
  166.                          values.put(MediaStore.Audio.Media.IS_MUSIC, false);  
  167.                          
  168.                          //Insert it into the database  
  169.                          this.getContentResolver().insert(MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath()), values);  
  170.                          
  171.                          return true;  
  172.                         }  
  173.     }  
  174.  
  175. });
  176.    }
  177. }
Parsed in 0.056 seconds, using GeSHi 1.0.8.4
brybam
Developer
Developer
 
Posts: 27
Joined: Fri May 21, 2010 1:03 am

Re: Set a resource .mp3 for notification or ringtone

Postby stece » Sun Jun 27, 2010 12:38 am

could you tell us more about the problem so we know what we are looking for?

What kind of errors?
If you debug, on what line does your code crash?
User avatar
stece
Experienced Developer
Experienced Developer
 
Posts: 62
Joined: Wed Apr 21, 2010 10:50 am
Location: Belgium

Re: Set a resource .mp3 for notification or ringtone

Postby brybam » Sun Jun 27, 2010 4:44 am

Sorry,

Line 48 - The method onCreateContextMenu(ContextMenu, View, ContextMenu.ContextMenuInfo) is undefined for the type Object

Line 62 - Illegal modifier for parameter saveas; only final is permitted

Line 112 - The method getContentResolver() is undefined for the type new View.OnClickListener(){}





Those are the main ones bugging me. I have the onclick listenier setup just fine. I just got the other code from those guides you posted on the context menus and such.
brybam
Developer
Developer
 
Posts: 27
Joined: Fri May 21, 2010 1:03 am

Top

Re: Set a resource .mp3 for notification or ringtone

Postby brybam » Tue Jun 29, 2010 8:57 pm

Any ideas? After going through the tutorials it looks about right to me. Maybe the tutorials are leaving some key things out?
brybam
Developer
Developer
 
Posts: 27
Joined: Fri May 21, 2010 1:03 am

Re: Set a resource .mp3 for notification or ringtone

Postby stisi » Sun Sep 26, 2010 7:29 am

Did you resolve the problem?

Would be nice when you upload your project for other users, if it works. :run:

Thanks
stisi
Once Poster
Once Poster
 
Posts: 1
Joined: Fri Sep 17, 2010 4:18 pm

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 10 guests