在后台播放 m4v 视频文件,即仅播放声音

发布于 2024-12-12 04:26:18 字数 1202 浏览 0 评论 0原文

我正在尝试在后台单独播放 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

懵少女 2024-12-19 04:26:18

首先看一下支持的媒体格式页面。我还没有看到 m4v 格式,但如果它适用于您,请参见下文。

我不知道如何使用 SoundPool 但如果您想使用它,我建议您使用编码器从视频中推断出支持的音频格式。

最后我可以建议您使用 VideoView 的解决方案(因为您说视频有效)。在你的 main.xml 中,将此代码引用到你的 VideoView

<VideoView
   android:id="@+id/video"
   android:layout_width="0.1px"
   android:layout_height="0.1px">
</VideoView>

并在 Activity 的 onCreate 方法中使用此代码:

String uri = "android.resource://" + getPackageName() + "/" + R.raw.videoname;
VideoView video = (VideoView) findViewById(R.id.video);
video.setVideoURI(Uri.parse(uri));
video.start();

我自己尝试过,它有效。我知道这是一种在后台获取音频的方式,但如果您没有任何其他解决方案,我认为这已经足够了。

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

<VideoView
   android:id="@+id/video"
   android:layout_width="0.1px"
   android:layout_height="0.1px">
</VideoView>

And use this code in your onCreate method of your Activity:

String uri = "android.resource://" + getPackageName() + "/" + R.raw.videoname;
VideoView video = (VideoView) findViewById(R.id.video);
video.setVideoURI(Uri.parse(uri));
video.start();

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文