public class MergeVideo extends Activity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myfirstpage);
VideoView myVideoView = (VideoView)findViewById(R.id.myvideoview);
myVideoView.setVideoPath("/storage/emulated/0/Android/data/com.example.android.camera2video/files/a.mp4");
myVideoView.setMediaController(new MediaController(this));
myVideoView.requestFocus();
myVideoView.seekTo(6000);
myVideoView.start();
}
}
this is my code for play video . i have one main video file 19 second length i want to play video from 6 second to 12 second i am able to start video from 6 second . i don't know how to stop video play on 12 sec please suggest me how implement this .
Not tested this, but it should work in your case.
private VideoView mVideoView;
private boolean mShouldStop = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mVideoView = (VideoView) findViewById(R.id.myvideoview);
mVideoView.setVideoPath("/storage/emulated/0/Android/data/com.example.android.camera2video/files/a.mp4");
mVideoView.setMediaController(new MediaController(this));
mVideoView.requestFocus();
mVideoView.seekTo(6000);
mVideoView.start();
trackProgress();
}
private void trackProgress() {
new Thread(new Runnable() {
#Override
public void run() {
while (!mShouldStop) {
if (mVideoView != null && mVideoView.isPlaying()) {
if (mVideoView.getCurrentPosition() >= 12000) {
mVideoView.stopPlayback();
mShouldStop = true;
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}).start();
}
Since the VideoView documentation doesn't have hooks for this, let's take a step back: what do we want to do?
Play the video (already done)
After x amount of time, stop the video (to do)
Implement a timer
We want to play a video for an amount of time, then stop the video. Why not implement a Timer (see documentation)?
On this timer, you can schedule a task to be performed.
One caveat: Don't forget to cancel() the timer. Otherwise it'll keep calling the method to pause/stop the video.
Related
I want to play a music file which will be retrieved from a cloud storage service (such as Google drive or Mega.nz). I shared the file so anyone who have the link can access to it.
I'm using MediaPlayer class to handle this playback. So when I tried a direct link something like this, it worked well. But when I tried with a link from Google Drive, such as this, it didn't work.
Here is some code which I used to play the music file:
MusicPlayerFragment class
public class MusicPlayerFragment extends Fragment {
private MusicPlayer musicPlayer;
boolean isPlaying;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View layout = inflater.inflate(R.layout.fragment_music_player, container, false);
final ImageButton buttonPlayPause = layout.findViewById(R.id.button_play_pause);
buttonPlayPause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!isPlaying) {
musicPlayer.play();
buttonPlayPause.setImageResource(R.drawable.baseline_pause_24);
isPlaying = true;
}
else {
musicPlayer.pause();
buttonPlayPause.setImageResource(R.drawable.baseline_play_arrow_24);
isPlaying = false;
}
}
});
return layout;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
musicPlayer = new MusicPlayer();
musicPlayer.loadMedia("http://ssaurel.com/tmp/mymusic.mp3"); //work on physical device
//musicPlayer.loadMedia("https://drive.google.com/file/d/1Tj0a5f4dUMNnlPILr3vZzpPOwHKsP3Va/view?usp=sharing"); //doesn't work at all
}
}
MusicPlayer class
public class MusicPlayer{
private MediaPlayer mediaPlayer;
public void loadMedia(String url) {
if (mediaPlayer == null) {
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
try {
mediaPlayer.setDataSource(url);
mediaPlayer.prepareAsync();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void play() {
if (mediaPlayer != null && !mediaPlayer.isPlaying()) {
mediaPlayer.start();
}
}
public void pause() {
if (mediaPlayer != null && mediaPlayer.isPlaying()) {
mediaPlayer.pause();
}
}
public void reset() {
if (mediaPlayer != null) {
mediaPlayer.reset();
}
}
public void release() {
if (mediaPlayer != null) {
mediaPlayer.release();
}
}
}
I'm working with SQL Server too, which will store data of the music and URL to that music file to play. But since it didn't work so there are 2 things I'm wondering:
1st: Where am I wrong in here? In my opinion, I'm suspecting that I'm not using the setDataSource(url) method in the correct way.
2nd: Is the URL of Google Drive in the right format to use? If it is not the right format to use with any (overloaded) setDataSource() method, then how can I store a music file with the right URL format it needs? (the right format like this, I think: "http://domain/path/audiofilename.mp3"). I'm accessing the database on local machine.
You have the answer hidden in your question itself. When you're passing the direct download link to the music, it works, meaning, you need to use something that will be able to directly download the file.
When you give the Google Drive link, you don't give the complete address of the file, instead, you go on the page which tells you to manually download the file.
You should use something like FirebaseStorage to store the songs, so you'll be able to get the direct download links of those songs.
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'm looking for a good working solution to integrate an IP-Cameras video stream in my Android App. At the moment i am using the "Axis P1214-E" which has a good image quality, but i couldn't get a "LIVE" stream from it. Either the stream is very laggy or it's a few seconds delayed (sometimes even more) or the stream is shutting down after awhile. What i tried so far:
Using a SurfaceView to get the MJPEG Stream as described in this post: Android and MJPEG
Problem: Lagging
Using a WebView to get the RTSP stream:
public class MainActivity extends Activity {
private static final String TAG = "VideoViewExample.MainActivity";
private static final String RTSP_URL = "rtsp://ip/axis-media/media.amp";
private VideoView videoView;
private MediaController mediaController;
private OnPreparedListener opl;
private int position = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(savedInstanceState != null)
position = savedInstanceState.getInt("POSITION");
videoView = (VideoView) findViewById(R.id.videoView);
opl = new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
videoView.seekTo(position);
if (position == 0) {
videoView.start();
}
else {
videoView.pause();
}
}
};
if (mediaController == null){
mediaController = new MediaController(this);
}
mediaController.setAnchorView(videoView);
AsyncTask<Void, Void, Void> at = new AsyncTask<Void, Void, Void>() {
#Override
protected Void doInBackground(Void... params) {
try{
videoView.setMediaController(mediaController);
videoView.setVideoURI(Uri.parse(RTSP_URL));
} catch (Exception e) {
Log.d(TAG, e.getMessage());
}
videoView.requestFocus();
videoView.setOnPreparedListener(opl);
return null;
}
};
at.execute();
}
#Override
protected void onPause() {
position = videoView.getCurrentPosition();
super.onPause();
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("POSITION", position);
}
}
Problem: Good video Quality, but delayed.
Using external frameworks like FFMPEG and GSTREAMER (Only some examples so far)
Problem: Also very laggy and/or delayed.
Now i'm running out of ideas to get this working. It's very important for my Application that the stream is live and not lagging.
I'm developing on a "Banana Pi" board with Android 4.2.2 (4.4 is possible as well).
Does anybody know how to get this working? Or maybe should i use an other camera? Do you have any suggestions that would work well with android?
Thanks in advance
Christian
I am doing the Android Programming Tutorial on Splash Screens where you show a picture or text for 5 Seconds than it goes to the Main Application. My Question is..Instead of Text or Pictures I want to display a Video File for 5 Seconds before it goes to the Next page of the Application.
I am not talking about when the Application Loads I am talking about when it is Loaded and you program it to display something on a Separate Java & XML page to display something then move to something else..here is my current code.
#Override
protected void onCreate(Bundle SplashScreen1) {
// TODO Auto-generated method stub
super.onCreate(SplashScreen1);
setContentView(R.layout.splash);
ourSong = MediaPlayer.create(Splash.this, R.raw.splashsound);
ourSong.start();
Thread timer = new Thread(){
public void run(){
try{
sleep(5000);
} catch (InterruptedException e){
e.printStackTrace();
}finally{
Intent openStartingPoint = new Intent("com.Player.Splash.STARTINGPOINT");
startActivity(openStartingPoint);
}
}
};
timer.start();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
ourSong.release();
finish();
}
So What Do I do to make it display a Video Media file without the Start/Stop etc..
1) Create SplashScreen.java class.
2) Create a raw folder inside res directory(res/raw).
3) Paste your mp4 video file in this raw folder(if you don't have any sample mp4, you can download from the below link). http://www.mediafire.com/download/p05ki89i2dt5x2x/splash.mp4
4) Then add the following code in your SplashScreen.java class.
public class SplashScreenActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
VideoView videoHolder = new VideoView(this);
setContentView(videoHolder);
Uri video = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.splash);
videoHolder.setVideoURI(video);
videoHolder.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
jump();
}
});
videoHolder.start();
} catch (Exception ex) {
jump();
}
}
#Override
public boolean onTouchEvent(MotionEvent event) {
jump();
return true;
}
private void jump() {
if (isFinishing())
return;
startActivity(new Intent(this, MainActivity.class));
finish();
}
}
Note: splash_activity.xml is not required.
I hope this will help you. You just create a simple VideoView for create a splash screen for the video.
Checkout the source code hear and simple steps what is the best practice to create a splash screen
Use a MediaPlayer along with a VideoView. You can then "listen" for when the video playback is done, by setting an OnCompletionListener on your MediaPlayer.
See here: http://developer.android.com/reference/android/media/MediaPlayer.html
And here: http://developer.android.com/reference/android/widget/VideoView.html
Also, pay special attention to the state diagram on the MediaPlayer reference page. It can be a bit tricky and has been known to trip a few people up.
imgAnim=(VideoView)findViewById(R.id.animimage);
String uriPath = "android.resource://com.petnvet/" + R.drawable.vidio;
Uri uri = Uri.parse(uriPath);
imgAnim.setVideoURI(uri);
imgAnim.requestFocus();
imgAnim.start();
// imgAnim.setVideoPath("android.resource://com.myapplication/" + R.drawable.vidio);
int SPLASH_DISPLAY_LENGTH = 3000;
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent mainIntent = new Intent(SplashScreen.this, Login.class);
startActivity(mainIntent);
finish();
}
}, SPLASH_DISPLAY_LENGTH);
Here is the code for adding video. In case you need to add controls on video like pause or seek etc. you can add them with:
vv.setMediaController(new MediaController(this));
Rest of the code:
VideoView vv;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
vv=(VideoView)findViewById(R.id.videoView);
Uri path=Uri.parse("android:resource://"+getPackageName()+"/"+R.raw.hello);
vv.setVideoURI(path);
vv.setMediaController(new MediaController(this));
vv.setOnCompletionListener(new MediaPlayer.OnCompletionListener()
{
#Override
public void onCompletion(MediaPlayer mp) {
Intent in=new Intent(splash.this,MainActivity.class);
startActivity(in);
finish();
}
});
vv.start();
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);
}
}