Android How to set a sound as ringtone with long press on button
<p>How do i set the sound as ringtone with long press on the button?</p>

<p>At the moment it only works on sound4 but not sound5</p>

<p>package com.test.soundboard;</p>

<p>import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;import android.view.ContextMenu.ContextMenuInfo;<br>
import android.widget.Button;<br>
import android.widget.Toast; 
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;</p>

<p>public class SoundBoardTest extends Activity implements OnClickListener{</p>

<pre><code>private SoundManager mSoundManager;
</code></pre>

<p>@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);</p>

<pre><code>    mSoundManager = new SoundManager();
    mSoundManager.initSounds(getBaseContext());
    mSoundManager.addSound(1, R.raw.sound4);
    mSoundManager.addSound(2, R.raw.sound5);
</code></pre>

<p>//BUTTONS PLAY SOUND WHEN PRESSED</p>

<pre><code>    View SoundButton4 = findViewById(R.id.SoundButton4);
    SoundButton4.setOnClickListener(this);

    View SoundButton5 = findViewById(R.id.SoundButton5);
    SoundButton5.setOnClickListener(this);
</code></pre>

<p>}</p>

<pre><code>        public void onClick(View v) {
            switch (v.getId()) {

            case R.id.SoundButton4:
    mSoundManager.playSound(1);
            break;

        case R.id.SoundButton5:
mSoundManager.playSound(2);
            break;
}
</code></pre>

<p>//WHEN LONG PRESSED BUTTONS BRING UP CONTEXT MENU FOR SAVE AS RINGTONE OR NOTIFICATION</p>

<pre><code>    Button SoundButton4 = (Button) findViewById(R.id.SoundButton4);  
    registerForContextMenu(SoundButton4);  

    Button SoundButton5 = (Button) findViewById(R.id.SoundButton5);  
    registerForContextMenu(SoundButton5);  
}  
</code></pre>

<p>//CONTEXT MENU FOR BUTTON 1
    @Override<br>
    public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {<br>
    super.onCreateContextMenu(menu, v, menuInfo);<br>
        menu.setHeaderTitle("Save as...");<br>
        menu.add(0, v.getId(), 0, "Ringtone");<br>
        menu.add(0, v.getId(), 0, "Notification");<br>
    }  </p>

<pre><code>@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(R.raw.sound4)){  
        // 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 (savering(R.raw.sound4)){  
        // 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();
        }       


}
</code></pre>

<p>public boolean savering(int ressound){<br>
       byte[] buffer=null;<br>
       InputStream fIn = getBaseContext().getResources().openRawResource(ressound);<br>
       int size=0;  </p>

<pre><code>   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/media/audio/ringtones/";  
   String filename="soundtest4"+".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, "HahaSound");  
   values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/ogg");  
   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, 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;  
  }  
</code></pre>

<p>}</p>
