MediaPlayer.setDisplay() in android - android

I am creating a video player using MediaPlayer class in android. I set a SurfaceHolder in MediaPlayer.setDisplay(), it shows video successfully but when this activity resumed again then no video is diplayed. So what is wrong ??
The Code is:
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
player = new MediaPlayer();
surface = (SurfaceView)findViewById(R.id.surface);
surface.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
VideoDemo.this.startActivity(new Intent(VideoDemo.this, AnotherActivity.class));//Video not displayed when return from this activity by pressing back button
}
});
holder = surface.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
try
{
String songName = Environment.getExternalStorageDirectory().toString()+File.separator+"1.mp4";
player.setDataSource(songName);
player.prepare();
player.start();
}
catch (Exception e)
{
e.printStackTrace();
}
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
{
player.setDisplay(holder);
}
#Override
public void surfaceCreated(SurfaceHolder holder)
{
Log.d(TAG, "surfaceCreated() is called ************ holder.getSurface() = "+holder.getSurface());
}
#Override
public void surfaceDestroyed(SurfaceHolder holder)
{
Log.d(TAG, "surfaceDestroyed() is called ************");
}

I have initialized media player again and now its working fine

Related

Android MediaPlayer with SurfaceView Fails on Prepare() Method

I am trying to build a video player using surface view and media player. My data source is an online video and I have enabled internet permission in manifest.
However, the video does not pay but throws a warning about prepare method without any hint at what the problem is like below:
W/System.err: at android.media.MediaPlayer._prepare(Native Method)
at android.media.MediaPlayer.prepare(MediaPlayer.java:1188)
at in.sashi.androidvideoplayer.MainActivity.surfaceCreated(MainActivity.java:64)
here's my code for this purpose:
public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback, MediaPlayer.OnPreparedListener, View.OnClickListener {
private static final String TAG = MainActivity.class.getSimpleName();
private SurfaceView surfaceView;
private ImageView playIV, backIV;
private TextView videoNameTV;
private MediaPlayer mediaPlayer;
private SurfaceHolder holder;
// private static final String VIDEO_URL = "https://www.youtube.com/watch?v=vEVd0QMjCc8";
private static final String VIDEO_URL = "http://mic.duytan.edu.vn:83/FINAL.mp4";
private static final String VIDEO_NAME = "FALL HARDER | BASKETBALL MOTIVATION";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
surfaceView = findViewById(R.id.surfsceView);
playIV = findViewById(R.id.playIV);
backIV = findViewById(R.id.backIV);
videoNameTV = findViewById(R.id.videoNameTV);
videoNameTV.setText(VIDEO_NAME);
backIV.setOnClickListener(this);
playIV.setOnClickListener(this);
holder = surfaceView.getHolder();
holder.addCallback(this);
surfaceView.setKeepScreenOn(true);
mediaPlayer = new MediaPlayer();
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
mediaPlayer.setDisplay(holder);
try {
mediaPlayer.setDataSource(VIDEO_URL);
mediaPlayer.prepare();
mediaPlayer.setOnPreparedListener(this);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.backIV:
onBackPressed();
break;
case R.id.playIV:
if (mediaPlayer.isPlaying()){
Log.d(TAG, "Pause Clicked");
mediaPlayer.pause();
playIV.setImageResource(R.drawable.play);
} else {
Log.d(TAG, "Play Clicked");
mediaPlayer.start();
playIV.setImageResource(R.drawable.pause);
}
break;
}
}
#Override
protected void onPause() {
super.onPause();
collect();
}
private void collect() {
if (mediaPlayer != null){
mediaPlayer.release();
mediaPlayer = null;
}
}
}
Can anyone help me to figure this out? Thanks.
Use mediaplayer object which is being defined globally.
#Override
public void onPrepared(MediaPlayer mp) {
mediaPlayer.start();
}
How about this?
#Override
public void surfaceCreated(SurfaceHolder holder) {
mediaPlayer.setDisplay(holder);
try {
mediaPlayer.setDataSource(VIDEO_URL);
mediaPlayer.setOnPreparedListener(this);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.prepareAsync();
} catch (IOException e) {
e.printStackTrace();
}
}

wowza cloud live streaming is not working

