android铃声 - 如何添加从URL路径获取的铃声

时间:2012-10-09 12:30:34

标签: android

我想知道当用户长按活动中的铃声名称时,有没有办法设置从网址路径获取的铃声。

Bellow代码我试图将R.raw.sound1的filnal路径更改为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);

0 个答案:

没有答案