Streaming audio issue on ICS - android

I'm using audio streaming to play MP3 files on a device and had no problems until ICS came out. Then I thought it might be a format support issue so ive tried OGG files with the following code :
MediaPlayer mmp= MediaPlayer.create(context,
Uri.parse("http://vorbis.com/music/Epoq-Lepidoptera.ogg"));
mmp.start();
This works on earlier OS versions but fails on ICS. I have also tried creating media player with setDataSource(this, uri) using onPreparesListener atc but still the same error:
04-08 09:52:12.459: D/MediaPlayer(541): Couldn't open file on client side, trying server side
04-08 09:52:12.489: E/MediaPlayer(541): Unable to to create media player
04-08 09:52:12.529: D/MediaPlayer(541): create failed:
04-08 09:52:12.529: D/MediaPlayer(541): java.io.IOException: setDataSource failed.: status=0x80000000
04-08 09:52:12.529: D/MediaPlayer(541): at android.media.MediaPlayer._setDataSource(Native Method)
04-08 09:52:12.529: D/MediaPlayer(541): at android.media.MediaPlayer.setDataSource(MediaPlayer.java:844)
04-08 09:52:12.529: D/MediaPlayer(541): at android.media.MediaPlayer.setDataSource(MediaPlayer.java:806)
04-08 09:52:12.529: D/MediaPlayer(541): at android.media.MediaPlayer.setDataSource(MediaPlayer.java:761)
04-08 09:52:12.529: D/MediaPlayer(541): at android.media.MediaPlayer.create(MediaPlayer.java:695)
04-08 09:52:12.529: D/MediaPlayer(541): at android.media.MediaPlayer.create(MediaPlayer.java:676)
So my question is how to stream audio files using MediaPlayer on ICS? Does anyone have working code or maybe there must be some specific file format and encoding?
But it sucks that new os version isnt compatibile backwards...

I likewise had the same problem, so I switched gears and did this instead, which has worked fine for me. You have to keep your sound files in a res/assets folder however.
try {
AssetFileDescriptor afd = getAssets().openFd("mysound.mp3");
mPlayer = new MediaPlayer();
mPlayer.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
mPlayer.prepare();
mPlayer.start();
}
catch (IllegalStateException e) {
Log.d(TAG, "IllegalStateException: " + e.getMessage());
}
catch (IOException e) {
Log.d(TAG, "IOException: " + e.getMessage());
}
catch (IllegalArgumentException e) {
Log.d(TAG, "IllegalArgumentException: " + e.getMessage());
}
catch (SecurityException e) {
Log.d(TAG, "SecurityException: " + e.getMessage());
}
And then you would release the resources in the usual way:
if(mPlayer != null) {
if(mPlayer.isPlaying()) mPlayer.stop();
mPlayer.release();
mPlayer = null;
}
Hope this works for you too.

Related

MediaPlayer cannot play video from url

