0
我试图从我的活动中设置声音文件作为铃声? 我尝试了很多代码没有发生,并没有出现错误!如何将声音设置为GridView的铃声?
这是我的代码:
package com.De7kat.Grid;
import java.io.File;
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) {
int j = 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的项目将发挥的声音文件一个依赖于项目
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
// Toast.makeText(De7katGrid.this, "" + position, Toast.LENGTH_SHORT).show();
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;
}
}
当用户的ID和位置点击长按菜单将出现选择“设置为铃声”的声音位置,他们选择
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;
}
这里必须发送mSongsIds [position]到saveas函数,但它会导致错误原因位置在这个级别没有定义?所以我通过R.raw.b1我的一个声音文件来测试功能
public void function1(int id){
saveas(R.raw.b_1);
Toast.makeText(this, "function 1 called", Toast.LENGTH_SHORT).show();
}
public void function2(int id){
Toast.makeText(this, "function 2 called", Toast.LENGTH_SHORT).show();
}
这里是我的功能来设置铃声?不工作,因为我跟踪它,它达到它的结束..
public boolean saveas(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/ringtone/";
String filename="examplefile"+".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, "mysong.mp3"); // path is a file to /sdcard/media/ringtone
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, "My Song title");
values.put(MediaStore.MediaColumns.SIZE, 215454);
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
values.put(MediaStore.Audio.Media.ARTIST, "Madonna");
values.put(MediaStore.Audio.Media.DURATION, 230);
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);
//Insert it into the database
Uri uri = MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath());
Uri newUri = this.getContentResolver().insert(uri, values);
RingtoneManager.setActualDefaultRingtoneUri(
this,
RingtoneManager.TYPE_RINGTONE,
newUri
);
//Insert it into the database,, << I didn't get this one ?? what database
this.getContentResolver().insert(MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath()), values);
return true;
}
}
您是否试过SoundPool? – Yahor10