MediaPlayer error when attempt to play from a stream - android

Does anybody know what does the second argument mean from this error (1, -2147483648) in MediaPlayer? I constantly receive it when attempting to play an audio from a url stream. I try to play it from a class that extends BaseExpandableListAdapter if that matter. I have already reviewed this post Android MediaPlayer error: MediaPlayer error(1, -2147483648) on Stream from internet however all the answers refers to the stream support issues. It's not a stream support issue in my case since I am able to play an audio from the same stream but just using a different class. This is a method that I'm using for playing:
private void startPlaying(String fileName) {
mediaPlayer = new MediaPlayer();
try {
if (fileInputStream != null) { // Read a file from a fileInputStream from a filesystem (EXTERNAL OR INTERNAL storage)
mediaPlayer.setDataSource(fileInputStream.getFD());
Log.d("MediaPlayer is playing", "from device");
} else {
// mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(getFilePath()); // Read a file from a url
Log.d("MediaPlayer is playing", "from stream");
}
mediaPlayer.prepare();
mediaPlayer.start();
mediaPlayer.setOnCompletionListener(new CompletionListener());
} catch (IOException e) {
Log.e(LOG_TAG, "prepare() failed");
}
}

The error is usually one of these in this case:
File path is in error. Incorrect directory or Url or Uri found.
Media file is in error, incompatible format.
Missing permissions
Here's a good blog that outlines these situations and how to fix them:
http://www.weston-fl.com/blog/?p=2988
Also see this thread:
Android mediaplayer MediaPlayer(658): error (1, -2147483648)

Related

play mp3 file in resource folder not working

I want to play an mp3 file in my res/raw folder.
But i get error as "error (1, -2147483648)" and IOException on mp.prepare()
My code
try {
MediaPlayer mPlayer = MediaPlayer.create(NavigationHome.this, R.raw.notfy);
mp.prepare();
mp.start();
} catch (Exception e) {
e.printStackTrace();
}
I also tried with
try {
mp.setDataSource(NavigationHome.this, Uri.parse("android.resource://com.hipay_uae/res/raw/notfy"));
mp.prepare();
mp.start();
} catch (Exception e) {
e.printStackTrace();
}
Another solution that I tried
AssetFileDescriptor afd = getAssets().openFd("AudioFile.mp3");
MediaPlayer player = new MediaPlayer();
player.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
player.prepare();
player.start();
These too didn't work for me.
It will help more if you can post the StackTrace in your question.
But, as per the information in your question, the below code should work for playing the media file from the raw resource folder.
If you use the create() method, prepare() gets called internally and you don't need to explicitly call it.
MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.notify);
mediaPlayer.start();
But, the point to consider is that prepare() generally throws an IllegalStateException, and in your case, you are getting an IOException. So it would be worth checking if the file is in fact present in raw folder and/or the file is corrupt.
Try to initialize your media player before preparing it or setting data source to it
Play From external directory
String filePath = Environment.getExternalStorageDirectory()+"/folderName/yourfile.mp3";
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(filePath);
mediaPlayer.prepare();
mediaPlayer.start()
From raw folder
MediaPlayer mediaPlayer = MediaPlayer.create(MainActivity.this,R.raw.song);
mediaPlayer.start();
Try this
String fname="your_filename";
int resID=getResources().getIdentifier(fname, "raw", getPackageName());
MediaPlayer mediaPlayer=MediaPlayer.create(this,resID);
mediaPlayer.start();

Android MediaPlayer won't play music from URL

I have a server side webapp which provides file upload and download functionality and an Android app which uploads music files onto the web site and then requests uploaded files by some URL.
My URL is structured like: http://mywebsite:8080/api/v1/files/fileById?fileId=<file_id>
When I'm trying to get a file from a web browser, it works fine; the browser downloads file and the OS can play it. But when I'm trying to put URL described before into MediaPlayer as a datasource, I'm getting java.io.IOException: setDataSource failed.
The code of MediaPlayer usage:
private void startPlaying(Uri fileUri) {
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(this, fileUri);
mediaPlayer.prepareAsync();
mediaPlayer.setOnPreparedListener(preparedPlayer -> preparedPlayer.start());
}
catch (IOException e) {
LogUtil.loge(LOG_TAG, e);
stopPlaying();
return;
}
mediaPlayer.setOnCompletionListener(mp -> stopPlaying());
}
private void stopPlaying() {
if (mediaPlayer == null) { return; }
mediaPlayer.stop();
mediaPlayer.reset();
mediaPlayer.release();
mediaPlayer = null;
}
UPD: maybe I have to implement direct links to the files on a server side. all the samples for playing music from URL are requesting direct URL to the file.
Any suggestions?
If you want to play music from a URL, you have to input a String parameter.
Change to:
private void startPlaying(String url) {
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(url);
mediaPlayer.prepareAsync();
mediaPlayer.setOnPreparedListener(preparedPlayer -> preparedPlayer.start());
}
catch (IOException e) {
LogUtil.loge(LOG_TAG, e);
stopPlaying();
return;
}
mediaPlayer.setOnCompletionListener(mp -> stopPlaying());
}
I think I can close this question because I've checked my code correctness by using URL for an external service which works fine. I found that this problem is a problem in my server side app. Android app code is correct.

Streaming music and save cache file on local on Android

I use below code to play streaming music:
try {
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(URL);
mediaPlayer.prepare();
mediaPlayer.start();
}
catch(Exception e) {
e.printStackTrace();
}
I want to buffering file to local cache file at the same time.
And next time want to play this url music can play the local file directly.
How can I do it?
Or anyone can provide references?

