gpt4 book ai didi

java - 使保存到 SD 卡和上下文菜单功能动态,或相对于按下的按钮

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

相对于下面的代码,我该如何制作

if (savering(R.raw.sound1)){

String filename=”sound1″+”.ogg”;

values.put(MediaStore.MediaColumns.TITLE, “sound1″);

动态而不是静态,以便将保存的声音文件基于按下的按钮?

我的声音文件在一个 soundArray 中,保存为 sound1、sound2、sound3 等。每个声音文件都有一个按钮。点击播放,长按弹出下方上下文菜单。

如有任何帮助或提示,我们将不胜感激。

//CONTEXT MENU

@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=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="sound1"+".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, "sound1 Ringtone");
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, 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="sound1"+".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, "sound1 Notification");
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, 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;
}

最佳答案

我认为您可以通过以下更改获得解决方案:

1:取两个数组。一个用于存储您要添加到菜单中以播放的铃声按钮 ID,另一个数组分别具有该按钮声音的相应声音数组 ID;

例如,

int btnid=new int[count];//count is no of buttons for playing ring
int rawid=ne int[count];

然后你可以捕获点击的菜单项的按钮的 ID,你必须在 function1 中与你的 btnid 数组进行比较,并获得匹配的 btnid 数组的索引。

2: 使用相同的索引后,您可以在 rawid 数组中找到 rawsource。


您的第一个问题已解决,现在对于文件名的第二个问题,您只需尝试以下操作:

String filename="sound"+id+".ogg";//id is index of array 

关于java - 使保存到 SD 卡和上下文菜单功能动态,或相对于按下的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4765855/

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