android– 使用Intent启动Media Player

android– 使用Intent启动Media Player,第1张

概述我正在开发我的第一个Android应用程序.这是录音应用程序.我正在使用MediaRecord录制语音,如下所示:mRecorder=newMediaRecorder();mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GP

我正在开发我的第一个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();

我有另一个活动来播放这些录制的声音(.3gpp文件).在此活动中,有一个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因为它需要min API lvl 15.我的项目的最低API级别应为8.

解决方法:

我认为最好的选择是使用选择器活动,用户可以选择他喜欢的媒体播放器.

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启动Media Player全部内容,希望文章能够帮你解决android – 使用Intent启动Media Player所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1113157.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-29
下一篇2022-05-29

发表评论

登录后才能评论

评论列表(0条)

    保存