How to play audio file of format wav in android webview?

This is my code: mp3 format file plays without any error, but wav format brings MediaPlayer error(1,-1):
try {
MediaPlayer player = new MediaPlayer();
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setDataSource("h*.wav");
player.prepare();
player.start();} catch (Exception e) {
// TODO: handle exception
}
Kindly make a reference to Android supported media-formats

Streaming AAC audio with Android

As I understand it, Android will only play AAC format audio if it's encoded as MPEG-4 or 3GPP.
I'm able to play AAC audio encoded as M4A when it's local to the app, but it fails when obtaining it from a server.
The following works, as the m4a file is held locally in the res/raw directory.
MediaPlayer mp = MediaPlayer.create(this, R.raw.*file*);
mp.start();
The following doesn't work. (But does with MP3's).
Uri uri = Uri.parse("http://*example.com*/blah.m4a");
MediaPlayer mp = MediaPlayer.create(this, uri);
mp.start();
Can anyone shed any light on why it fails when the m4a audio file is not local?
Here's (some of) the error...
ERROR/PlayerDriver(542): Command PLAYER_INIT completed with an error or info UNKNOWN PVMFStatus
ERROR/MediaPlayer(769): error (200, -32)
WARN/PlayerDriver(542): PVMFInfoErrorHandlingComplete
DEBUG/MediaPlayer(769): create failed:
DEBUG/MediaPlayer(769): java.io.IOException: Prepare failed.: status=0xC8
DEBUG/MediaPlayer(769): at android.media.MediaPlayer.prepare(Native Method)
DEBUG/MediaPlayer(769): at android.media.MediaPlayer.create(MediaPlayer.java:530)
DEBUG/MediaPlayer(769): at android.media.MediaPlayer.create(MediaPlayer.java:507)
...
I'm targeting SDK 1.6.
This work-around allows you to play M4A files from the net (and AAC files in other containers such as MP4 & 3GP). It simply downloads the file and plays from the cache.
private File mediaFile;
private void playAudio(String mediaUrl) {
try {
URLConnection cn = new URL(mediaUrl).openConnection();
InputStream is = cn.getInputStream();
// create file to store audio
mediaFile = new File(this.getCacheDir(),"mediafile");
FileOutputStream fos = new FileOutputStream(mediaFile);
byte buf[] = new byte[16 * 1024];
Log.i("FileOutputStream", "Download");
// write to file until complete
do {
int numread = is.read(buf);
if (numread <= 0)
break;
fos.write(buf, 0, numread);
} while (true);
fos.flush();
fos.close();
Log.i("FileOutputStream", "Saved");
MediaPlayer mp = new MediaPlayer();
// create listener to tidy up after playback complete
MediaPlayer.OnCompletionListener listener = new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
// free up media player
mp.release();
Log.i("MediaPlayer.OnCompletionListener", "MediaPlayer Released");
}
};
mp.setOnCompletionListener(listener);
FileInputStream fis = new FileInputStream(mediaFile);
// set mediaplayer data source to file descriptor of input stream
mp.setDataSource(fis.getFD());
mp.prepare();
Log.i("MediaPlayer", "Start Player");
mp.start();
} catch (Exception e) {
e.printStackTrace();
}
}
I tried it too but I could not find out the solution!
At the last Google I/O I saw something that helped me a lot. It is Extending from MediaPlayer and improve a lot of things! Take a look.
EXOPLAYER CAN HELP YOU A LOT
Check this part of the example:
private static final int BUFFER_SEGMENT_SIZE = 64 * 1024;
private static final int BUFFER_SEGMENT_COUNT = 256;
...
// String with the url of the radio you want to play
String url = getRadioUrl();
Uri radioUri = Uri.parse(url);
// Settings for exoPlayer
Allocator allocator = new DefaultAllocator(BUFFER_SEGMENT_SIZE);
String userAgent = Util.getUserAgent(context, "ExoPlayerDemo");
DataSource dataSource = new DefaultUriDataSource(context, null, userAgent);
ExtractorSampleSource sampleSource = new ExtractorSampleSource(
radioUri, dataSource, allocator, BUFFER_SEGMENT_SIZE * BUFFER_SEGMENT_COUNT);
audioRenderer = new MediaCodecAudioTrackRenderer(sampleSource);
// Prepare ExoPlayer
exoPlayer.prepare(audioRenderer);
EXOPLAYER- I can play anything from streamings (video and audio)!
LET ME KNOW IF YOU NEED HELP TO IMPLEMENT IT! :)
This is a wild shot in the dark, but I have seen similar behavior with the flash player where it actually ignores the file name and only relies on the MIME type sent by the server. Any idea what headers are being sent down from example.com? You might want to try wrapping your blah.m4a in a page that can set the headers and then stream the binary data. Give these types a shot and the community would appreciate a confirmation of what works:
audio/mpeg
audio/mp4a
audio/mp4a-latm
audio/aac
audio/x-aac
I found that if you record the audio file on Android with the following properties, you are then able to play it on your server. It also plays well in the HTML Audio Element, however only on Firefox at the moment. This may change in the future.
Android (JAVA):
mediaRecorder = new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.HE_AAC);
mediaRecorder.setAudioSamplingRate(44100);
mediaRecorder.setAudioChannels(1);
mediaRecorder.setOutputFile(filePath);
HTML:
<audio id="audioMediaControl" controls src="yourfile.m4a"> Your browser does not support the audio element. </audio>
try --
1) MP.prepareAsync()
2) onPrepared() { mp.start() }

Categories

Resources