Android video is rotated when play from web server - android

I'm playing video from web server, the video is playing on videoview nicely, but some samsung mobile playing the video on 90 degree rotation. But the same video playing without ration on others devices.
public void playVideo(String videoPath) {
try {
vidFull.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.setOnInfoListener(new MediaPlayer.OnInfoListener() {
#Override
public boolean onInfo(MediaPlayer mp, int what, int extra) {
switch (what) {
case MediaPlayer.MEDIA_INFO_BUFFERING_START:
progressBar.setVisibility(View.VISIBLE);
break;
case MediaPlayer.MEDIA_INFO_BUFFERING_END:
progressBar.setVisibility(View.GONE);
break;
}
return false;
}
});
}
});
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(vidFull);
vidFull.setMediaController(mediaController);
vidFull.setVideoPath(videoPath);
vidFull.requestFocus();
vidFull.start();
videoArrayLength = videoArrayLength -1;
vidpointer = 1;
//Goutam 17.03.2016
vidFull.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
logCollection.setLogERed(""+videoArrayLength);
try {
if (videoArrayLength > 0){
MediaController mediaController = new MediaController(FullPost.this);
mediaController.setAnchorView(vidFull);
vidFull.setMediaController(mediaController);
//vidFull.setVideoPath("http://www.sample-videos.com/video/mp4/480/big_buck_bunny_480p_1mb.mp4");
logCollection.setLogERed(basevideourl+videoArray.optJSONObject(vidpointer).getString("vdLink"));
vidFull.setVideoPath(basevideourl + videoArray.optJSONObject(vidpointer).getString("vdLink"));
vidFull.requestFocus();
vidFull.start();
videoArrayLength = videoArrayLength - 1;
vidpointer = vidpointer + 1;
}else {
logCollection.showToastLong("No more video to play.");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
} catch (Exception e) {
// TODO: handle exception
Toast.makeText(this, "Error connecting", Toast.LENGTH_SHORT).show();
}
}
Please help, thanks in advance.

I think you telling about orientation change. You can handle this by setting screenOrientation to activity in AndroidManifest.
<activity android:name=".Main2Activity"
android:screenOrientation="portrait">
</activity>

Related

Stream M3u url in Android

I am trying to stream m3u from url. The below code is working with an MP3 url but no with an m3u url.
This is how I initialize the player
private void initializeMediaPlayer() {
mPlayer = new MediaPlayer();
try {
mPlayer.setDataSource(url);
} catch (IllegalArgumentException | IllegalStateException | IOException e) {
e.printStackTrace();
}
mPlayer.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() {
public void onBufferingUpdate(MediaPlayer mp, int percent) {
// Log.i("Buffering", "" + percent);
}
});
}
And this is my listener for play button
radioPlayButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
radioPlayButton.setBackgroundResource(R.drawable.ic_pause);
mPlayer.prepareAsync();
mPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
mPlayer.start();
}
});
}
});
I am testing in an Android 4.1.2 API 16 device and I am getting
E/MediaPlayer: Error (1,-2147483648)

RTSP live video streaming

