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!
Related
This question already has answers here:
Media Player called in state 0, error (-38,0)
(17 answers)
Closed 3 years ago.
String url = "http://23.101.29.94:1111/text1.mp3";
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(url);
} catch (IOException e) {
e.printStackTrace();
}
try {
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.start();
When it run:
2019-11-04 20:55:31.221 13816-13816/com.example.loginlogout E/MediaPlayerNative: start called in state 0, mPlayer(0xe9a85440)
2019-11-04 20:55:31.221 13816-13816/com.example.loginlogout E/MediaPlayerNative: error (-38, 0)
2019-11-04 20:55:31.235 13816-13816/com.example.loginlogout E/MediaPlayer: Error (-38,0)
but this url (http://23.101.29.94:1111/text1.mp3) run well in browser, How I fix that
You need to call mediaPlayer.start() in the onPrepared method by using a listener.
Listeners can be attached simply by
mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mediaPlayer.start();
}
});
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...
I am developing a widget that plays a random sound. My problem lies in getting the service I use to handle the MediaPlayer and play the sounds to load the sound files I have stored in my app's asset folder.
Instead of a SoundPool as in my application, I opted for a MediaPlayer since it has an onCompletionListener which allows me to stop the Service after the sound has been completely played. That way I hope to minimize the resource usage of the widget.
More precisely, in my code (checkout the [now outdated] commit on GitHub) I try to load a random sound with the MediaPlayer's create() convenience method using an Uri to file:///android_asset/pathToFile.
This however throws an IOException at runtime.
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent != null) {
final String action = intent.getAction();
if (ACTION_PLAY_FART.equals(action)) {
Log.v(LOG_TAG, "Intent received");
String pathToFile = String.format(
Locale.US,
"fart%02d.ogg",
Utility.getIntBetween(1, 15)
);
MediaPlayer tempMediaPlayer = MediaPlayer.create(
this,
Uri.parse("file:///android_asset/"+pathToFile)
);
tempMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.release();
stopSelf();
}
});
Log.v(LOG_TAG, "Start playing");
tempMediaPlayer.start();
}
}
return START_NOT_STICKY;
}
Is there an easy way (i. e., other than a Content Provider) to load those asset files from inside the Service?
[Edit, providing more information]
Apparently it is possible to access the asset files but the MediaPlayer cannot load them properly.
I modified the code to this:
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent != null) {
final String action = intent.getAction();
if (ACTION_PLAY_FART.equals(action)) {
Log.v(LOG_TAG, "Intent received");
String pathToFile = String.format(
Locale.US,
"fart%02d.ogg",
Utility.getIntBetween(1, 15)
);
try {
AssetFileDescriptor assetFD = this.getAssets().openFd(pathToFile);
MediaPlayer tempMediaPlayer = new MediaPlayer();
tempMediaPlayer.setDataSource(assetFD.getFileDescriptor());
tempMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.release();
stopSelf();
}
});
Log.v(LOG_TAG, "Start playing");
tempMediaPlayer.start();
} catch (IOException e) {
Log.e(LOG_TAG, "File "+pathToFile+" could not be loaded.", e);
}
}
}
The output I get is:
08-29 15:29:54.868 10191-10191/com.y0hy0h.furzknopf V/VolatileFartService﹕ Intent received
08-29 15:29:54.868 10191-10191/com.y0hy0h.furzknopf V/MediaPlayer﹕ constructor
08-29 15:29:54.868 10191-10191/com.y0hy0h.furzknopf V/MediaPlayer﹕ setListener
08-29 15:29:54.868 10191-10191/com.y0hy0h.furzknopf V/MediaPlayer﹕ setDataSource(51, 0, 576460752303423487)
08-29 15:29:54.898 10191-10191/com.y0hy0h.furzknopf E/MediaPlayer﹕ Unable to to create media player
08-29 15:29:54.908 10191-10191/com.y0hy0h.furzknopf E/VolatileFartService﹕ File fart07.ogg could not be loaded.
java.io.IOException: setDataSourceFD failed.: status=0x80000000
at android.media.MediaPlayer.setDataSource(Native Method)
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1032)
at com.y0hy0h.furzknopf.widget.VolatileFartService.onStartCommand(VolatileFartService.java:39)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2524)
at android.app.ActivityThread.access$1900(ActivityThread.java:138)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1302)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4929)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:798)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:565)
at dalvik.system.NativeStart.main(Native Method)
Note that line 39 is
tempMediaPlayer.setDataSource(assetFD.getFileDescriptor());
Alternitavely, is there a way to pass the FileDescriptor or other object for the service to load?
Or might there be an even better way to handle the playback of possibly multiple sounds simultaneously than to put that functionality in a Service?
As an alternative to Want2bExpert's suggestion of moving the files into the res/raw folder, the asset files can be loaded "manually" into the MediaPlayer.
Apparently the assets are not stored as would be expected. Instead, the start position and length of the asset together with its (non Asset-)FileDescriptor needs to be passed on to the MediaPlayer.
The working code:
String pathToFile = String.format(
Locale.US,
"fart%02d.ogg",
Utility.getIntBetween(1, 15)
);
try {
AssetFileDescriptor assetFD = getAssets().openFd(pathToFile);
MediaPlayer tempMediaPlayer = new MediaPlayer();
tempMediaPlayer.setDataSource(
assetFD.getFileDescriptor(),
assetFD.getStartOffset(),
assetFD.getLength()
);
assetFD.close();
tempMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.release();
stopSelf();
}
});
tempMediaPlayer.prepare();
Log.v(LOG_TAG, "Start playing");
tempMediaPlayer.start();
} catch (IOException e) {
Log.e(LOG_TAG, "File "+pathToFile+" could not be loaded.", e);
}
Try this;
Create a raw folder inside your resource dir e.g. res/raw
put the files to play in the raw folder
load the file using media player as below
MediaPlayer mediaPlayer = MediaPlayer.create(serviceClass.this, R.raw.sound_file_1);
mediaPlayer.start();
Read more;
http://developer.android.com/guide/topics/media/mediaplayer.html#mpandservices
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.
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.