我已经成功编写了一个程序,将歌曲设置为铃声,但面临一个非常小的问题
当我第一次点击按钮将歌曲设为铃声时,请点击此处:
但是当我再次点击按钮(第二次)时,得到这个:
那么可能是什么原因?为什么我第一次也没有成功......
以下是 RingtoneActivity.java 的完整代码:
public class RingtoneActivity extends Activity {
private final Context context = this;
private static final String TAG = "Meri Desi Look";
private File sound;
private final File folder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_RINGTONES);
private MediaPlayer mediaPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button buttonRingtone = (Button) findViewById(R.id.btnRingtone);
buttonRingtone.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ask();
}
});
}
private void ask() {
mediaPlayer = MediaPlayer.create(this, R.raw.meri_desi_look);
mediaPlayer.start();
AlertDialog.Builder alert = new AlertDialog.Builder(context);
alert.setTitle("Meri Desi Look");
alert.setMessage("You want this sound as your ringtone?");
alert.setCancelable(false);
alert.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int whichButton) {
mediaPlayer.stop();
mediaPlayer.reset();
confirmed();
dialog.dismiss();
}
});
alert.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int whichButton) {
mediaPlayer.stop();
mediaPlayer.reset();
dialog.dismiss();
}
});
alert.show();
}
/**
* When the user confirms the popup, this will run, saving the file and
* setting the ringtone utilising the RingtoneManager class.
*/
private void confirmed() {
Boolean success = false;
sound = new File(folder, "meri_desi_look.mp3");
if (!sound.exists()) {
Log.i(TAG, "Writing Meri Desi Look to " + folder.toString());
try {
InputStream in = getResources().openRawResource(R.raw.meri_desi_look);
FileOutputStream out = new FileOutputStream(sound.getPath());
byte[] buff = new byte[1024];
int read = 0;
try {
while ((read = in.read(buff)) > 0) {
out.write(buff, 0, read);
}
} finally {
in.close();
out.close();
}
} catch (Exception e) {
success = false;
Log.i(TAG, "Desi Look failed to write.");
}
} else {
success = true;
Log.i(TAG, "Meri Desi Look ringtone already there.");
}
if (!success) {
onSetRingtoneError("We couldn't give you a Meri Desi Look ringtone.\n\nThere's an issue writing the file.");
} else {
setRingtone();
}
}
/**
* Physically sets the RingtoneManager preferences.
*/
private void setRingtone() {
try {
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, sound.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, "Meri Desi Look - performed by Sunny Leone");
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/*");
values.put(MediaStore.Audio.Media.ARTIST, "Meri Desi Look");
values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
values.put(MediaStore.Audio.Media.IS_ALARM, true);
values.put(MediaStore.Audio.Media.IS_MUSIC, true);
Uri uri = MediaStore.Audio.Media.getContentUriForPath(sound.getAbsolutePath());
getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + sound.getAbsolutePath() + "\"",
null);
Uri newUri = getContentResolver().insert(uri, values);
RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE, newUri);
onSetRingtoneSuccess();
} catch (Exception e) {
onSetRingtoneError("We couldn't give you a Meri Desi Look ringtone.\n\nThere's an issue setting the file.");
}
}
/**
* Generic helper to show error messages (if ever there are any) :p
*
* @param message
*/
private void onSetRingtoneError(String message) {
AlertDialog.Builder alert = new AlertDialog.Builder(context);
alert.setTitle("Meri Desi Look");
alert.setMessage(message);
alert.setCancelable(false);
alert.setPositiveButton("OK. That's too bad.", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
}
});
alert.show();
}
private void onSetRingtoneSuccess() {
AlertDialog.Builder alert = new AlertDialog.Builder(context);
alert.setTitle("Meri Desi Look");
alert.setMessage("You've got the ringtone :)\n\nHope you enjoy it!");
alert.setCancelable(false);
alert.setPositiveButton("Sweet!", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
}
});
alert.show();
}
@Override
protected void onPause() {
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
mediaPlayer.release();
}
super.onPause();
}
}
答案 0 :(得分:0)
我认为你很久以前就已经想到了代码中存在问题的地方。但如果你没有,请尝试以下方法。
private void confirmed() {
...
try {
while ((read = in.read(buff)) > 0) {
out.write(buff, 0, read);
}
success = true;
}
catch(Exception e) {
success = false;
e.printStackTrace();
}
finally {
n.close();
out.close();
}
...
}