I'm building an application to live stream video from the raspberry pi to my android device. I have three functional ways of playing this stream:
rtsp://media.smart-streaming.com/mytest/mp4:sample_phone_150k.mp4
Media Player + SurfaceView
VideoView
Intent.ACTION_VIEW
The problem is the application plays the above^ stream but doesn't play mine (rtsp://192.168.1.143:8554/vid.mp4) ... but VLC does.
I have tried streaming with VLC and with LIVE555 in multiple video formats and I've also tried playing a video that was recorded on the phone.
Here is my code:
//Stream methods 0 = MediaPlayer & SurfaceView, 1 = VideoView, 2 = Native Video Player
final int STREAM_USING = 0;
//MediaPlayer on surfaceView
String streamPath = "rtsp://192.168.1.143:8554/vid.mp4";//"rtsp://media.smart-streaming.com/mytest/mp4:sample_phone_150k.mp4";//"rtsp://192.168.1.143:8554/vid.mp4";//;"rtp://239.255.0.1:5004/";
Uri streamUri;
private MediaPlayer mediaPlayer;
private SurfaceView surfaceView;
private SurfaceHolder surfaceHolder;
//VideoView
VideoView videoView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//MediaPlayer
switch (STREAM_USING) {
case 0: {
surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.setFixedSize(800, 480);
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
mediaPlayer = new MediaPlayer();
mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
if (what == MediaPlayer.MEDIA_ERROR_SERVER_DIED) {
mediaPlayer.release();
mediaPlayer = new MediaPlayer();
//mediaPlayer.stop();
play();
}
return false;
}
});
mediaPlayer.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() {
#Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
Toast.makeText(getApplicationContext(), "BUFF : " + percent, Toast.LENGTH_SHORT).show();
if (!mediaPlayer.isPlaying()) {
mediaPlayer.start();
}
}
});
//Intent i = new Intent(Intent.ACTION_GET_CONTENT);
//i.setType("video/*");
//startActivityForResult(i, 1234);
streamUri = Uri.parse(streamPath);
play();
break;
}
case 1: {
videoView = (VideoView) findViewById(R.id.videoView);
videoView.setVideoURI(Uri.parse(streamPath));
MediaController mediaController = new MediaController(this);
//mediaController.setAnchorView(videoView);
videoView.setMediaController(mediaController);
videoView.requestFocus();
try {
videoView.start();
}
catch (SecurityException se) {
Log.e("SE", se.getMessage());
se.printStackTrace();
}
break;
}
case 2: {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(streamPath));
startActivity(intent);
break;
}
default: {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(streamPath));
startActivity(intent);
}
}
}
private void play() {
try {
//final FileInputStream fis = new FileInputStream(streamPath);
mediaPlayer.stop();
mediaPlayer.reset();
mediaPlayer.setDataSource(MainActivity.this, streamUri);
//mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.prepareAsync();
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
//mediaPlayer.reset();
mediaPlayer.start();
}
});
} catch (SecurityException se) {
Log.e("SE", se.getMessage());
se.printStackTrace();
} catch (IOException ie) {
ie.printStackTrace();
}
}
Any help is appreciated, I've been trying for a week to get this to work with no success :(
You already added two ways just i want to add bit modification. I have done streaming ralalted task.I have used rtsp with wowza.Few ways i'll let you know
try it once and let me know if you got issue.If your streaming in vlc working fine then something problem with app side. If these methods are not working then try with different phone. It also help you.
1. Try with videoview
vvVideoPlay = (VideoView) findViewById(R.id.vvVideoPlay);
MediaController mediaController = new MediaController(this);
String videoUrl = "rtsp://192.168.1.143:8554/vid.mp4";
mediaController.setAnchorView(vvVideoPlay);
Uri uri = Uri.parse(videoUrl);
vvVideoPlay.setVideoURI(uri);
vvVideoPlay.setMediaController(mediaController);
vvVideoPlay.requestFocus();
vvVideoPlay.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
pdialog.dismiss();
mp.setOnVideoSizeChangedListener(new OnVideoSizeChangedListener() {
#Override
public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
mp.start();
}
});
}
});
2. Try with direct your phone player
startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse("rtsp://192.168.1.143:8554/vid.mp4")));
3. Third way try to with this library with custom player in your app.
Step1. Add it to your gradle
compile "fm.jiecao:jiecaovideoplayer:4.7.0"
Step2. Add it as your video play in xml layout.
<fm.jiecao.jcvideoplayer_lib.JCVideoPlayerStandard
android:id="#+id/videoPlayer"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Step 3. Check from here how to use this library in your class,
public class PlayVideoActivity extends BaseActivity {
#BindView(R.id.videoPlayer)
JCVideoPlayerStandard mVideoPlayer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
restoreFromIntent(getIntent());
}
#Override
public int getLayout() {
return R.layout.activity_play_video;
}
// get video path from intent and play the video here
private void restoreFromIntent(Intent intent) {
mVideoPlayer.setUp("rtsp://192.168.1.143:8554/vid.mp4"
, JCVideoPlayerStandard.SCREEN_LAYOUT_LIST, "");
}
#Override
public void onBackPressed() {
if (JCVideoPlayer.backPress()) {
return;
}
super.onBackPressed();
}
#Override
protected void onPause() {
super.onPause();
JCVideoPlayer.releaseAllVideos();
}
}
Hope this will help you to fix your problem.Thanks

how to listen to the radio stream instantly without waiting in Android?

I always use this method to read the flow of streaming radio stations.
play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(play.getTag().equals("Start")){
notificationBar("Click To Stop Radio",nom);
play.setEnabled(false);
startPlaying(image, nom, urlradio);
}else{
play.setTag("Start");
stopPlaying(urlradio);
play.setImageResource(R.drawable.play);
}
}
});
OnAudioFocusChangeListener focusChangeListener =
new OnAudioFocusChangeListener() {
public void onAudioFocusChange(int focusChange) {
switch (focusChange) {
case (AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) :
// Lower the volume while ducking.
player.setVolume(0.2f, 0.2f);
break;
case (AudioManager.AUDIOFOCUS_LOSS_TRANSIENT) :
player.pause();
break;
case (AudioManager.AUDIOFOCUS_LOSS) :
// player.stop();
break;
case (AudioManager.AUDIOFOCUS_GAIN) :
// Return the volume to normal and resume if paused.
player.setVolume(1f, 1f);
player.start();
break;
default: break;
}
}
};
// Request audio focus for playback
int result = audioManager.requestAudioFocus(focusChangeListener,
// Use the music stream.
AudioManager.STREAM_MUSIC,
// Request permanent focus.
AudioManager.AUDIOFOCUS_GAIN);
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
// other app had stopped playing song now , so u can do u stuff now .
}
private void startPlaying(String image,String nom,String urlradio) {
initializeMediaPlayer(urlradio);
playSeekBar.setVisibility(View.VISIBLE);
player.prepareAsync();
player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
player.start();
playSeekBar.setVisibility(View.INVISIBLE);
play.setEnabled(true);
play.setImageResource(R.drawable.stop);
play.setTag("Stop");
}
});
}
private void stopPlaying(String urlradio) {
if (player != null) {
player.stop();
player.release();
player = null;
}
notificationClose(1);
playSeekBar.setVisibility(View.INVISIBLE);
}
private void initializeMediaPlayer(String radio) {
player = new MediaPlayer();
player.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
try {
player.setDataSource(radio);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
player.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() {
public void onBufferingUpdate(MediaPlayer mp, int percent) {
playSeekBar.setSecondaryProgress(percent);
Log.i("Buffering", "" + percent);
}
});
}
But in some radio the stream I expected 20 to 30 seconds to be listen and I saw in some application that the flow passes instantaneous .
how to listen to the radio stream instantly without waiting?

