I want to show my captured video in another activity but the problem is when I play that video, It shows in landscape mode even I want to show portrait only.
At screenshot you can see the captured video plays in landscape but it should play in portrait mode.
Captured Screen :
public class AndroidVideoViewExample extends Activity {
private VideoView myVideoView;
private int position = 0;
private ProgressDialog progressDialog;
private MediaController mediaControls;
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the layout from video_main.xml
setContentView(R.layout.videodemo);
if (mediaControls == null) {
mediaControls = new MediaController(AndroidVideoViewExample.this);
}
// Find your VideoView in your video_main.xml layout
myVideoView = (VideoView) findViewById(R.id.video_view);
// Create a progressbar
progressDialog = new ProgressDialog(AndroidVideoViewExample.this);
// Set progressbar title
progressDialog.setTitle("JavaCodeGeeks Android Video View Example");
// Set progressbar message
progressDialog.setMessage("Loading...");
progressDialog.setCancelable(false);
// Show progressbar
progressDialog.show();
try {
myVideoView.setMediaController(mediaControls);
myVideoView.setVideoURI(Uri.parse(android.os.Environment.getExternalStorageDirectory()+AppConstants.GAME_IMAGE_DIR+"/myvideo1.mp4"));
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
myVideoView.requestFocus();
myVideoView.setOnPreparedListener(new OnPreparedListener() {
// Close the progress bar and play the video
public void onPrepared(MediaPlayer mp) {
progressDialog.dismiss();
myVideoView.seekTo(position);
if (position == 0) {
myVideoView.start();
} else {
myVideoView.pause();
}
}
});
}
}
And Xml file is :
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_gravity="center"
android:layout_height="fill_parent" >
<VideoView
android:id="#+id/video_view"
android:layout_width="720px"
android:layout_height="720px"
android:layout_gravity="center|center_vertical" />
</FrameLayout>
Related
The problem is video is not play at all , but when i use http://www.youtubemaza.com/files/data/366/Tom%20And%20Jerry%20055%20Casanova%20Cat%20(1951).mp4 (example) the video player works good.
I'm using json to get url
https://api.vid.me/videos/featured?limit=1
(Current Url https://api.vid.me/video/18175470/stream?format=dash)
public class MediaPlayerActivity extends AppCompatActivity {
private VideoView videoView;
private int position = 0;
private MediaController mediaController;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_player);
videoView = (VideoView) findViewById(R.id.videoView);
videoView.setVisibility(View.VISIBLE);
// Set the media controller buttons
if (mediaController == null) {
mediaController = new MediaController(MediaPlayerActivity.this);
// Set the videoView that acts as the anchor for the MediaController.
mediaController.setAnchorView(videoView);
// Set MediaController for VideoView
videoView.setMediaController(mediaController);
}
try {
String videoUrl="https://api.vid.me/video/18175470/stream?format=dash"; // dash
Uri video = Uri.parse(videoUrl);
videoView.setVideoURI(video);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
videoView.requestFocus();
// When the video file ready for playback.
videoView.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mediaPlayer) {
videoView.seekTo(position);
if (position == 0) {
videoView.start();
}
// When video Screen change size.
mediaPlayer.setOnVideoSizeChangedListener(new MediaPlayer.OnVideoSizeChangedListener() {
#Override
public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
// Re-Set the videoView that acts as the anchor for the MediaController
mediaController.setAnchorView(videoView);
}
});
}
});
}
// When you change direction of phone, this method will be called.
// It store the state of video (Current position)
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
// Store current position.
savedInstanceState.putInt("CurrentPosition", videoView.getCurrentPosition());
videoView.pause();
}
// After rotating the phone. This method is called.
#Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// Get saved position.
position = savedInstanceState.getInt("CurrentPosition");
videoView.seekTo(position);
}
}
xml file
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--<com.google.android.exoplayer2.ui.SimpleExoPlayerView
android:id="#+id/videoView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>-->
<SurfaceView
android:id="#+id/surface_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"/>
<VideoView
android:id="#+id/videoView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"/>
</FrameLayout>
The only way I found was to use a third party library (however this one is done by Google so not sure what you'd call it really...)
http://google.github.io/ExoPlayer/
It has very good documentation and even sample code on how to use it with DASH video sources!
I have a VideoView which is used to play live stream. I am not able to find where the problem occurs.
In the beginning, I used RTMP to play the live stream using Vitamio which resulted in playing the audio leaving the screen blank. I checked out with the RTMP link, it works fine with a website. I surfed a lot about this, but I did not find any solution.
So now, I switched to HTTP to play the live stream which also results in the same problem (i.e audio playing fine but video is blank).
I am expecting a solution for either RTMP or HTTP.
Any Suggestions???
UPDATE 1: I have found a problem with the link. I used VLC Media Player to check whether my RTMP and HTTP link is working fine or not. The RTMP link works fine whereas the problem was with the HTTP link. The HTTP link plays only the audio.
On the other hand, having the RTMP link working fine, it doesn't solve the problem when using Vitamio. Any Suggestions for RTMP??
Here is my code:
public class ITVLiveStreamActivity extends Activity {
private VideoView liveVideoView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_itvlive_stream);
liveVideoView = (VideoView) findViewById(R.id.liveVideoView);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(liveVideoView);
Uri uri = Uri.parse(getIntent().getStringExtra("rtmp://61.16.143.170:1935/live/7khh-8fhu-vxd3-8fuw"));
liveVideoView.setVideoURI(uri);
liveVideoView.setMediaController(mediaController);
liveVideoView.requestFocus();
liveVideoView.start();
}
}
UPDATE 2:
Here is my code using Vitamio:
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import io.vov.vitamio.LibsChecker;
import io.vov.vitamio.MediaPlayer;
import io.vov.vitamio.widget.MediaController;
import io.vov.vitamio.widget.VideoView;
public class ITVLiveStreamActivity extends Activity {
private String pathToFileOrUrl="rtmp://61.16.143.170:1935/live/7khh-8fhu-vxd3-8fuw";
private VideoView mVideoView;
private ProgressDialog progDailog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(!LibsChecker.checkVitamioLibs(this))
return;
setContentView(R.layout.activity_itvlive_stream);
mVideoView = (VideoView)findViewById(R.id.vitamio_videoView);
progDailog = new ProgressDialog(this);
progDailog.setCancelable(false);
progDailog.setMessage(getResources().getString(R.string.loading));
progDailog.show();
mVideoView.setVideoPath(pathToFileOrUrl);
mVideoView.setMediaController(new MediaController(this));
mVideoView.requestFocus();
mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
progDailog.dismiss();
mediaPlayer.setPlaybackSpeed(1.0f);
}
});
}
}
I am getting the same result (i.e) Audio is playing but video is blank.
Here I have added the screenshot
You can create an activity like this to open default Video View
public class VideoViewActivity extends Activity {
// Declare variables
ProgressDialog pDialog;
VideoView videoview;
// Insert your Video URL
// String VideoURL; /*= "http://www.androidbegin.com/tutorial/AndroidCommercial.3gp";*/
String VideoURL;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the layout from video_main.xml
setContentView(R.layout.video_view);
// Find your VideoView in your video_main.xml layout
videoview = (VideoView) findViewById(R.id.VideoView);
// Execute StreamVideo AsyncTask
VideoURL = getIntent().getExtras().getString(Constants.LINK);
// Log.v("video", VideoURL);
// Create a progressbar
pDialog = new ProgressDialog(VideoViewActivity.this);
// Set progressbar title
//pDialog.setTitle("Android Video Streaming Tutorial");
// Set progressbar message
pDialog.setMessage("Buffering...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
// Show progressbar
pDialog.show();
try {
// Start the MediaController
MediaController mediacontroller = new MediaController(
VideoViewActivity.this);
mediacontroller.setAnchorView(videoview);
// Get the URL from String VideoURL
Uri video = Uri.parse(VideoURL);
videoview.setMediaController(mediacontroller);
videoview.setVideoURI(video);
} catch (Exception e) {
// Log.e("Error", e.getMessage());
pDialog.dismiss();
e.printStackTrace();
} finally {
videoview.setOnErrorListener(new OnErrorListener() {
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
CommonUtilities.showToast(VideoViewActivity.this, "Video Format not supported by device.");
VideoViewActivity.this.finish();
return true;
}
});
}
videoview.requestFocus();
videoview.setOnPreparedListener(new OnPreparedListener() {
// Close the progress bar and play the video
public void onPrepared(MediaPlayer mp) {
pDialog.dismiss();
videoview.start();
}
});
}
}
hi i have done this and its working fine here is code
public class PlayingLiveStream extends Activity {
VideoView vvmyliveplaying;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_playinglivestream);
vvmyliveplaying = (VideoView) findViewById(R.id.vvmyliveplaying);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(vvmyliveplaying);
Uri uri = Uri.parse(getIntent().getStringExtra("url_play"));
vvmyliveplaying.setVideoURI(uri);
vvmyliveplaying.setMediaController(mediaController);
vvmyliveplaying.requestFocus();
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
try {
vvmyliveplaying.pause();
finish();
} catch (Exception e) {
}
}
return false;
}
}
<VideoView
android:id="#+id/vvmyliveplaying"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Edited Answer
I have tested streaming on vitamio library demo sample and its working fine.
public class VideoViewDemo extends Activity {
private VideoView mVideoView;
private String pathToFileOrUrl ="rtmp://61.16.143.170:1935/live/7khh-8fhu-vxd3-8fuw";
private ProgressDialog progDailog;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
if (!LibsChecker.checkVitamioLibs(this))
return;
setContentView(R.layout.videoview);
mVideoView = (VideoView) findViewById(R.id.surface_view);
progDailog = new ProgressDialog(this);
progDailog.setCancelable(false);
progDailog.setMessage("Please wait");
progDailog.show();
mVideoView.setVideoPath(pathToFileOrUrl);
mVideoView.setMediaController(new MediaController(this));
mVideoView.requestFocus();
mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
progDailog.dismiss();
mediaPlayer.setPlaybackSpeed(1.0f);
}
});
}
}
and xml layout,
<io.vov.vitamio.widget.VideoView
android:id="#+id/surface_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Now i'm sure code work well.
Thanks hope this will help you .
I am using videoview, in the onPrepared method and it returns the media player height and width. Then I check if the width of the video is greater than the height and rotate the screen. The problem is that if I rotate the screen orientation, it again calls the onCreate method and then recalls all of the code so it takes time to initialize the video again.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the layout from video_main.xml
setContentView(R.layout.play_video_activity);
// Insert your Video URL
String VideoURL = "http://www.androidbegin.com/tutorial/AndroidCommercial.3gp";
// Find your VideoView in your video_main.xml layout
final VideoView videoview = (VideoView) findViewById(R.id.VideoView);
// Execute StreamVideo AsyncTask
// Create a progressbar
final ProgressDialog pDialog;
pDialog = new ProgressDialog(PlayVideoActivity.this);
// Set progressbar title
pDialog.setTitle("Android Video Streaming Tutorial");
// Set progressbar message
pDialog.setMessage("Buffering...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
// Show progressbar
pDialog.show();
try {
// Start the MediaController
MediaController mediacontroller = new MediaController(
PlayVideoActivity.this);
mediacontroller.setAnchorView(videoview);
// Get the URL from String VideoURL
Uri video = Uri.parse(VideoURL);
videoview.setMediaController(mediacontroller);
videoview.setVideoURI(video);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
videoview.requestFocus();
videoview.setOnPreparedListener(new OnPreparedListener() {
// Close the progress bar and play the video
public void onPrepared(MediaPlayer mp) {
pDialog.dismiss();
videoview.start();
if(mp.getVideoWidth()> mp.getVideoHeight())
{
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
}
});
}
Add android:configChanges="orientation" to your activity in manifest file
Declare variables in your activity
boolean orienChanged = false;
int lastOrientation = 0;
Override method
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
int orientation = newConfig.orientation;
if(orientation != lastOrientation){
orienChanged = true;
lastOrientation = orientation ;
}
}
Check the variable orienChanged in onCreate() for only initial time it will be false,otherwise it must be true.
Initialize new configuration oncreate... and make newConfigOD valiable global.
Configuration newConfigOD = getResources().getConfiguration();
Then override this method..
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if(videoview.getVideoWidth()> videoview.getVideoHeight()) {
if (newConfigOD.orientation != Configuration.ORIENTATION_LANDSCAPE) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
} else {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}
Try this... this should work as per your requirement. And remove this if condition from onCreate()
Auto set orientation according to video scale.
Implement addVideoListener()
videoPlayer.addVideoListener(new VideoListener() {
#Override
public void onVideoSizeChanged(int width, int height, int
unappliedRotationDegrees, float pixelWidthHeightRatio) {
}
#Override
public void onRenderedFirstFrame() {}
});
According to height & width set conditions
if(width>height){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
if(height>width){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
Your final objective would be like
videoPlayer.addVideoListener(new VideoListener() {
#Override
public void onVideoSizeChanged(int width, int height, int
unappliedRotationDegrees, float pixelWidthHeightRatio) {
if(width>height){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
if(height>width){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}
#Override
public void onRenderedFirstFrame() {}
});
I didn't use a real device, I was working on emulator. (Problem Solved)
I am developing a live stream video application. I have so many live stream URLs which have *.m3u8 postfix.
When I start the app, I can hear only sound but no video displaying. Video display area is always dark.
I am waiting for your helpful suggestions to solve my problem.
Regards.
Some of my code:
public class MainActivity extends Activity {
private static ProgressDialog progressDialog;
String videourl = "URL/playlist.m3u8";
VideoView videoView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
videoView = (VideoView) findViewById(R.id.video_View);
progressDialog = ProgressDialog.show(MainActivity.this, "", "Buffering video...", true);
progressDialog.setCancelable(false);
PlayVideo();
}
private void PlayVideo() {
try {
getWindow().setFormat(PixelFormat.TRANSLUCENT);
MediaController mediaController = new MediaController(MainActivity.this);
mediaController.setAnchorView(videoView);
Uri video = Uri.parse(videourl);
videoView.setMediaController(mediaController);
videoView.setVideoURI(video);
videoView.requestFocus();
videoView.setVisibility(View.VISIBLE);
videoView.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
progressDialog.dismiss();
videoView.start();
}
});
} catch (Exception e) {
progressDialog.dismiss();
System.out.println("Video Play Error :" + e.toString());
finish();
}
}
}
layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<VideoView
android:id="#+id/video_View"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</VideoView>
</LinearLayout>
How to Play mp4 Video in Android Emulator Using Remote URL ? I used following code but this code give me error "Sorry, this video cannot be played".
07-05 16:58:19.525: INFO/AwesomePlayer(34): mConnectingDataSource->connect() returned -1007
07-05 16:58:19.525: ERROR/MediaPlayer(1242): error (1, -1007)
07-05 16:58:19.525: ERROR/MediaPlayer(1242): Error (1,-1007)
07-05 16:58:19.525: DEBUG/VideoView(1242): Error: 1,-1007
My Code is:-
public class VideoPlayerController extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
VideoView videoView = (VideoView) findViewById(R.id.VideoView);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
String Video="http://s509.photobucket.com/albums/s338/eveanthony/?action=view¤t=Video013.mp4";
videoView.setMediaController(mediaController);
videoView.setVideoURI(Uri.parse(Video));
videoView.start();
}
}
You need to execute the app on original device rather than emulator since it does not supports playing video files. In rare cases it may but it really depends upon your system configurations.
Android 4.1.2 version seems to play mp4 video in the emulator in youtube app, not elsewhere. I tested it. Both Intel and non-Intel version work. 4.0.3 did not play them.
private VideoView myVideoView;
private int position = 0;
private ProgressDialog progressDialog;
private MediaController mediaControls;
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
private static final String Videos_URL = "*Your_URI*";
// Get the layout from video_main.xml
setContentView(R.layout.activity_main);
// Find your VideoView in your video_main.xml layout
myVideoView = (VideoView) findViewById(R.id.videoView);
// Create a progressbar
progressDialog = new ProgressDialog(this);
// Set progressbar title
progressDialog.setTitle("Anything u Want");
// Set progressbar message
progressDialog.setMessage("Loading...");
progressDialog.setCancelable(false);
// Show progressbar
progressDialog.show();
try {
Uri video = Uri.parse(Videos_URL);
myVideoView.setVideoURI(video);
myVideoView.setMediaController(mediaControls);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
myVideoView.requestFocus();
myVideoView.setOnPreparedListener(new OnPreparedListener() {
// Close the progress bar and play the video
public void onPrepared(MediaPlayer mp) {
progressDialog.dismiss();
myVideoView.seekTo(position);
if (position == 0) {
myVideoView.start();
} else {
myVideoView.pause();
}
}
});
}
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putInt("Position", myVideoView.getCurrentPosition());
myVideoView.pause();
}
#Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
position = savedInstanceState.getInt("Position");
myVideoView.seekTo(position);
}
}