Android如何通过长按按钮将铃声设置为铃声

如何通过长按按钮将声音设置为铃声?

目前它只适用于声音4但不适合声音5

包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;
导入android.widget.Button;
导入android.widget.Toast; 导入android.app.Activity; 导入android.content.ContentValues; 导入android.content.Intent; 导入android.net.Uri; 导入android.os.Bundle; 导入android.provider.MediaStore; 导入android.view.ContextMenu; 导入android.view.MenuItem; 导入android.view.View; 导入android.view.View.OnClickListener;

公共类SoundBoardTest扩展活动实现OnClickListener {

private SoundManager mSoundManager;

@Override public void onCreate(Bundle 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);  
}  

//上下文菜单按钮1 @Override
public void onCreateContextMenu(ContextMenu菜单,View v,ContextMenuInfo menuInfo){
super.onCreateContextMenu(menu,v,menuInfo);
menu.setHeaderTitle(“另存为...”);
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();
        }       


}

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="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;  
  }  

}


这是因为你保存到文件是静态设置的,每次保存R.raw.sound4(你已经写了保存(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(soundArray[ressound]);
链接地址: http://www.djcxy.com/p/26217.html

上一篇: Android How to set a sound as ringtone with long press on button

下一篇: starting/stopping service multiple times not working