Android can not create media player - android

I have one mp4 video and i try to play my video in TextureView.
I have one problem. In Nexus or HTC one i have no problem but in some Huawei devices i have this message
Unable To create Media player
this is a my source
private void playVideoAnimation()
{
textureView.setVisibility(View.VISIBLE);
mMediaPlayer = new MediaPlayer();
textureView.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
#Override
public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int i, int i2) {
Surface surface = new Surface(surfaceTexture);
try {
Log.e("FILE_NAME", FILE_NAME);
AssetFileDescriptor afd = getAssets().openFd(FILE_NAME);
mMediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
mMediaPlayer.setSurface(surface);
mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
if (mediaPlayer.isPlaying()) {
}
mediaPlayer.start();
}
});
mMediaPlayer.prepareAsync();
} 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());
}
}
#Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
}
#Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
return false;
}
#Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
}
});
mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
Log.e("FILE_NAME", "Finished");
}
});
}
as I said I can play my video in Nexus and HTC devices but yesterday i run my app in Huawei and i have this error message.
How i can check if device can play video?
If anyone knows solution please help me

Related

Glitch while changing video inside media player

My requirement is to change video inside video player with animation. When a person taps on "change" button the video in media player will get change with a new one and the old will end up with zoom in animation. I am able to implement this functionality with the help of texture view but I am getting one issue that there is a glitch while changing the video. is there any way I can make it more smooth?
Here is my code :
public class VideoVieww extends Activity implements TextureView.SurfaceTextureListener {
private TextureView textureView;
private MediaPlayer mMediaPlayer;
private Button mButton;
private Animation zoomAnimation;
private SurfaceTexture mSurfaceTexture;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.texture_layout);
textureView = findViewById(R.id.textureView);
textureView.setSurfaceTextureListener(this);
zoomAnimation = AnimationUtils.loadAnimation(VideoVieww.this, R.anim.zomm_out);
mButton = findViewById(R.id.change);
mButton.setOnClickListener(view -> {
if (mMediaPlayer.isPlaying()) {
mMediaPlayer.stop();
mMediaPlayer.release();
mMediaPlayer = null;
}
textureView.startAnimation(zoomAnimation);
try {
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setDataSource(VideoVieww.this, Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.broadchurch));
} catch (IOException e) {
e.printStackTrace();
}
try {
Surface surface = new Surface(mSurfaceTexture);
mMediaPlayer.setSurface(surface);
mMediaPlayer.prepare();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
});
zoomAnimation.setAnimationListener(new Animation.AnimationListener(){
#Override
public void onAnimationStart(Animation arg0) {
}
#Override
public void onAnimationRepeat(Animation arg0) {
}
#Override
public void onAnimationEnd(Animation arg0) {
mMediaPlayer.start();
}
});
}
#Override
public void onBackPressed() {
super.onBackPressed();
mMediaPlayer.release();
mMediaPlayer=null;
}
#Override
public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, int height) {
mSurfaceTexture=surfaceTexture;
Surface surface = new Surface(surfaceTexture);
try {
mMediaPlayer= new MediaPlayer();
mMediaPlayer.setDataSource(VideoVieww.this, Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.sid));
mMediaPlayer.setSurface(surface);
mMediaPlayer.prepare();
mMediaPlayer.start();
// mMediaPlayer.setLooping(true);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
}
#Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
return false;
}
#Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
}
}
Any help would be greatly appreciated!!!

Android Media Player:setDataSource Failed

I try to create Media Player using Surface View. When I try to set the DataSource for the Media Player, I got the setDataSource Failed Exception. How to resolve it?
Code:
#Override
public void surfaceCreated(SurfaceHolder holder) {
try {
mediaPlayer.reset();
mediaPlayer.setDataSource(getApplicationContext(), Uri.parse("android.resource//"+getPackageName()+ "/raw/"+R.raw.androidvideo));
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
System.err.println(e.getMessage());
}
catch (Exception e)
{
e.printStackTrace();
Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_LONG).show();
System.err.println(e.getMessage());
}
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
mediaPlayer.release();
}
You forgot to add colon after resource.
mediaPlayer.setDataSource(getApplicationContext(), Uri.parse("android.resource://"+getPackageName()+ "/raw/"+R.raw.androidvideo));
mediaPlayer.setDataSource(getApplicationContext(), Uri.parse("android.resource://"+ getPackageName() + R.raw.androidvideo));
Try this

Exception mOnBufferingUpdateListener is null. Failed to send MEDIA_BUFFERING_UPDATE message

