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
Related
When I open the app first I can load the Url to the surface view but later on itemclick when i try to laod another url it cannot load.
My code to load url to player is :
private void setChannelLink(String url){
SurfaceHolder videoHolder = videoSurface.getHolder();
videoHolder.addCallback(this);
player = new MediaPlayer();
try {
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setDataSource(this, Uri.parse(url));
player.setOnPreparedListener(this);
player.setOnErrorListener(this);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
when I try to load another url i am doing player.stop() and player.reset() before calling this method.. What am i missing?
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);
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();
}
}
Can anyone please suggest me how to play live radio stream url in android. Someone who has experience in playing live radio url in android.
Thanks
try
{
MediaPlayer media = new MediaPlayer();
media.setAudioStreamType(AudioManager.USE_DEFAULT_STREAM_TYPE);
media.setDataSource("http://indiespectrum.com:9000");
media.prepare();
media.start();
}
catch(Exception e)
{
//Getting Exception
}
public void startRadio(String streamUrl) {
MediaPlayer mPlayer = new MediaPlayer();
mPlayer.setOnErrorListener(
new MediaPlayer.OnErrorListener() {
public boolean onError(MediaPlayer mp, int what, int extra) {
Log.e(getClass().getName(), "Error in MediaPlayer: (" + what +") with extra (" +extra +")" );
}
});
try {
mPlayer.setDataSource(streamUrl);
mPlayer.prepare();
mPlayer.start();
} catch (IllegalArgumentException e) {
Log.e(getClass().getName(), "IllegalArgumentException");
} catch (IllegalStateException e) {
Log.e(getClass().getName(), "IllegalStateException");
} catch (IOException e) {
Log.e(getClass().getName(), "IOException");
}
}
Quick copy paste ..
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.