Android震动和播放资源文件中的声音文件
public class SoundManage {
//获取声音文件
public static FileDescriptor getAssetsFile(Context context, String filename){
AssetFileDescriptor assetFileDescriptor = null;
try {
assetFileDescriptor = context.getResources().getAssets().openFd(filename);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(assetFileDescriptor!=null)
{
return assetFileDescriptor.getFileDescriptor();
}else{
return null;
}
}
//播放声音
public static void playSound(FileDescriptor file){
MediaPlayer mp = new MediaPlayer();
try {
mp.setDataSource(file);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
mp.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mp.start();
mp.setOnSeekCompleteListener(new OnSeekCompleteListener() {
@Override
public void onSeekComplete(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.release();
}
});
}
//产生震动
public static void playVibator(Context context, long timelong){
Vibrator vib = (Vibrator)context.getSystemService (Service.VIBRATOR_SERVICE);
vib.vibrate(timelong);
}
}
更多相关文章
- Android Settings 声音设置
- Android两种播放声音的方式
- android点击按钮发出声音
- Android7.0 设置音量最小时有声音输出
- RK3288 android 5.1 HDMI 喇叭同时输出声音
- android添加开机声音
- Android耳机声音自动调整
- Android播放ogg声音文件