I am using the Surface view and want to play the video. But video is not display its giving error message multiple times "mOnBufferingUpdateListener is null. Failed to send MEDIA_BUFFERING_UPDATE message."
Please suggest me to solve this issue.
#SuppressWarnings("deprecation")
private void createSurface() {
try {
holder = surfaceView.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setDisplay(holder);
} catch(Exception e) {
Log.v("NB", "Exception at ::" + e.getMessage());
}
}
private void releaseMediaPlayer() {
if (mMediaPlayer != null) {
mMediaPlayer.stop();
mMediaPlayer.release();
mMediaPlayer = null;
}
}
private void playVideo() {
try {
if(videoPath!=null) {
mMediaPlayer.setDataSource(videoPath);
mMediaPlayer.prepare();
}
}
catch (IllegalArgumentException e)
{e.printStackTrace();}
catch (IllegalStateException e)
{e.printStackTrace();}
catch (IOException e)
{e.printStackTrace();}
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.start();
mMediaPlayer.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
releaseMediaPlayer();
//finish();
}
});
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
playVideo();
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
#Override
protected void onDestroy() {
super.onDestroy();
releaseMediaPlayer();
}
Thanks in advance.

Media player not working in Android version >=4.0

I am developing a simple radio streaming application to play radio using a URL. This application is working in all the versions except for V>=4.0
Do anyone have any Idea over this.
initializeUIElements();
StartPlaying();
private void initializeUIElements() {
buttonPlay = (ImageView) findViewById(R.id.buttonPlay);
buttonPlay.setOnClickListener(this);
playSeekBar=(ProgressBar)findViewById(R.id.progressBar1);
}
private void startPlaying() {
initializeMediaPlayer();
player.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
Log.i("on prepared", "on prepared");
mp.start();
}
});
player.setOnErrorListener(new OnErrorListener(){
public boolean onError(MediaPlayer arg0, int arg1,
int arg2) {
Toast.makeText(getApplicationContext(),"An error happened while preparing radio",Toast.LENGTH_LONG).show();
player.reset();
initializeMediaPlayer();
return false;
}
});
private void initializeMediaPlayer() {
player = new MediaPlayer();
player.reset();
try {
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setDataSource("http://***************");
player.prepareAsync();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
catch(Exception e)
{
e.printStackTrace();
}
player.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {
public void onBufferingUpdate(MediaPlayer mp, int percent) {
Log.i("percent", ""+percent);
if( (percent!=0)||(percent==100) )
{
playSeekBar.setVisibility(View.INVISIBLE);
buttonPlay.setVisibility(View.VISIBLE);
}
}
});
In Android version 4.0, It is giving an error as:
11-23 13:06:37.329: E/MediaPlayer(4011): Error (1,-2147483648)
and Player.bufferingUpdateListener() is not called. Here It is showing just that playseekbar revolving and revolving.. Please help on this.
It can be a problem with your particular device. Try running the same on 4.0 emulator.

Surface view getting problem

i am new to android, currently doing dynamic multimedia application. For accessing videos, included tappable SurfaceView. whenever playing videos from localhost server, only sound's can visible, there is no visual. i am not sure why this will happen, can anyone guide me how to visualize the video.
if (player == null) {
player = new MediaPlayer();
player.setScreenOnWhilePlaying(true);
} else {
player.stop();
player.reset();
}
player.setDataSource(file);
player.setDisplay(holder);
player.prepareAsync();
//player.setOnBufferingUpdateListener(this);
player.setOnCompletionListener(this);
player.setOnPreparedListener(this);
player.setOnVideoSizeChangedListener(this);
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
Here setDataSoruce (file) is used to get localhost url from existing class using intent.
Check out my Surface View - Playing video tutorial
public class VideoAssetActivity extends Activity implements
TextureView.SurfaceTextureListener {
// Log tag.
private static final String TAG = VideoAssetActivity.class.getName();
// Asset video file name.
private static final String FILE_NAME = "big_buck_bunny.mp4";
// MediaPlayer instance to control playback of video file.
private MediaPlayer mMediaPlayer;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.texture_video_simple);
initView();
}
private void initView() {
TextureView textureView = (TextureView) findViewById(R.id.textureView);
textureView.setSurfaceTextureListener(this);
}
#Override
protected void onDestroy() {
super.onDestroy();
if (mMediaPlayer != null) {
mMediaPlayer.stop();
mMediaPlayer.release();
mMediaPlayer = null;
}
}
#Override
public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int i, int i2) {
Surface surface = new Surface(surfaceTexture);
try {
AssetFileDescriptor afd = getAssets().openFd(FILE_NAME);
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
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());
}
}
#Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int i, int i2) {
}
#Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
return true;
}
#Override
public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {
}
}

Categories

Resources