ListView原始资源设置为铃声

时间:2014-01-28 04:25:50

标签: android listview media-player ringtone

当我按下歌曲播放的项目时,我只有一个文本的列表视图。这很有效,但是当我按onItemLongClick()时,我需要将该歌曲设为铃声。

我编写了代码。现在我的问题是,当我按itemlongclick结果是别的“没有se pudo asignar como铃声”(无法分配为铃声)

所有歌曲都是原始资源。

public class MainActivity extends Activity {

MediaPlayer mp = new MediaPlayer();
ListViewAdapter adapter;
int s1[] =
        {
                R.raw.el_tri_abuelita_soy_tu_nieto,
                R.raw.el_tri_ahi_te_lo_lavas,
                R.raw.el_tri_bajate_del_avion,

        };

int position;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ListView list30 = (ListView) findViewById(R.id.list);

    ArrayList<String> items = new ArrayList<String>();
    items.add("Abuelita soy tu nieto");
    items.add("Ahi te lo lavas");
    items.add("Bajate del avion");

    adapter = new ListViewAdapter(this, items );
    list30.setAdapter(adapter);

    list30.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            method(position); //play de song normaly, this work well
            }
        });
    Log.i("ramiro", "onClickListener");

    list30.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {


 if(saveas(R.raw.el_tri_abuelita_soy_tu_nieto, "examplename") == true)
    Toast.makeText(getApplication(), "Se asigno como ringtone", Toast.LENGTH_SHORT).show();
 else
    Toast.makeText(getApplicationContext(), "No se pudo asignar como ringtone", Toast.LENGTH_SHORT).show();  //out over here
    return false;
        }
    });
}

public boolean saveas(int ressound, String soundName)
{
    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+".mp3";


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

1 个答案:

答案 0 :(得分:0)

我按照了几个关于设置铃声和通知的教程,这就是我最终的结果。您调用 saveas()方法并在原始文件夹中传递声音,然后传递一个字符串 soundName ,这将是您选择铃声时列出的声音的标题。如果失败,该方法将返回false,如果有效则返回true。同样在android清单中你需要允许;
uses-permission android:name =“android.permission.WRITE_EXTERNAL_STORAGE”
uses-permission android:name =“android.permission.WRITE_SETTINGS”
uses-permission android:name =“android.permission.CHANGE_CONFIGURATION”
uses-permission android:name =“android.permission.MODIFY_AUDIO_SETTINGS”

如果您使用的不是.mp3

,请务必根据需要更改代码
saveas(R.raw.examplesound, "examplename")

public boolean saveas(int ressound, String soundName)
{
    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+".mp3";


    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, soundName);
    values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
    values.put(MediaStore.Audio.Media.ARTIST, "artistname ");
    values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
    values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
    values.put(MediaStore.Audio.Media.IS_ALARM, false);
    values.put(MediaStore.Audio.Media.IS_MUSIC, false);

    //Work with the content resolver now
    //First get the file we may have added previously and delete it,
    //otherwise we will fill up the ringtone manager with a bunch of copies over time.
    Uri uri = MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath());
    this.getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + k.getAbsolutePath() + "\"", null);

    //Ok now insert it
    Uri newUri = this.getContentResolver().insert(uri, values);

    RingtoneManager.setActualDefaultRingtoneUri( this,RingtoneManager.TYPE_RINGTONE,newUri);