2

我想知道当用户在 Activity 中长按铃声名称时,有什么方法可以设置从 url 路径获取的铃声。

波纹管代码我试图将最终路径从 R.raw.sound1 更改为 URL 路径,但仍然不能。

任何人都可以告诉我如何设置从 URL 获取文件的铃声?

这是我的代码

final MediaPlayer ring01 = MediaPlayer.create(this, R.raw.sound1);

// play sound files on clicks
    Button s01 = (Button) findViewById(R.id.btnring01); 
    s01.setText(this.getString(R.string.title01));
    s01.setOnClickListener(new OnClickListener()
    {
        public void onClick(View v)
        {
    try 
            {
        ring01.prepare();
    }
            catch (IllegalStateException e)
            {
        e.printStackTrace();
    } 
            catch (IOException e)
            {
        e.printStackTrace();
        }
    ring01.start();             
    }
});
    registerForContextMenu(s01);

public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {  
super.onCreateContextMenu(menu, v, menuInfo);  
        menu.setHeaderTitle("Save as...");  
        menu.add(0, v.getId(), 0, "Tab Here To Save As Ringtone");    
    }  

@Override  
public boolean onContextItemSelected(MenuItem item) {  
    if(item.getTitle()=="Tab Here To Save As Ringtone"){function1(item.getItemId());}  
    else {return false;}  
return true;  
}

public boolean function1(int ressound){

    String soundname = "";

    switch(ressound)
    {
        case R.id.btnring01:
        ressound=R.raw.aftermath;
        soundname = (this.getString(R.string.title01));
        break;
    }

  //and so on and so on.....
  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=soundname+".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;
   }
 File k = new File(path, filename);

   ContentValues values = new ContentValues();
   values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
   values.put(MediaStore.MediaColumns.TITLE, soundname);
   values.put(MediaStore.MediaColumns.SIZE, 215454);
   values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/ogg");
   values.put(MediaStore.Audio.Media.ARTIST, "Sokun Kanha");
   values.put(MediaStore.Audio.Media.DURATION, 230);
   values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
   //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);
   values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
   values.put(MediaStore.Audio.Media.IS_ALARM, false);
   values.put(MediaStore.Audio.Media.IS_MUSIC, false);
4

0 回答 0