I am using a TextureView to play a video from my App-Engine server. I can play the video fine if I open the link in a browser. But MediaPlay has the following errors about file not found and then not being about to play the video
I/MediaPlayer: Need to enable context aware info
E/MediaPlayer-JNI: QCMediaPlayer mediaplayer NOT present
D/MediaPlayer: setDataSource IOException happend :
D/MediaPlayer: java.io.FileNotFoundException: No content provider: https://companycloud.appspot.com/watchvideo/?videoid=1234567771234567
D/MediaPlayer: at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1074)
D/MediaPlayer: at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:927)
D/MediaPlayer: at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:854)
D/MediaPlayer: at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1085)
D/MediaPlayer: at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1059)
D/MediaPlayer: at com.company.app.android.fragment.VideoFragment.playNewVideo(VideoFragment.java:411)
D/MediaPlayer: at com.company.app.android.fragment.VideoFragment.access$400(VideoFragment.java:63)
D/MediaPlayer: at com.company.app.android.fragment.VideoFragment$1.onReceive(VideoFragment.java:120)
D/MediaPlayer: at android.support.v4.content.LocalBroadcastManager.executePendingBroadcasts(LocalBroadcastManager.java:297)
D/MediaPlayer: at android.support.v4.content.LocalBroadcastManager.access$000(LocalBroadcastManager.java:46)
D/MediaPlayer: at android.support.v4.content.LocalBroadcastManager$1.handleMessage(LocalBroadcastManager.java:116)
D/MediaPlayer: at android.os.Handler.dispatchMessage(Handler.java:102)
D/MediaPlayer: at android.os.Looper.loop(Looper.java:145)
D/MediaPlayer: at android.app.ActivityThread.main(ActivityThread.java:5837)
D/MediaPlayer: at java.lang.reflect.Method.invoke(Native Method)
D/MediaPlayer: at java.lang.reflect.Method.invoke(Method.java:372)
D/MediaPlayer: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)
D/MediaPlayer: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)
D/MediaPlayer: Couldn't open file on client side, trying server side
10-24 12:25:26.665 6742-7036/com.company.app.android E/MediaPlayer: error (1, -2147483648)
10-24 12:25:26.685 6742-6742/com.company.app.android E/MediaPlayer: Error (1,-2147483648)
Well I cant comment and since there isn't any reference code to look at, here are few suggestions:
1)
The solution works for Android version 4.0 or higher.
Initialize the MedialPlayer object inside the onSurfaceTextureAvailable() method and put it inside the try/catch block.
Parse the Uri. Since A Uri object is usually used to pass the reference of the content to a ContentProvider.
#Override
public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int i, int i2) {
Surface surface = new Surface(surfaceTexture);
try {
mMediaPlayer= new MediaPlayer();
// parse the URL path
mMediaPlayer.setDataSource(getApplicationContext(), Uri.parse(URL_PATH));
mMediaPlayer.setSurface(surface);
mMediaPlayer.setLooping(true);
mMediaPlayer.prepareAsync();
mMediaPlayer.setOnBufferingUpdateListener(this);
mMediaPlayer.setOnCompletionListener(this);
mMediaPlayer.setOnVideoSizeChangedListener(this);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
// Play video when the media source is ready for playback.
mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
mediaPlayer.start();
}
});
} catch (IllegalArgumentException e) {
Log.d(TAG, e.getMessage());
} catch (SecurityException e) {
Log.d(TAG, e.getMessage());
} catch (IllegalStateException e) {
Log.d(TAG, e.getMessage());
} catch (IOException e) {
Log.d(TAG, e.getMessage());
}
}
Also for streams prefer using prepareAsync(), since it returns immediately, rather than blocking until enough data has been buffered.
call MediaPlayer.prepareAsync() method since you use constructor for creating MediaPlayer object.
Don't forget to do a memory cleanup once you are done playing video
#Override
protected void onDestroy() {
super.onDestroy();
if (mMediaPlayer != null) {
mMediaPlayer.stop();
mMediaPlayer.release();
mMediaPlayer = null;
}
}
Hope it helps!

android .mov format video playing from url

I am playing video from url then .mp4 in playing but .mov format is giving IOException
java.io.IOException: Prepare failed.: status=0x1
My code for playing video is,
private void playVideo() {
if (extras.getString("video_path").equals("VIDEO_URI")) {
showToast("Please, set the video URI in HelloAndroidActivity.java in onClick(View v) method");
} else {
new Thread(new Runnable() {
public void run() {
try {
player.setDataSource(extras.getString("video_path"));
player.setDisplay(holder);
player.prepare();
} catch (IllegalArgumentException e) {
showToast("Error while playing video");
Log.i(TAG, "========== IllegalArgumentException ===========");
e.printStackTrace();
} catch (IllegalStateException e) {
showToast("Error while playing video");
Log.i(TAG, "========== IllegalStateException ===========");
e.printStackTrace();
} catch (IOException e) {
showToast("Error while playing video. Please, check your network connection.");
Log.i(TAG, "========== IOException ===========");
e.printStackTrace();
}
}
}).start();
}
}
Any pointers?
Please visit the website vitamio.org or download VitamioDemo from github github.com/yixia/VitamioDemo
if you want to use Vitamio as your video player in an app, you need to download the Vitamio SDK from their website. Play around with the demo project and you'll understand how it works.
I have implemented the same and it works absolutely fine with me.
.mov files are not supported in android.
Check the Supported Media Formats on developers site.

Android mediaplayer stream not playing

