Android 使用MediaPlayer播放assets或者raw目录的音频文件
生活随笔
收集整理的這篇文章主要介紹了
Android 使用MediaPlayer播放assets或者raw目录的音频文件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
不廢話,需求:當接收到推送消息時,播放apk自帶的聲音,怎么辦?
?
首先接入極光推送,我前幾篇文章已經講了,不會的自己去看。在MyReceiver里的public void onReceive(Context context, Intent intent)判斷消息類型,然后使用MediaPlayer播放assets或者raw目錄的音頻文件。
if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {Log.e(TAG, "[MyReceiver] onReceive - " + intent.getAction() + ", extras: " + printBundle(bundle));Log.e(TAG, "[MyReceiver] 接收到推送下來的通知");int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);Log.e(TAG, "[MyReceiver] 接收到推送下來的通知的ID: " + notifactionId);openRawMusicS(context);} /*** 打開raw目錄下的音樂mp3文件*/private void openRawMusicS(Context context) { // mediaPlayer1 = MediaPlayer.create(this, R.raw.please_get_close);mediaPlayer1 = MediaPlayer.create(context, R.raw.weige);//用prepare方法,會報錯誤java.lang.IllegalStateExceptio//mediaPlayer1.prepare();mediaPlayer1.start();}?
還有使用MediaPlayer播放assets目錄下的媒體文件:
/*** 打開assets下的音樂mp3文件*/private void openAssetMusics() {try {//播放 assets/a2.mp3 音樂文件AssetFileDescriptor fd = getAssets().openFd("identification_success.mp3");mediaPlayer = new MediaPlayer();mediaPlayer.setDataSource(fd.getFileDescriptor(), fd.getStartOffset(), fd.getLength());mediaPlayer.prepare();mediaPlayer.start();} catch (IOException e) {e.printStackTrace();}}?
?
?
?
?
?
總結
以上是生活随笔為你收集整理的Android 使用MediaPlayer播放assets或者raw目录的音频文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android 省,市,区选择权
- 下一篇: Android 极光推送设置别名