Stream an audio .pls in android

2019-02-14 10:22发布

问题:

I'm making an radio like application that uses streaming. Here i need to stream the audio from a link (http://somedomain/some.pls).

I have created MediaPlayer and know how to play from an audio file. But don't know how to stream it from net.

EDIT : logcat i got while i used the following code

MediaPlayer mp = new MediaPlayer();  
mp.setDataSource(http://somedomain/some.pls);
mp.prepare();
mp.start()

By log tag **MediaPlayer**

  02-15 05:50:11.761: VERBOSE/MediaPlayer(23715): constructor
    02-15 05:50:11.761: VERBOSE/MediaPlayer(23715): setListener
    02-15 05:50:11.761: INFO/MediaPlayer(23715): uri is:http://some:444/sdfd.pls
    02-15 05:50:11.761: INFO/MediaPlayer(23715): path is null
    02-15 05:50:11.761: DEBUG/MediaPlayer(23715): Couldn't open file on client side, trying server side
    02-15 05:50:11.765: VERBOSE/MediaPlayer(23715): setDataSource(http://some:444/sdfd.pls)
    02-15 05:50:11.777: VERBOSE/MediaPlayer(23715): prepare
    02-15 05:50:13.105: ERROR/MediaPlayer(23715): message received msg=3, ext1=0, ext2=0
    02-15 05:50:13.105: VERBOSE/MediaPlayer(23715): buffering 0
    02-15 05:50:13.105: ERROR/MediaPlayer(23715): callback application
    02-15 05:50:13.105: ERROR/MediaPlayer(23715): back from callback

By log tag **PlayerDriver**

02-15 05:50:11.765: VERBOSE/PlayerDriver(2393): constructor
02-15 05:50:11.765: VERBOSE/PlayerDriver(2393): OpenCore hardware module not found
02-15 05:50:11.765: VERBOSE/PlayerDriver(2393): start player thread
02-15 05:50:11.765: VERBOSE/PlayerDriver(2393): startPlayerThread
02-15 05:50:11.769: VERBOSE/PlayerDriver(2393): InitializeForThread
02-15 05:50:11.769: VERBOSE/PlayerDriver(2393): OMX_MasterInit
02-15 05:50:11.769: VERBOSE/PlayerDriver(2393): OsclScheduler::Init
02-15 05:50:11.769: VERBOSE/PlayerDriver(2393): CreatePlayer
02-15 05:50:11.777: VERBOSE/PlayerDriver(2393): AddToScheduler
02-15 05:50:11.777: VERBOSE/PlayerDriver(2393): PendForExec
02-15 05:50:11.777: VERBOSE/PlayerDriver(2393): OsclActiveScheduler::Current
02-15 05:50:11.777: VERBOSE/PlayerDriver(2393): StartScheduler
02-15 05:50:11.777: VERBOSE/PlayerDriver(2393): Send player code: 2
02-15 05:50:11.777: VERBOSE/PlayerDriver(2393): CommandCompleted
02-15 05:50:11.777: VERBOSE/PlayerDriver(2393): Completed command PLAYER_SETUP status=1
02-15 05:50:11.777: INFO/PlayerDriver(2393): PlayerDriver::isProtectedFile(http://some:444/sdfd.pls)
02-15 05:50:11.777: VERBOSE/PlayerDriver(2393): File EXT is : .pls
02-15 05:50:11.777: ERROR/PlayerDriver(2393): PlayerDriver::it is a not Protected file
02-15 05:50:11.781: VERBOSE/PlayerDriver(2393): Send player code: 3
02-15 05:50:11.781: VERBOSE/PlayerDriver(2393): handleSetDataSource
02-15 05:50:11.781: VERBOSE/PlayerDriver(2393): handleSetDataSource- scanning for extension
02-15 05:50:11.800: VERBOSE/PlayerDriver(2393): CommandCompleted
02-15 05:50:11.800: VERBOSE/PlayerDriver(2393): Completed command PLAYER_SET_DATA_SOURCE status=1
02-15 05:50:11.800: VERBOSE/PlayerDriver(2393): Send player code: 6
02-15 05:50:11.800: VERBOSE/PlayerDriver(2393): release string is 2.2 len 3
02-15 05:50:13.101: VERBOSE/PlayerDriver(2393): HandleInformationalEvent: 34
02-15 05:50:13.101: VERBOSE/PlayerDriver(2393): HandleInformationalEvent: 19
02-15 05:50:13.124: VERBOSE/PlayerDriver(2393): HandleInformationalEvent: 20
02-15 05:50:14.511: VERBOSE/PlayerDriver(2393): HandleInformationalEvent: 20
02-15 05:50:16.509: VERBOSE/PlayerDriver(2393): HandleInformationalEvent: 20

This 02-15 05:50:16.509: VERBOSE/PlayerDriver(2393): HandleInformationalEvent: 20 loops but no sound ..
What may be wrong..
Thank you.

回答1:


You can use the file streaming ability available on the MediaPlayer class to do the above.
I have not tested the following code, but it should be something on these lines:

MediaPlayer mp = new MediaPlayer();  
mp.setDataSource(http://somedomain/some.pls);
mp.prepare();
mp.start();  

But remember to do the following:
1. Surround each of the statements above with try and catch statements and print copiously to LogCat. It will help debug easily.
2. Release the resources once you are done with it.

HTH,
Sriram



回答2:

Mediaplayer api from android supports streaming a .pls file, but the API is not best option & the documentation is not well. The life cycle diagram given in the official documentation gives valuable information but may be confusing at a first glance.

A sample code snippet:

MediaPlayer mp;
mp=MediaPlayer.create(getApplicationContext(),Uri.parse(url))
Example url of  .pls file http://50.xx.xxx.xx:xx40/)

mp.start();
mp.pause();
mp.release() (or mp.reset() as applicable)

http://developer.android.com/reference/android/media/MediaPlayer.html#create(android.content.Context, android.net.Uri)

There are listeners/callback available with MediaPlayer api but it is really problematic to a App developer working on audio streaming.

The static constructor used in the code snippet is suggested/better approach to create the media object with URI (like http url with host and port.) But the call back functions can't be used by developer because the prepare is called by the constructor itself.

The created object can be played/stopped in a asyn thread (AsyncTask api).

The android official documentation does not give any 'get method' to get the status of the mediaplayer.

I welcome android app developers to talk on this;

and I wish frame work developers provide more accurate documentation - how much it supports or what's importance & vision of Google Inc and Android framework development team on Mediaplayer API for audio streaming with urls.

If some need help from me or share their experience let's please talk on MediaPlayer api for audio streaming @ http://stackoverflow.com or android developers blog (android-developers.blogspot.com).

Regards Sree Ramakrishna Program Manager & Android developer @ New Mek Solutions,Hyderabad.



回答3:

Man, try this two things:

1: the code

mediaPlayer = MediaPlayer.create(this, Uri.parse("http://vprbbc.streamguys.net:80/vprbbc24.mp3"));
mediaPlayer.start();

2: the .pls file

This URL is from BBC just as an example. It was an .pls file that on linux i downloaded with

wget http://foo.bar/file.pls

and then i opened with vim (use your favorite editor ;) and i've seen the real URLs inside this file. Unfortunately not all of the .pls are plain text like that.

have fun!



回答4:

You can try download pls file, parse the urls that are in there, and put them in MediaPlayer.