2010-09-09 98 views
0

如何将声音设置为长按按钮上的铃声?Android如何在长按按钮上设置声音作为铃声

在它仅适用于sound4但不sound5

包com.test.soundboard的那一刻;

import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import android.view.ContextMenu.ContextMenuInfo;
import android.widget.Button;
import android.widget.Toast; import android.app.Activity; import android.content.ContentValues; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.provider.MediaStore; import android.view.ContextMenu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener;

公共类SoundBoardTest延伸活动实现OnClickListener {

private SoundManager mSoundManager; 

@Override 公共无效的onCreate(捆绑savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main);

mSoundManager = new SoundManager(); 
    mSoundManager.initSounds(getBaseContext()); 
    mSoundManager.addSound(1, R.raw.sound4); 
    mSoundManager.addSound(2, R.raw.sound5); 

//按钮播放声音时按下该按钮

View SoundButton4 = findViewById(R.id.SoundButton4); 
    SoundButton4.setOnClickListener(this); 

    View SoundButton5 = findViewById(R.id.SoundButton5); 
    SoundButton5.setOnClickListener(this); 

}

 public void onClick(View v) { 
      switch (v.getId()) { 

      case R.id.SoundButton4: 
    mSoundManager.playSound(1); 
      break; 

     case R.id.SoundButton5: 
mSoundManager.playSound(2); 
      break; 
} 

//当使用长按钮触动提出来供储存为铃声上下文菜单或通知

Button SoundButton4 = (Button) findViewById(R.id.SoundButton4); 
    registerForContextMenu(SoundButton4); 

    Button SoundButton5 = (Button) findViewById(R.id.SoundButton5); 
    registerForContextMenu(SoundButton5); 
} 

// CONTEXT MEN U FOR BUTTON 1 @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.sound4)){ 
     // 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 (savering(R.raw.sound4)){ 
     // 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(); 
     }  


} 

公共布尔savering(INT ressound){
字节[]缓冲= 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="soundtest4"+".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, "HahaSound"); 
    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, false); 
    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

这是因为你保存到文件的设置静态每次都保存R.raw.sound4(你写savering(R.raw.sound4))。您需要使用您传递给它的变量(ressound)并使用它来决定保存哪个文件。

要做到这一点,最好的办法可能是创造声音的阵列(或它们的引用)像这样(这正好在你的java文件开始与可变减速的其余部分):

int[] soundArray = {R.raw.sound1,R.raw.sound2,R.raw.sound3}; // etc... 

哪你会添加到SoundManager类像这样(这正好你在哪里手动添加的声音)

for (int i=0; i<soundArray.length; i++){ // Loops over the sound array 
    mSoundManager.addSound(i, soundArray[i]); // Adds sounds to sound manager 
    } 

然后你savering功能将使用传递的参数知道哪些文件进行保存。 (这正好在那里你以前称为函数)

savering(soundArray[ressound]); 
+0

我真的不明白我应该在哪里添加你提到的代码,请你能不能用什么SoundBoardTest.java代码和SoundManager.java的代码实际上是再次回复, (有10个声音,sound1,sound2等..) 许多Thankyou的 – lucy 2010-09-09 12:40:30

+1

我不会为你写程序,我已经添加了一些解释每个项目应该在哪里。 – stealthcopter 2010-09-09 13:40:12