Android MediaPlayer - I/O error - File not found - android

I have a res/raw folder made in the project in which I placed a single file t.mp4. I intended to use a textureview to play the video file using the SurfaceTextureAvailable listener by implementing the relevant interface.
I created a mediaplayer object in the onSurfaceTextureAvailable method
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
Surface s = new Surface(surface);
Log.d("debug", "Surface Texture Available");
mMediaPlayer = new MediaPlayer();
try {
mMediaPlayer.setDataSource(this, Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.t));
Log.d("debug", "data source set");
mMediaPlayer.setSurface(s);
Log.d("debug", "Surface set");
mMediaPlayer.prepare();
Log.d("debug", "prepared");
mMediaPlayer.setOnBufferingUpdateListener(this);
mMediaPlayer.setOnCompletionListener(this);
mMediaPlayer.setOnPreparedListener(this);
mMediaPlayer.setOnVideoSizeChangedListener(this);
Log.d("debug", "listeners set");
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
} catch (IOException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.d("error", e.getMessage());
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.d("error", e.getMessage());
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.d("error", e.getMessage());
}
}
This worked fine the first few times I ran the app, but after working on another (non-related) part of my project, it suddenly stopped working.
In the log, I keep getting an IOException, which I am 90% sure is because the application cannot find the file.
What happened?
Cheers

You can use MediaPlayer.create() in place of new MediaPlayer()/setDataSource() to easily create a MediaPlayer for a raw resource:
mMediaPlayer = MediaPlayer.create(this, R.raw.t);

Related

Live Video Streaming From one android device to other android device over wifi

Through below code i am starting video streaming from one device and its running successfully
// Begin video communication
final ParcelFileDescriptor pfd = ParcelFileDescriptor.fromSocket(client);
handler.post(new Runnable() {
#Override
public void run() {
recorder = new MediaRecorder();
recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setOutputFile(pfd.getFileDescriptor());
// recorder.setVideoFrameRate(20);
recorder.setVideoSize(176, 144);
recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263);
recorder.setPreviewDisplay(mHolder.getSurface());
try {
recorder.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
recorder.start();
}
catch (Exception e)
{
System.out.println("Video recorder3.3333 " + e);
}
Through below code i am playing video streaming on second device
clientSocket = new Socket(InetAddress.getByName(SERVERIP), SERVERPORT);
ParcelFileDescriptor pfd = ParcelFileDescriptor.fromSocket(clientSocket);
pfd.getFileDescriptor().sync();
pfd.close();
mp.setDisplay(holder);
mp.prepareAsync();
mp.start();
but problem on this line // pfd.getFileDescriptor().sync();
i got error:
java.io.SyncFailedException: fsync failed: EINVAL (Invalid argument)
what is the solution for this?

Android MediaPlayer does not loop when its data source is from url

I am using MediaPlayer in my app. I am following the following tutorial
http://blog.lemberg.co.uk/surface-view-playing-video
Here's part of the code
Surface surface = new Surface(surfaceTexture);
try {
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setDataSource(getApplicationContext(), Uri.parse(FILE_URL));
mMediaPlayer.setSurface(surface);
mMediaPlayer.setLooping(true);
mMediaPlayer.prepareAsync();
// 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());
}
Everything works fine except the video does not loop. When I set the data source a file from assets it loops just fine. But when I stream the video from URL it does not loop.
Thanks

Android Media Player plays sound only once

public void playClickSound() {
AssetFileDescriptor afd;
MediaPlayer sound = new MediaPlayer();
try {
sound.setAudioStreamType(AudioManager.STREAM_MUSIC);
afd = getAssets().openFd("click.mp3");
sound.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
sound.prepare();
sound.start();
} catch (IllegalStateException e) {
} catch (IOException e) {
e.printStackTrace();
}
}
This is my code for playing sound. I call this method on a few buttons in mu GUI.
It works fine the first time i press the button, but second time i get IllegalStateException.
What should I do to make this work?
You need to manage the life-cycle of the Media Player. Following the below flow, should work out:
RefreshPlayer()
{
if (mediaPlayer != null) {
{
mediaPlayer.stop();
mediaPlayer.reset();
}
}
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(getSherlockActivity(),
Uri.fromFile(new File(VidPath)));
mediaPlayer.setLooping(true);
mediaPlayer.prepare();
mediaPlayer.start();
} catch (IllegalArgumentException e)
{
e.printStackTrace();
} catch (IllegalStateException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}

Media Player called in state 0, error (-38,0) error

i'm trying to use this code which i called from a button but it doesn't work and when i see the LogCat window i found Media Player called in state 0, error (-38,0)
public void audioPlayer(String path, String fileName){
//set up MediaPlayer
MediaPlayer mp = new MediaPlayer();
try {
mp.setDataSource(HeyActivity.this, Uri.parse(path+"/"+fileName));
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
mp.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
System.out.print("hey");
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mp.start();
}
Try setting OnPreparedListener and call the start method within it. Like this:
MediaPlayer mp = new MediaPlayer();
mp.setOnPreparedListener( new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
mp.start();
}
};
);
I finally solved the problem by running on an emulator that supported audio!!
Can't believe it was that simple!
Window > AVD Manager > Edit your AVD (I created a new one to be on the safe side cause I was running from snapshot) > Hardware > New > Audio Playback Support

Error creating MediaPlayer with Uri or file in assets

I copied song.mp3 to my project's assets directory and wrote this code:
private MediaPlayer mp;
Uri uri = Uri.parse("file:///android_asset/song.mp3");
mp=MediaPlayer.create(this, uri);
After running the create statement, the variable mp is null. What is wrong?
Thanks.
Try this:
try {
AssetFileDescriptor afd = getAssets().openFd("AudioFile.mp3");
player = new MediaPlayer();
player.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
player.prepare();
player.start();
}
catch (IllegalArgumentException e) { }
catch (IllegalStateException e) { }
catch (IOException e) { }
Try this and see if any exceptions are caught:
try {
MediaPlayer mp = new MediaPlayer();
mp.setDataSource(this, uri);
}
catch (NullReferenceArgument e) {
Log.d(TAG, "NullReferenceException: " + e.getMessage());
}
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());
}
The exception caught will explain what is going wrong in your create. According the the docs, the static create method is just shorthand for what is in the try block above. The major difference that I can see is that the static method create doesn't throw while setDataSource does.
You'd better try this on the devices running on Android N or the latest:
try {
AssetFileDescriptor afd = getAssets().openFd("*.mp3 / *.mp4");
player = new MediaPlayer();
player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
player.setDataSource(afd);
player.prepareAsync();
player.start();
} catch (...) {
}
else, do like the best answer below.

Categories

Resources