I'm using Wowza streaming cloud it works fine using HLS link on the web but an android device not working on the web view.can you anyone me how its work.
And rtsp live link also not working on android device
public class AnotherOne extends AppCompatActivity implements
SurfaceHolder.Callback, MediaPlayer.OnPreparedListener{
private String Downloadurl;
private int playminute;
private SurfaceView mSurfaceView;
private MediaPlayer mMediaPlayer;
private SurfaceHolder mSurfaceHolder;
ProgressBar p_bar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_another_one);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
View decorView = getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
Downloadurl = "https://7c6ad8.entrypoint.cloud.wowza.com/app-04be/ngrp:f23a3900_all/playlist.m3u8";
// Description = getIntent().getStringExtra("Description");
Log.e("kanish","Download Url inside player:"+Downloadurl);
mSurfaceView = (SurfaceView) findViewById(R.id.surfaceView);
mSurfaceHolder = mSurfaceView.getHolder();
mSurfaceHolder.addCallback(this);
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
// p_bar.setVisibility(View.VISIBLE);
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setDisplay(mSurfaceHolder);
try {
mMediaPlayer.setDataSource(Downloadurl);
mMediaPlayer.prepare();
mMediaPlayer.setOnPreparedListener(AnotherOne.this);
//mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
#Override
public void onBackPressed() {
super.onBackPressed();
}
#Override
public void onPrepared(MediaPlayer mp) {
//p_bar.setVisibility(View.GONE);
mMediaPlayer.start();
}
#Override
protected void onPause() {
super.onPause();
releaseMediaPlayer();
}
#Override
protected void onDestroy() {
super.onDestroy();
releaseMediaPlayer();
}
private void releaseMediaPlayer() {
if (mMediaPlayer != null) {
mMediaPlayer.release();
mMediaPlayer = null;
}
}
}
you need to declare a view to render video View and
You need to use Media Player Api Provided by Android to Stream m3u8.it is pretty Simple to play Live content follow this code.
public class PlayerActivity extends AppCompatActivity implements
SurfaceHolder.Callback, MediaPlayer.OnPreparedListener {
private String Downloadurl;
private int playminute;
private SurfaceView mSurfaceView;
private MediaPlayer mMediaPlayer;
private SurfaceHolder mSurfaceHolder;
ProgressBar p_bar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.player);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
View decorView = getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
Downloadurl = "YOUR M3U8 URL";
// Description = getIntent().getStringExtra("Description");
Log.e("kanish","Download Url inside player:"+Downloadurl);
mSurfaceView = (SurfaceView) findViewById(R.id.surface_view);
mSurfaceHolder = mSurfaceView.getHolder();
mSurfaceHolder.addCallback(PlayerActivity.this);
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
p_bar.setVisibility(View.VISIBLE);
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setDisplay(mSurfaceHolder);
try {
mMediaPlayer.setDataSource(Downloadurl);
mMediaPlayer.prepare();
mMediaPlayer.setOnPreparedListener(PlayerActivity.this);
//mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
#Override
public void onBackPressed() {
super.onBackPressed();
}
#Override
public void onPrepared(MediaPlayer mp) {
p_bar.setVisibility(View.GONE);
mMediaPlayer.start();
}
#Override
protected void onPause() {
super.onPause();
releaseMediaPlayer();
}
#Override
protected void onDestroy() {
super.onDestroy();
releaseMediaPlayer();
}
private void releaseMediaPlayer() {
if (mMediaPlayer != null) {
mMediaPlayer.release();
mMediaPlayer = null;
}
}
}
Let me Know if it helps.

MediaPlayer setLooping(true) doesn't work

I use MediaPlayer for displaying video. I need to loop it. I use native method setLooping(), but it doesn't work. Video finished and that's all. Here is my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_full_screen);
TextureView videoViewFullScreen = (TextureView) findViewById(R.id.video_screen);
path = getIntent().getExtras().getString(SettingsActivity.FILE_PATH);
cycle = getIntent().getExtras().getBoolean(SettingsActivity.CYCLE);
position = getIntent().getIntExtra(FullScreenActivity.POSITION, -1);
videoViewFullScreen.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
#Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
final Surface surf = new Surface(surface);
try {
mediaPlayerFullScreen = new MediaPlayer();
mediaPlayerFullScreen.setDataSource(path);
mediaPlayerFullScreen.setSurface(surf);
if (cycle) {
mediaPlayerFullScreen.setLooping(true);
}
mediaPlayerFullScreen.prepareAsync();
mediaPlayerFullScreen.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
if (position != -1) {
Log.d("Position", "Position Full " + position);
mediaPlayerFullScreen.seekTo(position);
}
mediaPlayer.start();
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
}
#Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
mediaPlayerFullScreen.stop();
mediaPlayerFullScreen.release();
mediaPlayerFullScreen = null;
return true;
}
#Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
}
});
}
#Override
public void onDestroy() {
super.onDestroy();
if (mediaPlayerFullScreen != null) {
mediaPlayerFullScreen.stop();
mediaPlayerFullScreen.release();
mediaPlayerFullScreen = null;
}
}
I looked all similar questions and tried all answers that find here, but none of them helped me.
I have Android version 4.4.2, Firmware version 4.5, Kernel version 3.3.0
Could someone help me? Is there another way to loop video?
I find replacement for setLooping(true), this works for me:
mediaPlayerFullScreen.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mediaPlayerFullScreen.reset();
try {
mediaPlayerFullScreen.setDataSource(path);
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayerFullScreen.setSurface(surf);
mediaPlayerFullScreen.prepareAsync();
mediaPlayerFullScreen.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
mediaPlayer.start();
}
});
}
});

MediaPlayer doesn't resume video after activity restart

