gpt4 book ai didi

java - Android Java音板代码仅将第一个按钮设置为铃声/等

转载 作者:行者123 更新时间:2023-11-30 04:46:08 26 4
gpt4 key购买 nike

我一直在玩弄这段代码,显然我正在尝试制作一个音板,可以在长按等情况下保存铃声。

不过,这是一个按钮的基本代码。我一直在互相复制相同的代码,试图制作数组,只是一切......我无法让这段代码按照我想要的方式工作。

当它们是多个按钮和更多代码时。

我可以让所有按钮都播放声音。我可以让菜单弹出每个按钮。不过,第一个按钮始终是被保存的那个,其余的只是从 map 上掉下来。

我假设我在以下代码中的数据流没有被切断并被其他声音按钮拾取。无论哪种方式,它都不起作用。请帮助我。

public class Soundboard extends Activity {
private SoundManager mSoundManager;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

mSoundManager = new SoundManager();
mSoundManager.initSounds(getBaseContext());
mSoundManager.addSound(1, R.raw.sound1);

Button SB = (Button)findViewById(R.id.sound1);

SB.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(1);
}
});

Button btn = (Button) findViewById(R.id.sound1);
registerForContextMenu(btn);
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Save as...");
menu.add(0, v.getId(), 0, "Ringtone");
menu.add(0, v.getId(), 0, "Notification");
}

@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.sound1)) {
// 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 (savenot(R.raw.sound1)) {
// 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();
}
}

//Save into Ring tone Folder
public boolean savering(int ressound){
byte[] buffer=null;
InputStream fIn = getBaseContext().getResources().openRawResource(ressound);
int size=50;

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="ohhh"+".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, "Ohhh Ringtone");
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/ogg");
values.put(MediaStore.Audio.Media.ARTIST, "weee");
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;
}

//Save in Notification Folder
public boolean savenot(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/notifications/";

String filename="ohhh"+".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, "Ohhh Notification");
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/ogg");
values.put(MediaStore.Audio.Media.ARTIST, "weee");
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;
}
}

最佳答案

我只是自己学习 Android 编程,但似乎这就是为什么你总是保存第一个声音:

savering(R.raw.sound1)

savenot(R.raw.sound1)

这两个都明确保存了第一个声音。除非您为每个按钮编写了单独的 function1 和 function2。但必须有比这更好的方式来管理储蓄。

关于java - Android Java音板代码仅将第一个按钮设置为铃声/等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4912778/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com