在后台播放 m4v 视频文件,即仅播放声音
我正在尝试在后台单独播放 m4v 文件音频。
我尝试使用 SoundPool,但从原始文件夹加载它时出现错误。
我使用了这个示例程序。
10-27 11:49:11.684: ERROR/AndroidRuntime(1484): Caused by: android.content.res.Resources$NotFoundException: File Hello World, AudioPlayerActivity! from drawable resource ID #0x7f040000
10-27 11:49:11.684: ERROR/AndroidRuntime(1484): at android.content.res.Resources.openRawResourceFd(Resources.java:860)
10-27 11:49:11.684: ERROR/AndroidRuntime(1484): at android.media.SoundPool.load(SoundPool.java:204)
10-27 11:49:11.684: ERROR/AndroidRuntime(1484): at com.sample.audio.AudioPlayerActivity.onCreate(AudioPlayerActivity.java:31)
10-27 11:49:11.684: ERROR/AndroidRuntime(1484): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1069)
10-27 11:49:11.684: ERROR/AndroidRuntime(1484): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2751)
10-27 11:49:11.684: ERROR/AndroidRuntime(1484): ... 11 more
但我可以使用 VideoView 播放文件,但我只需要音频。
我不知道如何在声音池中播放 m4v 文件或我需要在后台播放的某种方式。 如何实现这一目标?
提前致谢。
Im trying to play a m4v files audio alone in the background.
I tried using SoundPool
and it is giving me error while loading it from the raw folder.
i used this sample program.
10-27 11:49:11.684: ERROR/AndroidRuntime(1484): Caused by: android.content.res.Resources$NotFoundException: File Hello World, AudioPlayerActivity! from drawable resource ID #0x7f040000
10-27 11:49:11.684: ERROR/AndroidRuntime(1484): at android.content.res.Resources.openRawResourceFd(Resources.java:860)
10-27 11:49:11.684: ERROR/AndroidRuntime(1484): at android.media.SoundPool.load(SoundPool.java:204)
10-27 11:49:11.684: ERROR/AndroidRuntime(1484): at com.sample.audio.AudioPlayerActivity.onCreate(AudioPlayerActivity.java:31)
10-27 11:49:11.684: ERROR/AndroidRuntime(1484): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1069)
10-27 11:49:11.684: ERROR/AndroidRuntime(1484): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2751)
10-27 11:49:11.684: ERROR/AndroidRuntime(1484): ... 11 more
But i can able to play the file using VideoView
but i need only audio.
I dont know how to play the m4v file in the soundpool or some way i need to play in the background.
How to achieve that?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先看一下支持的媒体格式页面。我还没有看到 m4v 格式,但如果它适用于您,请参见下文。
我不知道如何使用
SoundPool
但如果您想使用它,我建议您使用编码器从视频中推断出支持的音频格式。最后我可以建议您使用 VideoView 的解决方案(因为您说视频有效)。在你的 main.xml 中,将此代码引用到你的
VideoView
并在 Activity 的
onCreate
方法中使用此代码:我自己尝试过,它有效。我知道这是一种在后台获取音频的坏方式,但如果您没有任何其他解决方案,我认为这已经足够了。
First of all take a look at supported media formats page. I've not see m4v format but if it works with you see below.
I don't know how to use
SoundPool
but if you want to use it I suggest you to extrapolate a supported audio format from your video with an encoder.Finally I can suggest you a solution with VideoView (since you say that the video works). In your main.xml put this to refer your
VideoView
And use this code in your
onCreate
method of your Activity:I've tried it myself and it works. I know that this is a bad way to get the audio in background but if you haven't any other solution I think that this is sufficient.