使用长按和上下文菜单将声音设置为铃声?

时间:2012-11-30 19:14:30

标签: android

按下长按,我正在尝试将声音设置为铃声 用户点击一下我的应用程序播放声音, 我将声音保存为整数数组,用于它们在Raw文件上的位置 当我按下长按然后用户可以选择它需要的动作时,我使用下面的代码显示上下文菜单,我被困在哪里我的代码应该从gridview上的clicked项获得特定声音并将其设置为铃声 我的代码是:

package com.De7kat.Grid;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.Toast;

public class De7katGrid extends Activity {
private MediaPlayer mp;

public Integer[] mSongsIds = { R.raw.l_1, R.raw.l_2, R.raw.l_3, R.raw.l_4,
        R.raw.l_5, R.raw.l_6, R.raw.l_7, R.raw.l_8, R.raw.tom, R.raw.love,
        R.raw.b_1, R.raw.b_2, R.raw.b_3, R.raw.b_4, };

public Object getItem(int position) {
    return mSongsIds[position];

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

    GridView gridView = (GridView) findViewById(R.id.grid_view);
    registerForContextMenu(gridView);
    // Instance of ImageAdapter Class
    gridView.setAdapter(new ImageAdapter(this));

    gridView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View v,
                int position, long id) {

            stopPlaying();
            mp = MediaPlayer.create(De7katGrid.this, mSongsIds[position]);
            mp.start();

        }
    });
}

@Override
public void onPause() {
    super.onPause();
    stopPlaying();

}

private void stopPlaying() {
    if (mp != null) {
        mp.stop();
        mp.release();
        mp = null;

    }
}

    public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo)         {  
    super.onCreateContextMenu(menu, v, menuInfo);  
        menu.setHeaderTitle("Context Menu");  
        menu.add(0, v.getId(), 0, "Set As RingTone");  
        menu.add(0, v.getId(), 0, "Set As Notification");


    }  


    public boolean onContextItemSelected(MenuItem item) {  
        if(item.getTitle()=="Set As RingTone"){
            function1(item.getItemId());
            }  
        else if(item.getTitle()=="Set As Notification"){function2(item.getItemId());}  
        else {return false;}  
    return true;  
    }  

    public  void function1(int id){ 

        Toast.makeText(this, "function 1 called", Toast.LENGTH_SHORT).show();
        // >>>  savering(mSongsIds[position]); <<<< Here I want to get sound as above where I can play sound by getting it's position from mSongsIds array

    }


    public void function2(int id){  
        Toast.makeText(this, "function 2 called", 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;  
          }  

}

0 个答案:

没有答案