博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android-铃声的设置与播放
阅读量:5962 次
发布时间:2019-06-19

本文共 5053 字,大约阅读时间需要 16 分钟。

在android系统中,不同铃声存放的铃声路径:
/system/media/audio/ringtones        来电铃声 /system/media/audio/notifications        短信通知铃声 /system/media/audio/alarms        闹钟铃声
铃声的设置:
import java.io.File;import android.app.Activity;import android.content.Intent;import android.media.RingtoneManager;import android.net.Uri;import android.os.Bundle;import android.view.View;import android.widget.Button;public class Activity01 extends Activity{	/* 3个按钮 */	private Button mButtonRingtone;	private Button mButtonAlarm;	private Button mButtonNotification;	/* 自定义的类型 */	public static final int ButtonRingtone			= 0;	public static final int ButtonAlarm				= 1;	public static final int ButtonNotification		= 2;	/* 铃声文件夹 */	private String strRingtoneFolder = "/sdcard/music/ringtones";	private String strAlarmFolder = "/sdcard/music/alarms";	private String strNotificationFolder = "/sdcard/music/notifications";	/** Called when the activity is first created. */	@Override	public void onCreate(Bundle savedInstanceState)	{		super.onCreate(savedInstanceState);		setContentView(R.layout.main);			mButtonRingtone = (Button) findViewById(R.id.ButtonRingtone);		mButtonAlarm = (Button) findViewById(R.id.ButtonAlarm);		mButtonNotification = (Button) findViewById(R.id.ButtonNotification);		/* 设置来电铃声 */		mButtonRingtone.setOnClickListener(new Button.OnClickListener() 		{			@Override			public void onClick(View arg0)			{				if (bFolder(strRingtoneFolder))				{					//打开系统铃声设置					Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);					//类型为来电RINGTONE					intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_RINGTONE);					//设置显示的title					intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "设置来电铃声");					//当设置完成之后返回到当前的Activity					startActivityForResult(intent, ButtonRingtone);				}			}		});		/* 设置闹钟铃声 */		mButtonAlarm.setOnClickListener(new Button.OnClickListener() 		{			@Override			public void onClick(View arg0)			{				if (bFolder(strAlarmFolder))				{					//打开系统铃声设置					Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);					//设置铃声类型和title					intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_ALARM);					intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "设置闹铃铃声");					//当设置完成之后返回到当前的Activity					startActivityForResult(intent, ButtonAlarm);				}			}		});		/* 设置通知铃声 */		mButtonNotification.setOnClickListener(new Button.OnClickListener() 		{			@Override			public void onClick(View arg0)			{				if (bFolder(strNotificationFolder))				{					//打开系统铃声设置					Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);					//设置铃声类型和title					intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION);					intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "设置通知铃声");					//当设置完成之后返回到当前的Activity					startActivityForResult(intent, ButtonNotification);				}			}		});	}	/* 当设置铃声之后的回调函数 */	@Override	protected void onActivityResult(int requestCode, int resultCode, Intent data)	{		// TODO Auto-generated method stub		if (resultCode != RESULT_OK)		{			return;		}		switch (requestCode)		{			case ButtonRingtone:				try				{					//得到我们选择的铃声					Uri pickedUri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);					//将我们选择的铃声设置成为默认					if (pickedUri != null)					{						RingtoneManager.setActualDefaultRingtoneUri(Activity01.this, RingtoneManager.TYPE_RINGTONE, pickedUri);					}				}				catch (Exception e)				{				}				break;			case ButtonAlarm:				try				{					//得到我们选择的铃声					Uri pickedUri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);					//将我们选择的铃声设置成为默认					if (pickedUri != null)					{						RingtoneManager.setActualDefaultRingtoneUri(Activity01.this, RingtoneManager.TYPE_ALARM, pickedUri);					}				}				catch (Exception e)				{				}				break;			case ButtonNotification:				try				{					//得到我们选择的铃声					Uri pickedUri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);					//将我们选择的铃声设置成为默认					if (pickedUri != null)					{						RingtoneManager.setActualDefaultRingtoneUri(Activity01.this, RingtoneManager.TYPE_NOTIFICATION, pickedUri);					}				}				catch (Exception e)				{				}				break;		}		super.onActivityResult(requestCode, resultCode, data);	}	//检测是否存在指定的文件夹 	//如果不存在则创建	private boolean bFolder(String strFolder)	{		boolean btmp = false;		File f = new File(strFolder);		if (!f.exists())		{			if (f.mkdirs())			{				btmp = true;			}			else			{				btmp = false;			}		}		else		{			btmp = true;		}		return btmp;	}} 铃声的播放:
player = new MediaPlayer();	      try{	      player.setDataSource(context, pickUri);	      }catch(Exception e){	    	  e.printStackTrace();	      }	     final AudioManager  audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);	     if(audioManager.getStreamVolume(AudioManager.STREAM_ALARM)!=0){         player.setAudioStreamType(AudioManager.STREAM_ALARM);         player.setLooping(true);         try {			player.prepare();		} catch (Exception e) {			// TODO Auto-generated catch block			e.printStackTrace();		}         player.start();    	}
 

转载地址:http://ycjax.baihongyu.com/

你可能感兴趣的文章
为数据库建立索引
查看>>
第二周作业-软件工作量的估计
查看>>
我的wordpress插件总结
查看>>
MAXIMO 快速查找实现
查看>>
Oracle——条件控制语句
查看>>
[Linux][Redis][05]Benchmark
查看>>
第一次作业-准备篇
查看>>
HDU1848 Fibonacci again and again
查看>>
HTML思维导图
查看>>
office2016选择性安装
查看>>
C# 自定义控件入门
查看>>
git改密码出现授权问题
查看>>
Hadoop IO 特性详解(2)
查看>>
ORA-02266: 表中的唯一/主键被启用的外键引用
查看>>
5(绪论).时间复杂度和空间复杂度-3
查看>>
MySQL类型转换 使用CAST将varchar转换成int类型排序
查看>>
Django的POST请求时因为开启防止csrf,报403错误,及四种解决方法
查看>>
Apache common-fileupload用户指南
查看>>
day-6 and day-7:面向对象
查看>>
COM基础
查看>>