Prepare Failed on Mediaplayer when trying to stream - android

I am trying to stream an MP3-file with the following code in Android 2.3.3:
player = new MediaPlayer();
String uri = "http://192.168.2.102:8080/sound/test.mp3";
player.setDataSource(uri);
player.prepare();
for some reason every time I call prepare I get the following IOException: java.io.IOException: Prepare failed.: status=0x1
I tested the link directly in a browser and it works.
I also added the Internet permission in the manifest.
Anyone know what I am doing wrong?
thanks for the help

It seems like the problem was that there was no ID3 tag attached to the MP3 file I was trying to stream, and Android doesn't like that...

Related

Android Visualizer won't pick up audio from Spotify streaming music

I'm making an Android audio visualizer app for streaming Spotify music. I'm using Spotify's android-streaming-sdk which creates a local service (no need to have the Spotify app installed) and it plays music from within the app. I'm having a difficult time getting Androids Visualizer library to pick up any audio from Spotify (it works fine if I use a local .mp3 file).
//Start playing spotify track
player.playUri(MainActivity.operationCallback,
VisualizerModel.getInstance().getTrackURI(), 0, 0);
.... other code ....
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
int audioSessionId = mediaPlayer.getAudioSessionId();
visualizer = new Visualizer(audioSessionId);
visualizer.setCaptureSize(audioSampleSize);
visualizer.setDataCaptureListener(this, Visualizer.getMaxCaptureRate(), true, true);
visualizer.setEnabled(true);
If I run this, everything compiles and runs okay, but the visualizer does not pick up any audio. This makes me thing that the audio sessionId is not the same one as Spotify is playing on. However, if I switch the audioSessionId to 0 (which should listen to all the audio sessions mixed from what I understand) it crashes with this error:
E/AudioEffect: set(): AudioFlinger could not create effect, status: -1
E/visualizers-JNI: Visualizer initCheck failed -3
E/Visualizer-JAVA: Error code -3 when initializing Visualizer.
According to their documentation that error means: Operation failed due to bad object initialization.
I'm targetting API 24 (Android 7.0).
Please let me know if you have suggestions or an alternative to using Android's Visualizer library. I feel like my main problem is that I'm not sure how to find the audioSessionId that Spotify is playing on, and because Spotify's android-streaming-sdk is in beta (and not mentioned on their website) there is virtually no documentation on it from what I can see on Github.
Thank you for taking the time to read my issue!
I was facing the same error. Adding "android.permission.RECORD_AUDIO" to manifest and requesting for runtime permissions fixed the issue.
to protect privacy of certain audio data (e.g voice mail) the use of the visualizer requires the permission android.permission.RECORD_AUDIO
Also
Creating a Visualizer on the output mix (audio session 0) requires permission Manifest.permission.MODIFY_AUDIO_SETTINGS
Android docs for Visualizer class

MediaPlayer playing in background throws unexpected end of stream

We are developing an app that plays podcasts, we are using services and the media player to play the files that are in the server, in some Marshmallow devices we are encountering this error (Motorola g4):
W/MediaHTTPConnection: readAt 22575768 / 32768 => java.net.ProtocolException: unexpected end of stream
But it only happens when the app is killed and the service starts to run, when the app is in foreground it works well, we have noticed that the buffering is working well, but when the app is killed it cannot buffer more, it recahes a limit and then it throws that error... In other devices the buffering is working well.
Here is the chunk of the code that prepares the media player.
Map<String, String> headers = new HashMap<>();
headers.put("Content-Type", "audio/mp3");
headers.put("Accept-Ranges", "bytes");
headers.put("Status", "206");
mp = new MediaPlayer();
Uri uri = Uri.parse(songPath);
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.setDataSource(getApplicationContext(), uri, headers);
mp.prepareAsync();
The headers were added because a post we saw, but it has the same result as if it doesn't have it.
Any advice?
Playing audio in background is a bit more complex than that. If you're just using a MediaPlayer, then that component's container (i.e. Activity) or the open http connection are probably being destroyed when Android decides to release resources. This can happen as soon as you switch to another app, or when the device (some other app) is using too many resources, or when the operating system decides it's a good time to do it.
However, MediaPlayer is just one part of the background audio playing.
I'd advice you to take a look at the oficial guide on how to build an Audio app.
https://developer.android.com/guide/topics/media-apps/audio-app/building-an-audio-app.html
I also found a well-explained step-by-step tutorial that can also help
https://code.tutsplus.com/tutorials/background-audio-in-android-with-mediasessioncompat--cms-27030

MediaPlayer not working on some android devices

I am trying to play the mp3 file from web using media player class , on some devices it is working fine but for few devices it is giving following error .
QCMediaPlayer mediaplayer NOT present
Unable to create media player
error (-38, 0)
Attempt to call getDuration without a valid mediaplayer
any help on this will be of great help.
In general error -38 means you are trying to call a function in wrong state for the player.
In your case as error says, you call getDuration() when player is not prepared or started. Set setOnPreparedListener() then start the player mp.start(), then you can call getDuration().
Check this Tutorial to stream audio from web

how to read a sound file (mp3)

how to open and read a sound file ?
My purpose is when click on Button read music open and I listen this file
I do not know how to open an mp3 file and read this file contenut
how, which the permission is there a tutorial and thank you in advance
If I understand you correctly you just want to play a local MP3 file. Correct?
You should read the official API guides. It will tell you everything you need to know!
Here are a few snippets from the guide:
How you might play from local storage:
Uri myUri = ....; // initialize Uri here
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(getApplicationContext(), myUri);
mediaPlayer.prepare();
mediaPlayer.start();
What permissions do you need?
Internet Permission - If you are using MediaPlayer to stream network-based content, your application must request network access.
Wake Lock Permission - If your player application needs to keep the screen from dimming or the processor from sleeping, or uses the MediaPlayer.setScreenOnWhilePlaying() or MediaPlayer.setWakeMode() methods, you must request this permission.

Streaming internet radio error android

I am trying out to play internet radio with an example downloaded from
http://www.4shared.com/zip/OTcJZZft/StreamingRadio.html
which works kinda like given in this example Online radio streaming app for Android, but the problem is my logcat keeps throwing this error:
11-07 17:20:39.207: E/MediaPlayer(335): error (1, -1002)
11-07 17:20:39.237: E/MediaPlayer(335): Error (1,-1002)
11-07 17:20:48.882: W/KeyCharacterMap(335): No keyboard for id 0
11-07 17:20:48.886: W/KeyCharacterMap(335): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
I am unable to understand what this means. Can someone please explain what it means and also if someone can guide me to a fresh tutorial for internet radio streaming it would be great help.
Thanks.
MediaPlayer is throwing that error (1, -1002) in response to the URL you passed it in the setDataSource(String) method. Set up an onErrorListener() for your MediaPlayer object and catch the error thrown by the player. (Quick look at the documentation - http://developer.android.com/reference/android/media/MediaPlayer.OnErrorListener.html) You can evaluate the error by checking the code thrown via the logcat or a Toast object and take appropriate action based on that.
In my experience with playing internet radio I've found that most often, that or similiar errors thrown by the MediaPlayer are caused by the stream being invalid from the server or the station is no longer viable through that URL. Try to gather several other links and try them to see which ones work and which do not.

Categories

Resources