[英]How to use a button to set a sound as ringtone in android?
I wrote this simple Main.java and need to know how to use simple codes to set a soundpool or mediaplayer as a ringtone. 我写了这个简单的Main.java,需要知道如何使用简单的代码将声音池或媒体播放器设置为铃声。 Direct question is: set as ringtone by button "b2"
直接的问题是: 通过按钮“ b2”设置为铃声
there are few sources in stackoverflow but I could n't understand any of them. stackoverflow中的资源很少,但我听不懂。 thanks in advance
提前致谢
My codes: 我的代码:
public class Main extends Activity implements OnClickListener{
SoundPool sp;
int dicesound;
Button play, setAsRingtone;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sp = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
dicesound = sp.load(this, R.raw.onedice, 1);
play = (Button) findViewById(R.id.b1);
setAsRingtone = (Button) findViewById(R.id.b2);
play.setOnClickListener(this);
setAsRingtone.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.b1:
sp.play(dicesound, 1, 1, 0, 0, 1);
break;
case R.id.b2:
// HOW TO SET "dicesound" SOUND AS A RINGTONE ???
break;
}
}
} }
Please check below code , hope it helps you 请检查以下代码,希望对您有所帮助
Replace "com.example.sample" with your package name 用包名称替换“ com.example.sample”
Uri m_path = Uri.parse("android.resource://com.example.sample/" + R.raw.onedice);
RingtoneManager.setActualDefaultRingtoneUri(activity.this,RingtoneManager.TYPE_RINGTONE, m_path);
First of All You need to copy the Your Sound File (Raw file to sd card)to SD Card ant then used following code to set as ringtone.: 首先,您需要将您的声音文件(原始文件复制到sd卡)复制到SD卡ant,然后使用以下代码将其设置为铃声。
String filepath ="/sdcard/myring.mp3";
File ringtoneFile = new File(filepath);
//To set a ringtone you have to add it to the database.
//otherwise it will not be set also gives no error also. //否则将不会被设置也不会给出错误。
ContentValues content = new ContentValues();
content.put(<span id="IL_AD7" class="IL_AD">MediaStore</span>.MediaColumns.DATA,ringtoneFile.getAbsolutePath());
content.put(MediaStore.MediaColumns.TITLE, "test");
content.put(MediaStore.MediaColumns.SIZE, 215454);
content.put(MediaStore.MediaColumns.MIME_TYPE, "audio/*");
content.put(MediaStore.Audio.Media.ARTIST, "artist");
content.put(MediaStore.Audio.Media.DURATION, 230);
content.put(MediaStore.Audio.Media.IS_RINGTONE, true);
content.put(MediaStore.Audio.Media.IS_NOTIFICATION, <span id="IL_AD4" class="IL_AD">false</span>);
content.put(MediaStore.Audio.Media.IS_ALARM, false);
content.put(MediaStore.Audio.Media.IS_MUSIC, false);
Insert it into the database 将其插入数据库
Log.i(TAG, "the absolute path of the file is :"+ringtoneFile.getAbsolutePath());
Uri uri = MediaStore.Audio.Media.getContentUriForPath(
ringtoneFile.getAbsolutePath());
Uri newUri = context.getContentResolver().<span id="IL_AD5" class="IL_AD">insert</span>(uri, content);
ringtoneUri = newUri;
Log.i(TAG,"the ringtone uri is :"+ringtoneUri);
RingtoneManager.setActualDefaultRingtoneUri(context,
RingtoneManager.TYPE_RINGTONE,newUri);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.