如何播放不在URL或存储中的音频文件的InputStream? [英] How to play InputStream of an audio file that's not within a url or storage?

查看:322
本文介绍了如何播放不在URL或存储中的音频文件的InputStream?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已成功将音频文件(3gp)上传到Google-Drive.

I've succeeded uploading an audio file (3gp) into Google-Drive.

现在,我希望能够在应用程序中播放文件.

Now I want to be able to play the file within the app.

Google Drive API仅允许获取存储在其中的文件的输入流.

The Google Drive API only allows to get the input stream of the file that's stored there.

在我的案例中,输入的所有MediaPlayer功能均不可用,只有InputSteam:

All MediaPlayer capabilities of inputs aren't available in my case, which is only InputSteam:

http://developer. android.com/reference/android/media/MediaPlayer.html#setDataSource(java.io.FileDescriptor)

我知道我可以将文件从Google驱动器保存到缓存中并播放,但是我想避免存储操作,而是即时播放文件.

I know I can save the file from the Google-Drive to the cache and play it, but I want to avoid the storage handling, and play the file on the fly.

我尝试搜索此问题,但仅发现使用AudioTrack可以实现此操作( 此处 ).也可以使用新的Jelly-Bean功能(显示在 此处 ,可从 此处 找到),但我不确定,因为它太低了级别.

I tried to search for this issue, and only found that it might be possible using AudioTrack (here). It might also be possible using new Jelly-Bean features (shown here, found from here), but I'm not sure as it's quite low level.

可悲的是,使用AudioTrack时,我播放了错误的声音(噪音).

Sadly, using AudioTrack I got wrong sounds being played (noise).

我还注意到MediaPlayer可以选择将dataSource设置为MediaDataSource(

I've also noticed that MediaPlayer has the option to set the dataSource to be MediaDataSource (here) , but not only I'm not sure how to use it, it also requires API 23 and above.

当然,我尝试使用Google-Drive中提供的网址,但这仅用于其他目的,并且不会定向到音频文件,因此无法使用MediaPlayer使用.

Of course, I tried using a url that is given in Google-Drive, but this is only used for other purposes and isn't being directed to the audio file, so it can't be used using MediaPlayer.

给出InputStream,是否可以使用AudioTrack或其他方式来播放3gp音频文件?

Given an InputStream, is it possible to use AudioTrack or something else, to play an audio 3gp file ?

也许为此有一个支持库解决方案吗?

Is there maybe a support library solution for this?

推荐答案

如果您的minSdkVersion为23或更高,则可以

If your minSdkVersion is 23 or higher, you can use setDataSource(MediaDataSource) and supply your own subclass of the abstract MediaDataSource class.

对于较旧的设备,您应该能够使用从ParcelFileDescriptor创建的管道.您将拥有一个将数据写入管道末端的线程,并将播放器末端的FileDescriptor(来自getFileDescriptor())传递到setDataSource(FileDescriptor).

For older devices, you should be able to use a pipe created from ParcelFileDescriptor. You would have a thread that writes data to your end of the pipe, and pass the FileDescriptor (from getFileDescriptor()) for the player's end to setDataSource(FileDescriptor).

这篇关于如何播放不在URL或存储中的音频文件的InputStream?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