2011-01-22 9 views
1

を押され、どのように私はは、以下のコードへの相対

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

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

values.put(MediaStore.MediaColumns.TITLE, “sound1″); 
を作るのですか

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

私のサウンドファイルはsound1、sound2、sound3などとして保存されたサウンドアレイにあります。各サウンドファイルにはボタンがあります。 Click to play、long clickは、以下のコンテキストメニューを表示します。

何か助けやヒントがあれば幸いです。


1:あなたのringbuttonsのプレイするには、メニューに追加されているIDと別の配列を格納するために2つの配列.oneを取る、私はあなたが以下の変更を使用してゾルを得たことができると思いますので、

//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; 
    } 
+0

のplsを、あなたはどのようなメニュー項目がyou.itを持っていることはウル上のサウンドファイルの各方法を詳しく説明することができますメニュー項目? –

+0

はい、サウンドファイル – Aaron

+0

ごとにボタンが表示されるので、着信音の各メニュー項目のIDを取得できますか? –

答えて

1

OK thtボタンサウンドの対応するサウンドアレイIDをそれぞれ指定します。

exa.`int btnid =新しいint型[回数]について

; //カウントがリング int型rawid = neのINT [回数]を再生するためのボタンがないです。

` より:後に同じと比べて:あなたは、メニュー項目をクリックし、THTから、あなたのbtnid配列とはfunction1,3に比較し、一致したbtnidの配列のインデックス

2を取得する必要がありますのボタンのIDをキャッチすることができますインデックスは、rawid配列内のrawsourceを見つけることができます。

ウル第一PROBは、ファイル名のTHT二PROBのために今slovedさ

あなたはちょうどこの試してみてください。

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

ありがとうございます。正直言って私が読んだ提案を使って私のサウンドアレイを構築したとき、あなたの提案をすべて実装する方法はかなり分かりません。また、配列インデックスのidを節約関数に渡す方法はあまりよく分かりませんが、これは間違いなく役立ちます。私は本当にありがとうございました。再度、感謝します! – Aaron

+0

私はショートカット構文int [] anArray = {R.raw.sound1、R.raw.sound2など}を使用して配列を作成しました。ボタンのR.id.sound1等を表示する。私はそれらを使用する方法、インデックス番号をキャッチしてそれをアクティビティの関数に渡す方法を知らない。私が見つけたすべてのチュートリアルは、インデックスの作成に終わり、使用することはありません。 – Aaron

+0

aronメニューclick.ok.soからmenuitemidを取得することができます。それに応じて、idリングメニューメニュー項目を開始し、index.itsを把握することができます。これで、ロジックを実装する必要があります。 –

関連する問題