So I’m close with my code. I’ve been trying to get my Soundboard to utilize Set As Ringtone for two days now. It’s obvious I need to utilize Arrays.
Three sections of code, but not quite sure how to implement it.
1: The array decleration:
int [] buttonArray = {R.id.button1, R.raw.button2};
2: The for statement (WRONG – This is what I’m not sure about)
for (int i=0; i<buttonArray.length; i++){
buttonArray[i];
}
3: The button switch:(WRONG – I know this is wrong too)
public void onClick(View v) {
switch (v.getId()) {
case buttonArray[i]:
@Ted – Ok, below is my full code. The Soundboard portion works great, it’s the Set as Ringtone that’s not working. And I know why. I am using R.raw.ifightsong for the function. I figured I need an array there so it differentiate between the sounds. Not to mention I am going to have 10 buttons and so I”m going to have A LOT of code. I wanted to eliminate A LOT of code by using an array.
package com.hawkaddictsoundboard;
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.media.MediaPlayer;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class HawkeyeAddictSoundboardActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
Button fightsong;
Button oniowa;
MediaPlayer mMediaPlayer;
int[] soundArray = {R.raw.ifightsong,R.raw.oniowa}; // etc...
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
fightsong = (Button) findViewById(R.id.fightsong_button);
fightsong.setOnClickListener(this);
oniowa = (Button) findViewById(R.id.oniowa_button);
oniowa.setOnClickListener(this);
registerForContextMenu(fightsong);
registerForContextMenu(oniowa);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.fightsong_button:
if (mMediaPlayer!=null && mMediaPlayer.isPlaying()) {
mMediaPlayer.stop();
mMediaPlayer.reset();
mMediaPlayer.start();
}
else{
if (mMediaPlayer!=null){
mMediaPlayer.reset();
mMediaPlayer.release();
}
mMediaPlayer = MediaPlayer.create(getBaseContext(), R.raw.ifightsong);
mMediaPlayer.start();
// TODO Auto-generated method stub
}
break;
case R.id.oniowa_button:
if (mMediaPlayer!=null && mMediaPlayer.isPlaying()) {
mMediaPlayer.stop();
mMediaPlayer.reset();
mMediaPlayer.start();
}
else{
if (mMediaPlayer!=null){
mMediaPlayer.reset();
mMediaPlayer.release();
}
mMediaPlayer = MediaPlayer.create(getBaseContext(), R.raw.oniowa);
mMediaPlayer.start();
// TODO Auto-generated method stub
}
break;
}}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Set As...");
menu.add(0, v.getId(), 0, "Ringtone");
menu.add(0, v.getId(), 0, "Notification");
}
@Override
public boolean onContextItemSelected(MenuItem item) {
if(item.getTitle().equals("Ringtone")){function1(item.getItemId());}
else if(item.getTitle().equals("Notification")){function2(item.getItemId());}
else return false;
return true;
}
public void function1(int id){
if (saveringifightsong(R.raw.ifightsong)){
// 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 (savenotifightsong(R.raw.ifightsong)){
// 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();
}}
public boolean saveringifightsong(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/media/audio/ringtones/";
String filename="ifightsong_ring"+".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, "Iowa Fight Song");
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/ogg");
values.put(MediaStore.Audio.Media.ARTIST, "Hawkeye Sounds ");
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 uri = MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath());
getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + k.getAbsolutePath() + "\"", null);
Uri newUri = getContentResolver().insert(uri, values);
RingtoneManager.setActualDefaultRingtoneUri(
HawkeyeAddictSoundboardActivity.this,
RingtoneManager.TYPE_RINGTONE,
newUri
);
return true;
}
public boolean savenotifightsong(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/media/audio/ringtones/";
String filename="ifightsong_not"+".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, "Iowa Fight Song");
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/ogg");
values.put(MediaStore.Audio.Media.ARTIST, "Hawkeye Sounds ");
values.put(MediaStore.Audio.Media.IS_RINGTONE, false);
values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
values.put(MediaStore.Audio.Media.IS_ALARM, false);
values.put(MediaStore.Audio.Media.IS_MUSIC, false);
//Insert it into the database
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(
HawkeyeAddictSoundboardActivity.this,
RingtoneManager.TYPE_NOTIFICATION,
newUri
);
return true;
}
public boolean saveringoniowa(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/media/audio/ringtones/";
String filename="oniowa_ring"+".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, "On Iowa");
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/ogg");
values.put(MediaStore.Audio.Media.ARTIST, "Hawkeye Sounds ");
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 uri = MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath());
getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + k.getAbsolutePath() + "\"", null);
Uri newUri = getContentResolver().insert(uri, values);
RingtoneManager.setActualDefaultRingtoneUri(
HawkeyeAddictSoundboardActivity.this,
RingtoneManager.TYPE_RINGTONE,
newUri
);
return true;
}
public boolean savenotoniowa(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/media/audio/ringtones/";
String filename="oniowa_not"+".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, "On Iowa");
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/ogg");
values.put(MediaStore.Audio.Media.ARTIST, "Hawkeye Sounds ");
values.put(MediaStore.Audio.Media.IS_RINGTONE, false);
values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
values.put(MediaStore.Audio.Media.IS_ALARM, false);
values.put(MediaStore.Audio.Media.IS_MUSIC, false);
//Insert it into the database
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(
HawkeyeAddictSoundboardActivity.this,
RingtoneManager.TYPE_NOTIFICATION,
newUri
);
return true;
}
}
EDIT
Please have a look at my getTag for contextmenu below. The question is, am I close Ted?
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
int tag;
try {
tag = Integer.parseInt((String) v.getTag());
} catch (Exception ex) {
// null or invalid tag -- how'd that happen?
return;
}
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Set As...");
menu.add(0, soundArray[tag], 0, "Ringtone");
menu.add(0, soundArray[tag], 0, "Notification");
}
@Override
public boolean onContextItemSelected(MenuItem item) {
if(item.getTitle().equals("Ringtone")){function1(item.getItemId());}
else if(item.getTitle().equals("Notification")){function2(item.getItemId());}
else return false;
return true;
}
public void function1(int tag){
if (savering(soundArray[tag])){
// 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 tag){
if (savenot(soundArray[tag])){
// 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();
}}
This code makes no sense. This statement:
isn’t legal Java—no variable name.
The body of your
forloop isn’t a statement (it doesn’t do anything), so that’s not legal either.The
casevalues need to be compile time constants. You cannot use acaseto select among different run-time values. You’ll have to re-do theswitchas something like this:EDIT
Okay, I think I see what you’re trying to do. When I do this kind of thing, I find it useful to use the
android:tagattribute; it’s much more convenient than what you’re trying to do. You can set the tag in the layout or do it in code. Here’s a method that does it in code.You can also use the tag instead of the view id when creating the context menu.