Not able to play Sound with this code - android

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.

Related

I want to play wav file stored in external directory but I am not able to create MediaPlayer instance

I am writing a small app that creates wav files using a microphone. I am using AudioRecord class do get raw data and then I am saving it in wav format in external storage. The problem is I can not create MediaPlayer instance to saved wav files. I am able to open the selected file and I have got the permissions to read it. Wav files are saved correctly (at least I guess so because I can play then on computer). I would be grateful for any help :)
What I have got so far:
#Override
public void onRecordingClick(int position) {
if (player == null) {
String path = wavFilepathList.get(position);
File audioFile = new File(path);
Log.d("audioFileLog", "File exists: " + audioFile.exists() + ", can read: " + audioFile.canRead());
player = new MediaPlayer();
try {
player.setDataSource(path);
Log.d("audioFileLog", "Data Source Changed");
player.setOnPreparedListener(this);
Log.d("audioFileLog", "Listener Set, before prepare method");
player.prepareAsync();
Log.d("audioFileLog", "after prepare method");
} catch (IOException e) {
e.printStackTrace();
Log.d("audioFileLog", "IOException when setting data source");
}
}
}
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
Log.d("audioFileLog", "after played method");
}
Logcat:
D/audioFileLog: File exists: true, can read: true
E/ExtMediaPlayer-JNI: env->IsInstanceOf fails
E/MediaPlayer-JNI: JNIMediaPlayerFactory: bIsQCMediaPlayerPresent 0
E/ExtMediaPlayer-JNI: env->IsInstanceOf fails
E/MediaPlayer-JNI: JNIMediaPlayerFactory: bIsQCMediaPlayerPresent 0
D/audioFileLog: Data Source Changed
D/audioFileLog: Listener Set, before prepare method
D/audioFileLog: after prepare method
E/MediaPlayer: error (1, -2147483648)
E/MediaPlayer: Error (1,-2147483648)
Older experiments:
1)
public void onRecordingClick(int position) {
if (player == null) {
File selectedFile = new File(wavFilepathList.get(position));
Log.d("Main", "voice exists : " + selectedFile.exists() +
", can read : " + selectedFile.canRead());
player = MediaPlayer.create(this, Uri.fromFile(selectedFile));
}
player.start();
} // on recording clicked
Logcat:
D/Main: voice exists : true, can read : true
E/ExtMediaPlayer-JNI: env->IsInstanceOf fails
E/MediaPlayer-JNI: JNIMediaPlayerFactory: bIsQCMediaPlayerPresent 0
E/ExtMediaPlayer-JNI: env->IsInstanceOf fails
E/MediaPlayer-JNI: JNIMediaPlayerFactory: bIsQCMediaPlayerPresent 0
E/MediaPlayer: error (1, -2147483648)
D/MediaPlayer: create failed:
2)
#Override
public void onRecordingClick(int position) {
if (player == null) {
player= new MediaPlayer();
try {
player.setDataSource(wavFilepathList.get(position));
player.setOnPreparedListener(this);
player.prepareAsync();
} catch (IOException e) {
e.printStackTrace();
}
}
} // on recording clicked
#Override
public void onPrepared(MediaPlayer mp) {
player.start();
}
Logcat:
E/ExtMediaPlayer-JNI: env->IsInstanceOf fails
E/MediaPlayer-JNI: JNIMediaPlayerFactory: bIsQCMediaPlayerPresent 0
E/ExtMediaPlayer-JNI: env->IsInstanceOf fails
E/MediaPlayer-JNI: JNIMediaPlayerFactory: bIsQCMediaPlayerPresent 0
E/MediaPlayer: error (1, -2147483648)
E/MediaPlayer: Error (1,-2147483648)
Try setting the mediaPlayer using the following way:
try{
File audioFile = new File("The File's Path"); // audio.wav path
if(audioFile.exists()){
Log.d("audioFileLog", "Found the file");
MediaPlayer mp = new MediaPlayer();
mp.setDataSource('audioFile.getPath());
mp.prepare();
mp.start();
}else{
Log.d("audioFileLog", "Did not find the file");
}
}catch(Exception ex){
Log.d("audioFileLog", ex.toString());
}
and then use:
mp.stop();
mp.release();
when you have finished.
file patch was incorrect hence the problem...

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.

Streaming audio issue on ICS

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.

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