所以我接近我的代码。我一直试图让我的Soundboard现在使用Set As Ringtone两天。很明显我需要使用Arrays。
代码的三个部分,但不太确定如何实现它。
int [] buttonArray = {R.id.button1, R.raw.button2};
for (int i=0; i<buttonArray.length; i++){
buttonArray[i];
}
public void onClick(View v) {
switch (v.getId()) {
case buttonArray[i]:
@Ted - 好的,下面是我的完整代码。 Soundboard部分效果很好,它是Set as Ringtone无法正常工作。而且我知道为什么。我使用R.raw.ifightsong作为函数。我想我需要一个阵列,以便区分声音。更不用说我将有10个按钮,所以我将有很多代码。我想通过使用数组来消除大量代码。
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;
}
}
修改 请在下面查看我的getTag for contextmenu。问题是,我是否关闭了特德?
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();
}}
答案 0 :(得分:0)
这段代码毫无意义。这句话:
int [] = {R.raw.sound1, R.raw.sound2};
不合法Java - 没有变量名。
for
循环的正文不是声明(它不会做任何东西),所以这也不合法。
case
值必须是编译时常量。您不能使用case
在不同的运行时值中进行选择。你必须像这样重新做switch
:
int id = v.getId();
for (int button : buttonArray) {
if (id == button) {
// got a hit -- process it
break; // then end the for loop
}
}
修改强>
好的,我想我知道你要做什么了。当我做这种事情时,我发现使用android:tag
属性很有用;它比你想做的更方便。您可以在布局中设置标记或在代码中执行标记。这是一种在代码中执行此操作的方法。
int[] soundArray = { /* ... */ };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
fightsong = (Button) findViewById(R.id.fightsong_button);
fightsong.setOnClickListener(this);
fightSong.setTag("0");
oniowa = (Button) findViewById(R.id.oniowa_button);
oniowa.setOnClickListener(this);
oniowa.setTag("1");
registerForContextMenu(fightsong);
registerForContextMenu(oniowa);
}
@Override
public void onClick(View v) {
int tag;
try {
tag = Integer.parseInt(v.getTag());
} catch (Exception ex) {
// null or invalid tag -- how'd that happen?
return;
}
// clear out old media player (if any)
if (mMediaPlayer != null) {
if (mMediaPlayer.isPlaying()) {
mMediaPlayer.stop();
}
mMediaPlayer.release();
}
// fire up a media player with the correct sound
mMediaPlayer = MediaPlayer.create(this, soundArray[tag]);
mMediaPlayer.start();
}
创建上下文菜单时,您也可以使用标记而不是视图ID。
答案 1 :(得分:0)
此问题的示例解决方案如下 关于swicth case中的代码块
you are repeating the information regarding mMediaPlayer in each case which is increasing your code.
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;
}}
此外,为了减少代码行而不是swith case,你可以使用if else以更少的代码实现相同的功能。