I have a MediaPlayer object that uses a SurfaceHolder object as a surface. There is a button on top of the video that takes me out of the video to a website. When that happens, I pause the player with player.pause(). When I return from the website, I resume the player with player.start(). I know that the surface gets destroyed when the activity is not displayed anymore, and it gets recreated as soon as the activity is restarted. In my surfaceCreated(), I set the surface for the player again (since it no longer has a surface at that point), and then resume. However, the player simply restarts the video from the beginning.
I've tried commenting out the line that takes me to the website, just to see if pause/start works properly and resumes from last spot. It does. I'm not sure why this behaviour doesn't happen when I leave and re-enter the video activity though.
I also tried using the player.seekTo() call. There was no difference. In fact, when I disabled the button taking me to a site to just pausing the video, with the seekTo() call the video ALSO started from the beginning despite position being not 0.
The player object is the same all the way throughout.
Just because the surface is a new one on restart, it doesn't know or care of its contents, does it? The player should be managing that, right?
I'm out of ideas at this point. Can anyone please offer any tips?
UPDATE: So I threw together a quick app just to eliminate any other external factors. Here's the full code for the video class (other class is just an activity with a play button):
public class VideoPlayer extends Activity implements MediaPlayer.OnCompletionListener,
MediaPlayer.OnErrorListener, MediaPlayer.OnPreparedListener, MediaPlayer.OnSeekCompleteListener, MediaPlayer.OnVideoSizeChangedListener,
SurfaceHolder.Callback {
private MediaPlayer player;
private SurfaceHolder mSurfaceHolder;
private SurfaceView mSurfaceView;
private Button leaveVideoButton;
private boolean isPaused = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.video_layout);
leaveVideoButton = (Button) findViewById(R.id.go_to_web);
leaveVideoButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Uri uri = Uri.parse("http://www.google.com");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
pauseSteps();
startActivity(intent);
}
});
createPlayer();
createSurface();
}
private void createSurface() {
mSurfaceView = (SurfaceView) findViewById(R.id.surface);
mSurfaceHolder = mSurfaceView.getHolder();
mSurfaceHolder.addCallback(this);
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
private void createPlayer() {
player = new MediaPlayer();
player.setOnCompletionListener(this);
player.setOnErrorListener(this);
player.setOnPreparedListener(this);
player.setOnVideoSizeChangedListener(this);
player.setOnSeekCompleteListener(this);
}
private void pauseSteps() {
if(player.isPlaying()) {
player.pause();
isPaused = true;
}
}
private void playSteps() {
if(isPaused) {
isPaused = false;
player.start();
}
}
#Override
protected void onDestroy() {
super.onDestroy();
if (player.isPlaying()) {
player.stop();
}
player.reset();
player.release();
player = null;
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
player.setDisplay(holder);
if (!isPaused) {
try {
// player.setDataSource(path);
AssetFileDescriptor afd = getResources().openRawResourceFd(R.raw.video);
if (afd == null) return;
player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
afd.close();
player.prepare();
} catch (IOException e) {
e.printStackTrace();
}
} else {
playSteps();
}
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
#Override
public void onCompletion(MediaPlayer mp) {
}
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
return false;
}
#Override
public void onPrepared(MediaPlayer mp) {
player.start();
}
#Override
public void onSeekComplete(MediaPlayer mp) {
}
#Override
public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
}
}
UPDATE 2: So I tried a different video and it resumed just fine from the same spot. This must be some encoding issue.

Pausing MediaPlayer video and keeping screen visible

I was wondering if there was a way to pause the mediaplayer and keep the screen visible. What I mean by that is keep the current image on the screen as it is paused. What i've done is set up a timer that pauses the mediaplayer for a certain amount of time. As it is paused for that time I want current video to be visable. However, everytime I pause the Mediaplayer the screen goes black untill I call the Mediplayer.start() again. Is there anyway around this.
Thanks
Edit with code
public class FullImageActivity extends Activity implements SurfaceHolder.Callback, OnPreparedListener, OnErrorListener{
private static final String TAG = null;
MediaPlayer player;
SurfaceView surfaceview;
SurfaceHolder surfaceHolder;
int width;
int height;
String path;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.full_image);
Intent i = getIntent();
long id = i.getExtras().getLong("id");
path = i.getExtras().getString("videopath");
surfaceview = (SurfaceView)findViewById(R.id.surfaceview);
surfaceHolder = surfaceview.getHolder();
surfaceHolder.addCallback(this);
player = new MediaPlayer();
playVideo();
}
public void playVideo(){
try {
player.reset();
player.setOnErrorListener(this);
player.setDataSource(path);
player.setOnPreparedListener(this);
player.prepareAsync();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
protected void onPause() {
player.release();
Log.i(TAG, "Mediaplayer was relased");
super.onPause();
}
#Override
protected void onResume() {
playVideo();
super.onResume();
}
#Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
}
#Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
player.setDisplay(holder);
}
#Override
public void surfaceDestroyed(SurfaceHolder arg0) {
// TODO Auto-generated method stub
}
#Override
public void onPrepared(MediaPlayer p) {
width=player.getVideoWidth();
height=player.getVideoHeight();
if (width!=0 && height!=0) {
surfaceHolder.setFixedSize(width, height);
player.start();
player.pause();
Timer timer = new Timer();
timer.schedule(new TimerTask()
{
public void run()
{
player.start();
}
}, 3000);
}
}

Categories

Resources