我要在按钮按下时将mp3文件设置为铃声。我已经在我的代码文件中附加了这个问题。正如您在图像中看到的那样,一旦用户单击设置1按钮,对于其他设置按钮,我想将shiv shiv设置为铃声,每个设置按钮将设置在其栏中显示的铃声。
我添加了一些代码来将mp3设置为铃声,但是它不起作用
package com.techjapreet.shivshankarkiringtone;
import android.content.ContentValues;
import android.content.Context;
import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.PopupMenu;
import android.widget.Toast;
import java.io.File;
public class ringtone_tab extends AppCompatActivity {
Button clk1;
Button clk2;
Button clk3;
Button clk4;
Button clk5;
Button clk6;
private Button ring1;
MediaPlayer mediaPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ringtone_tab);
ring1 = findViewById(R.id.btn_setting1);
ring1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String filepath ="/sdcard/sample/"+currentName+"";
System.out.println("/sdcard/sample/"+currentName+"");
File ringtoneFile = new File(filepath);
ContentValues content = new ContentValues();
content.put(MediaStore.MediaColumns.DATA,ringtoneFile.getAbsolutePath());
content.put(MediaStore.MediaColumns.TITLE, currentName);
content.put(MediaStore.MediaColumns.SIZE, 215454);
content.put(MediaStore.MediaColumns.MIME_TYPE, "audio/*");
// content.put(MediaStore.Audio.Media.ARTIST, "Madonna");
content.put(MediaStore.Audio.Media.DURATION, 230);
content.put(MediaStore.Audio.Media.IS_RINGTONE, true);
content.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
content.put(MediaStore.Audio.Media.IS_ALARM, true);
content.put(MediaStore.Audio.Media.IS_MUSIC, true);
String Ringtonepath= "content://media/internal/audio/media/297";
Uri Ringtone1 = Uri.parse(filepath);
//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());
getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + ringtoneFile.getAbsolutePath() + "\"",
null);
Uri newUri = getContentResolver().insert(uri, content);
System.out.println("uri=="+uri);
Log.i("TAG","the ringtone uri is :"+newUri);
RingtoneManager.setActualDefaultRingtoneUri(
getApplicationContext(), RingtoneManager.TYPE_RINGTONE,
newUri);
}
});
clk6 = (Button) findViewById(R.id.btn_play6);
clk5 = (Button) findViewById(R.id.btn_play5);
clk4 = (Button) findViewById(R.id.btn_play4);
clk3 = (Button) findViewById(R.id.btn_play3);
clk2 = (Button) findViewById(R.id.btn_play2);
clk1 = (Button) findViewById(R.id.btn_play1);
mediaPlayer = new MediaPlayer();
}
public void setBtn_play6(View v)
{
stopPlayer();
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_vandana);
mediaPlayer.start();
}
public void setBtn_play5(View v)
{
stopPlayer();
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_tandav_mantra);
mediaPlayer.start();
}
public void setBtn_play4(View v)
{
stopPlayer();
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_shiv_om);
mediaPlayer.start();
}
public void setBtn_play3(View v)
{
stopPlayer();
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_shiv);
mediaPlayer.start();
}
public void setBtn_play2(View v)
{
stopPlayer();
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_aaradhna);
mediaPlayer.start();
}
public void setBtn_play1(View v)
{
stopPlayer();
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_shankar);
mediaPlayer.start();
}
private void stopPlayer(){
if(mediaPlayer != null && mediaPlayer.isPlaying())
{mediaPlayer.stop();
mediaPlayer.reset();
mediaPlayer.release();
}
}
}
答案 0 :(得分:0)
创建方法并将其放入其中
String filepath ="/sdcard/sample/"+currentName+"";
System.out.println("/sdcard/sample/"+currentName+"");
File ringtoneFile = new File(filepath);
ContentValues content = new ContentValues();
content.put(MediaStore.MediaColumns.DATA,ringtoneFile.getAbsolutePath());
content.put(MediaStore.MediaColumns.TITLE, currentName);
content.put(MediaStore.MediaColumns.SIZE, 215454);
content.put(MediaStore.MediaColumns.MIME_TYPE, "audio/*");
// content.put(MediaStore.Audio.Media.ARTIST, "Madonna");
content.put(MediaStore.Audio.Media.DURATION, 230);
content.put(MediaStore.Audio.Media.IS_RINGTONE, true);
content.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
content.put(MediaStore.Audio.Media.IS_ALARM, true);
content.put(MediaStore.Audio.Media.IS_MUSIC, true);
String Ringtonepath= "content://media/internal/audio/media/297";
Uri Ringtone1 = Uri.parse(filepath);
//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());
getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + ringtoneFile.getAbsolutePath() + "\"",
null);
Uri newUri = getContentResolver().insert(uri, content);
System.out.println("uri=="+uri);
Log.i("TAG","the ringtone uri is :"+newUri);
RingtoneManager.setActualDefaultRingtoneUri(
getApplicationContext(), RingtoneManager.TYPE_RINGTONE,
newUri);
只需在您的按钮内调用此方法
,如果您想让用户从设备中选择自己的文件
添加下一行以获取文件路径
Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("file/*");
startActivity(intent);