gpt4 book ai didi

android - 使用 Intent 启动媒体播放器

转载 作者:太空宇宙 更新时间:2023-11-03 12:20:38 25 4
gpt4 key购买 nike

我正在开发我的第一个 Android 应用。这是录音应用程序。我正在用 MediaRecord 像这样录制语音:

mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setOutputFile(mFileName);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

try {
mRecorder.prepare();
} catch (IOException e) {
Log.e(LOG_TAG, "prepare() failed");
}

mRecorder.start();

我还有另一个 Activity 来播放这些录制的声音(.3gpp 文件)。在此 Activity 中,有一个包含我录制的语音的 ListView。我想用手机上安装的任何音乐播放器播放声音。这是我的代码:(此代码的来源:https://stackoverflow.com/a/3367231/556169)

Intent intent = new Intent();  
intent.setAction(android.content.Intent.ACTION_VIEW);
File file = new File((String) ((TextView) item).getText());
intent.setDataAndType(Uri.fromFile(file), "audio/*");
startActivity(intent);

但我收到“音乐播放器无法播放此媒体类型”错误。

当我浏览此音频文件并通过我的文件资源管理器播放它们时,它运行良好(这意味着,我正在成功录制声音)。但是当我在我的应用程序中使用 Intent 时,我遇到了错误。

额外我无法使用 MediaStore.INTENT_ACTION_MUSIC_PLAYER,因为它已被弃用。我不能使用 Intent.CATEGORY_APP_MUSIC,因为它需要最低 API lvl 15。我的项目的最低 API 级别应该是 8。

最佳答案

我认为最好的选择是使用选择器 Activity ,用户可以在其中选择他最喜欢的媒体播放器。

Intent viewIntent = new Intent(Intent.ACTION_VIEW);
File file = new File((String) ((TextView) item).getText());
viewIntent.setDataAndType(Uri.fromFile(file), "audio/*");
startActivity(Intent.createChooser(viewIntent, null));

顺便说一句,我觉得这种传递文件名的方式有点奇怪。我会考虑改变它。

我不确定,但如果您声明相应的 intent-filters,您应该能够在选择器中看到您自己的播放器。

关于android - 使用 Intent 启动媒体播放器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17066481/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com