当前位置: 代码迷 >> Android >> Android震动跟播放资源文件中的声音文件
  详细解决方案

Android震动跟播放资源文件中的声音文件

热度:48   发布时间:2016-05-01 17:01:13.0
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);
}

}

  相关解决方案