1

以下のコードに関連して、どのように作成しますか

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

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

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

保存されるサウンドファイルが押されたボタンに基づいているように、静的ではなく動的ですか?

サウンド ファイルは、sound1、sound2、sound3 などとして保存された soundArray にあります。各サウンド ファイルにはボタンがあります。クリックして再生します。長押しすると、以下のコンテキスト メニューが表示されます。

どんな助けやヒントも大歓迎です。

//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; 
    }
4

1 に答える 1

1

次の変更を使用することで解決策を得ることができると思います。

1:2つの配列を取ります。1つは、再生するメニューに追加するリングボタンIDを格納するためのもので、もう1つは、そのボタンのサウンドに対応するサウンド配列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を見つけることができます。


最初の問題は解決されました。ファイル名の2番目の確率については、次のことを試してください。

String filename="sound"+id+".ogg";//id is index of array 
于 2011-01-22T05:59:46.257 に答える