videoView.setOnPreparedListener, videoView.setOnCompletionListener and videoView.setOnErrorListener not getting called

Here is my code snippet where I want to play a video coming from server
private void PlayVideo() {
try {
getWindow().setFormat(PixelFormat.TRANSLUCENT);
MediaController mediaController = new MediaController(VideoActivity.this);
mediaController.setAnchorView(videoView);
Uri video = Uri.parse(videoPath);
videoView.setMediaController(mediaController);
videoView.setVideoURI(video);
videoView.requestFocus();
videoPlayer.removeAllViews();
videoPlayer.setVisibility(View.GONE);
videoView.setVisibility(View.VISIBLE);
videoView.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
dismissProgressDialog();
videoView.bringToFront();
videoView.setFocusable(true);
videoView.start();
contentStarted = true;
}
});
videoView.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
contentStarted = false;
}
});
videoView.setOnErrorListener(new OnErrorListener() {
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
dismissProgressDialog();
Intent in = new Intent();
setResult(1, in);
finish();
return false;
}
});
} catch (Exception e) {
dismissProgressDialog();
finish();
}
}
The ProgressDialog is dismissed only in videoView.setOnPreparedListener and videoView.setOnErrorListener. But the ProgressDialog is not getting dismissed and video is not getting played. I tried to put Logs and see, Logs are printed upto just before videoView.setOnPreparedListener and after that no Logs are displayed. Listeners are not getting registered I guess.
Any help is appreciated.
Thanks in advance.
EDIT:
'M trying to stream a live video, if video is availbale it should go to videoView.setOnPreparedListener and should play the video. If Live is not available(i.e, Video will be live after some time) then it should go to videoView.setOnErrorListener and return to previous Activity with result "1"
and
Video is streamed over RTSP
After a lot of research and with the help of Preethi Rao I got to know that the fault was in the URL.
The URL might have the video (if it is time for Live Video) and might not have video (if Live Video will be streaming after some time). If I'm trying to stream video when Live Video is not available then the Listeners are not getting fired. If Live Video is availbale, Listeners are getting fired. So, videoView.setOnPreparedListener and videoView.setOnErrorListener are not getting attached.
I wrote a handler to run for 60 seconds and if no Listeners are attached I'm just returning to previous Activity.
Here's the code:
private void PlayVideo() {
try {
isListenerAttached = false;
getWindow().setFormat(PixelFormat.TRANSLUCENT);
MediaController mediaController = new MediaController(VideoActivity.this);
mediaController.setAnchorView(videoView);
Uri video = Uri.parse(videoPath);
videoView.setMediaController(mediaController);
videoView.setVideoURI(video);
videoView.requestFocus();
videoPlayer.removeAllViews();
videoPlayer.setVisibility(View.GONE);
videoView.setVisibility(View.VISIBLE);
// Using this Handler to revert to previous Activity when the Video View is not attached to Listeners
// As the Buffering Video dialog doesn't get dismissed if Video View is not attached to Listeners
runOnUiThread(new Runnable() {
#Override
public void run() {
new Handler().postDelayed(new Runnable(){
#Override
public void run() {
if(!isListenerAttached){
dismissProgressDialog();
Intent in = new Intent();
setResult(1, in);
finish();
}
}
}, WAIT_TIME);
}
});
videoView.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
isListenerAttached = true;
dismissProgressDialog();
videoView.bringToFront();
videoView.setFocusable(true);
videoView.start();
contentStarted = true;
}
});
videoView.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
contentStarted = false;
}
});
videoView.setOnErrorListener(new OnErrorListener() {
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
isListenerAttached = true;
dismissProgressDialog();
Intent in = new Intent();
setResult(1, in);
finish();
return false;
}
});
} catch (Exception e) {
dismissProgressDialog();
finish();
}
}

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.

Categories

Resources