i'm trying to work with internet radio streams, but on some streams I get nothing except this:
05-14 13:16:13.480: E/MediaPlayer(2088): error (1, -2147483648)
05-14 13:16:13.480: W/System.err(2088): java.io.IOException: Prepare failed.: status=0x1
05-14 13:16:13.480: W/System.err(2088): at android.media.MediaPlayer.prepare(Native Method)
05-14 13:16:13.480: W/System.err(2088): at com.darko.mk.RadioTresActivity$2.run(RadioTresActivity.java:141)
05-14 13:16:13.480: W/System.err(2088): at java.lang.Thread.run(Thread.java:1019)
Some stations are working fine but some are not playing at all.
Here is my code that gives the error:
public void playRadio(final String source) {
new Thread(new Runnable() {
public void run() {
try {
mediaPlayer.release();
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(source);
mediaPlayer.prepare(); //line 141 that gives the error
mediaPlayer.start();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
What can be the problem? Here is the souce example:http://217.16.69.17:8000/metropolis.mp3
Android documentation
"For streams, you should call prepareAsync(), which returns immediately, rather than blocking until enough data has been buffered."
iplayer.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
iplayer.prepareAsync();
To Stream audio using Android media player you should start playing audio once you receive onPreparedListner callback.As loading the data into the media player takes time .If you start playing the media player without listening onPreparedListner media player will Exception.

Not able to play Sound with this code

I am using below code in my app to play the Sound.
But i am not able to play it.
Whats the Problem ??
Code:
public void playSound(int resources){
try{
boolean mStartPlaying = true;
MediaPlayer mPlayer=null;
if (mStartPlaying==true){
mPlayer = new MediaPlayer();
Uri uri = Uri.parse("android.resource://MY_PACKAGE/" + resources);
mPlayer.setDataSource(getApplicationContext(),uri);
mPlayer.prepare();
mPlayer.start();
}
else{
mPlayer.release();
mPlayer = null;
}
mStartPlaying = !mStartPlaying;
}
catch (IOException e){
Log.e(LOG_TAG, "prepare() failed");
}
}
And i get log cat as like below:
05-09 05:24:22.272: VERBOSE/PlayerDriver(95): Completed command PLAYER_INIT status=PVMFFailure
05-09 05:24:22.272: ERROR/PlayerDriver(95): Command PLAYER_INIT completed with an error or info PVMFFailure
05-09 05:24:22.272: ERROR/MediaPlayer(8072): error (1, -1)
05-09 05:24:22.272: ERROR/AudioPlayTest(8072): prepare() failed
05-09 05:24:22.282: VERBOSE/PVPlayer(95): run_set_video_surface s=-2147483648, cancelled=0
05-09 05:24:22.282: VERBOSE/PlayerDriver(95): HandleInformationalEvent: PVMFInfoErrorHandlingComplete
05-09 05:24:22.282: WARN/PlayerDriver(95): PVMFInfoErrorHandlingComplete
Maybe you're trying to play unsupported format.
Do you know what fails? : new, setDataSource, prepare, start ... moreover IOException tells you there is something wrong with input file, such FileNotFoundException or EOFException (try to printout a more specific exception as suggested here)
ps.: moved comments in answer.

Android - Load video from private folder of app

I have a problem that I have been trying to find a solution for a long time. My situation is as follows:
I have an app that downloads zipped videos and unzips them at the application's private folder and more specifically at a subfolder. For example at /data/data/my.app.package.name.here/files/assets/assets-955.
Inside this folder the video is unzipped. The unzipping process is completed successfully since I can pull and view the video without problems when running the app on the emulator.
I then have another activity that is accessing this folder, finds the video file and tries to open it. At this point I get an error that "Sorry, this video cannot be played" with the following error stack:
01-30 17:36:17.770: D/ContentDemoActivity(6757): File: /data/data/xxxx/files/assets/assets-955/bank_2.mp4
01-30 17:36:17.830: I/MediaPlayer(6757): prepareAsync called in state 4
01-30 17:36:17.830: E/MediaPlayer(6757): error (1, -2147483648)
01-30 17:36:17.860: E/MediaPlayer(6757): Error (1,-2147483648)
01-30 17:36:17.860: D/VideoView(6757): Error: 1,-2147483648
01-30 17:36:19.370: E/MediaPlayer(6757): stop called in state 0
01-30 17:36:19.370: E/MediaPlayer(6757): error (-38, 0)
01-30 17:36:19.370: W/MediaPlayer(6757): mediaplayer went away with unhandled events
The code with which I am trying to play the video is pretty basic:
mView = (VideoView) findViewById(R.id.videoView);
mMediaPlayer = new MediaPlayer();
mView.requestFocus();
mHolder = mView.getHolder();
Log.d(tag, "Populating content. Assets path: " + mAssetsPath);
File f = new File(mAssetsPath);
File[] files = f.listFiles();
Log.d(tag, "File: " + files[0].toString());
mView.setVideoURI(Uri.parse(files[0].toString()));
mView.setMediaController(new MediaController(this));
and the layout of the activity has a plain VideoView, nothing fancy there.
The strangest thing is that for testing purposes I used the same video, this time loading it from the "raw" folder and it runs smoothly without problem. In that case though I had to load it with:
Uri video = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.bank_2);
mVideoView.setVideoURI(video);
mVideoView.start();
I would do the same with the downloaded videos but there doesn't seem to be any function at the API that will allow me to load a video Uri from the application's private folder.
I have found various solutions by using file descriptors, listeners for the videoView, flags indicating MODE_WORLD_READABLE, pre-calculation of the dimensions of the videoView, etc but none of them had positive results.
In a nutshell, my questions are:
Why do I get those errors which according to what I have found online are errors that are related with problematic encoding of the video file ?
What is the best things to use in my case, a VideoView or a surfaceView ?
Which is the ideal method to load a video from the application's private folder and be able to play it?
Thanks.
EDIT
After CommonsWare suggestion, I went with the following implementation:
File f = new File(mAssetsPath);
File[] files = f.listFiles();
Log.d(tag, "File: " + files[0].toString());
URI uri = URI.create("file://" + (files[0].toString()));
File file = new File(uri);
try {
Log.d(tag, "1");
ParcelFileDescriptor parcel = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_WRITE);
Log.d(tag, "2");
mMediaPlayer.setDataSource(parcel.getFileDescriptor());
Log.d(tag, "3");
mMediaPlayer.start();
Log.d(tag, "4");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Log.d(tag, "5");
Unfortunately, this time I get the following errors:
01-31 12:40:11.480: D/ContentDemoActivity(15896): File: /data/data/com.houseofradon.meb/files/assets/assets-955/bank_2.mp4
01-31 12:40:11.480: D/ContentDemoActivity(15896): 1
01-31 12:40:11.480: D/ContentDemoActivity(15896): 2
01-31 12:40:11.500: D/ContentDemoActivity(15896): 3
01-31 12:40:11.500: E/MediaPlayer(15896): start called in state 2
01-31 12:40:11.500: E/MediaPlayer(15896): error (-38, 0)
01-31 12:40:11.500: D/ContentDemoActivity(15896): 4
01-31 12:40:11.500: D/ContentDemoActivity(15896): 5
01-31 12:40:11.530: E/MediaPlayer(15896): Error (-38,0)
So, something happens when the media player starts. Error code -38 doesn't seem to mean anything specific as I found here.
Any idea what I am missing ???
EDIT #2
I now use a mediaPlayer and a SurfaceView to do the whole process along with a surfaceHolder listener. Here is the code:
mMediaPlayer = new MediaPlayer();
mSurfaceView = (SurfaceView) findViewById(R.id.surface);
mHolder = mSurfaceView.getHolder();
mHolder.addCallback(this);
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Log.d(tag, "surfaceChanged");
try {
mMediaPlayer.setDisplay(mHolder);
Log.d(tag, "7");
mMediaPlayer.start();
Log.d(tag, "8");
} catch (IllegalStateException e) {
e.printStackTrace();
}
Log.d(tag, "9");
}
public void surfaceCreated(SurfaceHolder holder) {
Log.d(tag, "surfaceCreated");
File f = new File(mAssetsPath);
File[] files = f.listFiles();
Log.d(tag, "File: " + files[0].toString());
URI uri = URI.create("file://" + (files[0].toString()));
File file = new File(uri);
try {
Log.d(tag, "1");
ParcelFileDescriptor parcel = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_WRITE);
Log.d(tag, "2");
mMediaPlayer.setDataSource(parcel.getFileDescriptor());
Log.d(tag, "3");
mMediaPlayer.setVolume(100, 100);
Log.d(tag, "4");
mMediaPlayer.prepare();
Log.d(tag, "5");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Log.d(tag, "6");
}
I can listen to the audio of the video but the picture is just a plain black color. I also get an error almost at the end of the video playback that says:
01-31 14:26:01.300: W/AudioSystem(17165): AudioFlinger server died!
01-31 14:26:01.300: W/IMediaDeathNotifier(17165): media server died
01-31 14:26:01.300: E/MediaPlayer(17165): error (100, 0)
01-31 14:26:01.300: E/MediaPlayer(17165): Error (100,0)
I am using an actual device, Samsung Galaxy Tab 10.1. Any ideas ?
Why do I get those errors which according to what I have found online are errors that are related with problematic encoding of the video file ?
Because the media playback engine runs in its own process, and it does not have rights to read your file.
What is the best things to use in my case, a VideoView or a surfaceView ?
A VideoView contains a SurfaceView. Whether you use VideoView or a combination of MediaPlayer and SurfaceView is up to you.
Which is the ideal method to load a video from the application's private folder and be able to play it?
Either create a ContentProvider that can serve up your local file and use the provider Uri instead of the Uri to a local file, or create the local file using openFileOutput() and MODE_WORLD_READABLE.

Categories

Resources