Streaming audio file and caching it - android

I want to stream an audio mp3 file and then play it through android media player plus I also want to cache this file, so that mediaplayer don't have to stream for recently played tracks.
I have tried using prepareAsync method but it doesn't give me access to buffer content, so I have decided to stream the audio file myself and then pass it to the media player for playing. I have achieved this by following this article here but this approach has a problem i.e. while transferring the file to media player it goes into error mode which causes my player to behave inconsistently.
When media player enters its error mode it doesn't come out of it automatically so I am forced to create a new media player and then re-provide it the downloaded file, this workaround causes the user to experience an undesired pause in the song playing.
So, does any one have improved an version of code given in above link? or do they know a better solution to this problem or is there is actually a library for streaming an audio file in android?
Thanks

The link you provided looks like a less than ideal solution (not to mention outdated). What you probably want is a local proxy server that gives you access to byte data before the MediaPlayer gets it. See my answer here for a little more explanation.

Related

ExoPlayer playing currently recording media files

Let me refraise my question, I wrote it in a hurry.
Current situation:
I have set up a digital video recorder to record broadcasts provided via DVB-C. It is running on a raspberry 3B using TVHeadend and jetty/cling to provide UPnP and other possibilities to access media files. For watching recordings, I wrote an android player app using IJKPlayer, which runs on smartphones, FireTV and AndroidTV.
One hassle when playing media files which are currently beeing recorded is, that IJKPlayer doesn not support timeshifting. Means, when I start playing a currently recording file, I can only watch the length which is known by the player at that moment. Anything which is recorded afterwards can not be played. I need to exit the player activity and start it again. I have resolved that issue by "simulating" a completed recoding using a custom servlet implementation. Since the complete length of the recording is already known, I can use ffmpeg to accomplish this.
Future situation:
I plan to move away from IJKPlayer to ExoPlayer, because it supports hardware playback and is much faster when playing h.264 media. I can of course use the same solution like above, but as far as I have found out yet, ExoPlayer can support media files which are currently being recorded by using the Timeline class. However, I don't seem to find neither a usefull documentation nor any good example. Hence, I would appreciate any help with the timeline object.
Regards
Harry
Looks like my approach won't work. At least, I didn't find a solution. Problem is, that the server returns the stream size as it is during player-start-time. I didn't find a method to update the media duration for "regular" files.
However, I can solve the problem by changing the server side. Instead of accessing a regular file, I convert the file to m3u8 in realtime, using ffmpeg. I then throw the m3u8 URI onto the player and it updates the duration of the stream (while playing) without the need to create any additional code on the client side.

Android ExoPlayer changing selected track

I am using Andorid ExoPlayer to stream content from internet through http(HLS). When I start the player it is working ok, but when I try to change the quality of the playing content -
player.setSelectedTrack(TYPE_VIDEO, 1)
for example, I get and HTTP 403 Forbidden error. If I initialize the player and run again the content, it is fine. I am using the Demo project as a source.
Do you know what might be causing this behavior and what is the difference in playing the initial stream and changing the track?
Is there a way to reset the streaming without re-initializing the whole player, because the url is passed to the builder when the player is initializing?
I found the problem. The URL i was passing to the ExoPlayer was returned from an external API and was rather long so I haven't noticed that it is actually 2 URLs concatenated by "|"(http://my_url_1.m3u8|http://my_url_2.m3u8). The strange thing is that if you pass this string to the ExoPlayer it is playing a stream without error.. but if you try to change the quality of the playing stream you have problems.
You do not change quality of the stream using setSelectedTrack(). That is used for selecting what to play from the available streams (like what language of audio, subtitle, or for videos it is quite rare, but for example you can set up different camera angles of a sport event).
All of these streams can have multiple quality levels, and ExoPlayer's FormatEvaluator selects what quality to download based on the network conditions.
If the decoder is different when you select a new track, then the player needs to be reinitialized for continuing playback.
I suggest downloading the HLS manifest (.m3u8) manually, and then check the listed information, try the urls one by one in a browser. All should work, you should not get 403 test in this test either.

Live mp3 stream playing by Media Player without buffering

My question is about media player live streaming from the url in my application?
I'm trying to play live stream mp3 audio by media player.
The problem is that when i am using default URL as given in this link , then it works fine. But i changed the following mp3 link ,
then after playing 1 seconds it taking buffering and after completion its play the song, but i need that it plays the song with the buffering as well.
please help.
Thanks in advance.
It is not easy task to accomplish.
You can use this link https://code.google.com/p/mp3tunes/source/browse/
as reference app to develop your own progressive mediaplayer app.
From the above link you will get the whole architecture for your app.Song downloading and playing is happens at the same time.The architecture developed in the reference app is more robust and reusable.
Specially look at following classes
NanoHTTD.java
HttpServer.java
PlaybackService.java
PlaybackQueue.java
TrackDownloader.java
Thank You.

Alternative to MediaPlayer to play an audio file

Is there an alternative way of playing audio data other than using MediaPlayer. The audio comes from a resource and not a file. I also want it played from a service and not from an activity or using a built-in music player app. What alternative APIs exist? I am coming to the conclusion after much testing that MediaPlayer in Android 4.1 is buggy and fails much more than under previous versions, so I am looking for an alternative means of playing audio.
I think the AudioTrack is the best thing to use here. as you are looking to play from resource then you can use raw pcm audio data as resource.

Short audio files

I'm trying to play a couple of short audio files (ca. 2sec) in an Android App with the Media Player. They do play, but for some of them I get a click sound at the end (which differs from file to file).
There are no click-sounds when I play it on the PC, also I tried to convert it from .mp3 in .ogg and modified the soundfiles themselves, nothing helps, I think the problem is probably the Media PLayer.
For short audio files, like sound effects, I would use a SoundPool
Also see this question and answer here: The correct way to play short sounds Android?

Categories

Resources