在默认音乐播放器中播放原始音频文件

时间:2019-11-05 14:38:41

标签: android android-intent android-music-player

我的应用程序的原始文件夹中有音频文件。现在,只要启动应用,这些音频文件都应使用默认的音乐播放器播放。我在下面的代码中尝试App崩溃,并显示“找不到用于处理此意图的活动”。

 Intent intent = new Intent();
 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
 intent.setAction(Intent.ACTION_VIEW);    
 intent.setDataAndType(Uri.parse("companion.harman.com.ceshuclient/res/raw/fav.mp3"), "audio/*");
 startActivity(intent);

可以使用“ MediaPlayer”对象播放相同的音频文件,但这并不是我要达到的目的。

任何示例代码或链接都将有所帮助。

谢谢。

1 个答案:

答案 0 :(得分:1)

请尝试以下操作(其中mp3file是原始文件夹中文件的名称,不带扩展名。请仅使用小写字符):

MediaPlayer player = MediaPlayer(this,R.raw.mp3file);

player.prepare();
player.start();

player.stop();
player.reset();
player.release();
player = null;

这是演示源代码到play mp3 File

的链接