gpt4 book ai didi

android - 如何在单击按钮时将音频文件设置为铃声?

转载 作者:行者123 更新时间:2023-11-29 02:23:07 25 4
gpt4 key购买 nike

我想将一个 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();
}
}

}

here

最佳答案

创建方法并将其放入其中

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

关于android - 如何在单击按钮时将音频文件设置为铃声?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53985410/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com