因为工作需要,再加上个人爱好,经过分析整理出短彩应用中从发送至收到附件为音频的彩信的下载,预览,播放整个流程,给大家一起分享。
第一步,添加附件:ComposeMessageActivity类下,addAttachement();
private void addAttachment(int type, boolean replace) {
// Calculate the size of the current slide if we're doing a replace so the
// slide size can optionally be used in computing how much room is left for an attachment.
int currentSlideSize = 0;
SlideshowModel slideShow = mWorkingMessage.getSlideshow();
if (replace && slideShow != null) {
WorkingMessage.removeThumbnailsFromCache(slideShow);
SlideModel slide = slideShow.get(0);
currentSlideSize = slide.getSlideSize();
}
switch (type) {
case AttachmentTypeSelectorAdapter.ADD_IMAGE:
MessageUtils.selectImage(this, REQUEST_CODE_ATTACH_IMAGE);
break;
case AttachmentTypeSelectorAdapter.TAKE_PICTURE: {
MessageUtils.capturePicture(this, REQUEST_CODE_TAKE_PICTURE);
break;
}
case AttachmentTypeSelectorAdapter.ADD_VIDEO:
MessageUtils.selectVideo(this, REQUEST_CODE_ATTACH_VIDEO);
break;
case AttachmentTypeSelectorAdapter.RECORD_VIDEO: {
long sizeLimit = computeAttachmentSizeLimit(slideShow, currentSlideSize);
if (sizeLimit > 0) {
MessageUtils.recordVideo(this, REQUEST_CODE_TAKE_VIDEO, sizeLimit);
} else {
Toast.makeText(this,
getString(R.string.message_too_big_for_video),
Toast.LENGTH_SHORT).show();
}
}
break;
case AttachmentTypeSelectorAdapter.ADD_SOUND:
MessageUtils.selectAudio(this, REQUEST_CODE_ATTACH_SOUND);
break;
case AttachmentTypeSelectorAdapter.RECORD_SOUND:
long sizeLimit = computeAttachmentSizeLimit(slideShow, currentSlideSize);
MessageUtils.recordSound(this, REQUEST_CODE_RECORD_SOUND, sizeLimit);
break;
case AttachmentTypeSelectorAdapter.ADD_SLIDESHOW:
editSlideshow();
break;
case AttachmentTypeSelectorAdapter.ADD_CONTACT_AS_TEXT:
pickContacts(MultiPickContactsActivity.MODE_INFO,
replace ? REQUEST_CODE_ATTACH_REPLACE_CONTACT_INFO
: REQUEST_CODE_ATTACH_ADD_CONTACT_INFO);
break;
case AttachmentTypeSelectorAdapter.ADD_CONTACT_AS_VCARD:
pickContacts(MultiPickContactsActivity.MODE_VCARD,
REQUEST_CODE_ATTACH_ADD_CONTACT_VCARD);
break;
default:
break;
}
}
第二步,选择音频类型:MessageUtils类中的selectAudio()方法;
public static void selectAudio(final Activity activity, final int requestCode) {
// Compare other phone's behavior, we are not only display the
// RingtonePick to add, we could have other choices like external audio
// and system audio. Allow the user to select a particular kind of data
// and return it.
String[] items = new String[2];
items[SELECT_SYSTEM] = activity.getString(R.string.system_audio_item);
items[SELECT_EXTERNAL] = activity.getString(R.string.external_audio_item);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(activity,
android.R.layout.simple_list_item_1, android.R.id.text1, items);
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
AlertDialog dialog = builder.setTitle(activity.getString(R.string.select_audio))
.setAdapter(adapter, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent audioIntent = null;
switch (which) {
case SELECT_SYSTEM:
audioIntent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);//android.intent.action.RINGTONE_PICKER
//add by zhihui.wang for SWBUG00028878 at 2014-5-5.
audioIntent.addCategory("android.intent.category.SIMRINGTONE");
audioIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, false);
audioIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, false);
audioIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_INCLUDE_DRM, false);
audioIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE,
activity.getString(R.string.select_audio));
audioIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, false);
break;
case SELECT_EXTERNAL:
audioIntent = new Intent();
audioIntent.setAction(Intent.ACTION_PICK);
audioIntent.setData(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
break;
}
activity.startActivityForResult(audioIntent, requestCode);
}
})
.create();
dialog.show();
}
在上述代码中弹出Choose audio对话框,选项分别为System audio和External audio,我们这里选择System audio选项,这里在Intent中封装了彩铃设置为没有默认选项(EXTRA_RINGTONE_SHOW_DEFAULT),非静音状态(EXTRA_RINGTONE_SHOW_SILENT),非数字版权管理(EXTRA_RINGTONE_INCLUDE_DRM),标题(EXTRA_RINGTONE_TITLE)-Choose audio
第三步,选择音频文件:RingtonePickerActivity类,该类继承了AlertActivity类,选择音频文件,OK;
public void onClick(DialogInterface dialog, int which) {
boolean positiveResult = which == DialogInterface.BUTTON_POSITIVE;
// Should't response the "OK" and "Cancel" button's click event at the
// same time.
if (mIsHasClick || (mCursor == null)) {
return;
}
mIsHasClick = true;
// Stop playing the previous ringtone
mRingtoneManager.stopPreviousRingtone();
if (positiveResult) {
Intent resultIntent = new Intent();
Uri uri = null;
if (mClickedPos == mDefaultRingtonePos) {
// Set it to the default Uri that they originally gave us
uri = mUriForDefaultItem;
} else if (mClickedPos == mSilentPos) {
// A null Uri is for the 'Silent' item
uri = null;
} else {
uri = mRingtoneManager.getRingtoneUri(getRingtoneManagerPosition(mClickedPos));
}
resultIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI, uri);//所选音频文件的地址
setResult(RESULT_OK, resultIntent);//RESULT_OK=-1
} else {
setResult(RESULT_CANCELED);
}
getWindow().getDecorView().post(new Runnable() {
public void run() {
mCursor.deactivate();
}
});
finish();
}