I want to play video in VideoView.
When I try to do that I got the following error:
Here is my source code:
package com.video.listitem;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.widget.MediaController;
import android.widget.VideoView;
public class PlayVideo extends Activity {
VideoView mVideoView;
MediaController mc;
String videourl;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.videoplay);
try {
mVideoView = (VideoView) findViewById(R.id.videoview);
String videourl = "rtsp://v7.cache4.c.youtube.com/CiILENy73wIaGQl25yDUbxNXTRMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp";
mc = new MediaController(this);
mVideoView.setMediaController(mc);
mVideoView.requestFocus();
mVideoView.setVideoURI(Uri.parse(videourl));
mc.show();
mVideoView.start();
} catch (Exception e) {
//TODO: handle exception
}
}
}
Here is my XML File:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="none"
android:gravity="center">
<VideoView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/videoview" />
</LinearLayout>
Try to run app in Device , Video may not run in Emulator...
see my answere here in the example code provided. If video is not playing try to re-encode it with a program like Handbrake and try again. Info on supported video formats: Android Supported Media Formats
Try to add permission to connect to internet !
uses-permission android:name="android.permission.INTERNET"
public void playVideo() {
videoView = findViewById(R.id.vid);
videoView.setVideoPath("rtsp://v7.cache4.c.youtube.com/CiILENy73wIaGQl25yDUbxNXTRMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp")
videoView.start();
}
Just call this function in your onCreate() method
The answer to this problem may be in this post:
Returning false or not having an OnErrorListener at all will cause
the OnCompletionListener to be called.
So return true instead of false from the function and no error
will be shown, i.e.
video.setOnErrorListener(new OnErrorListener() {
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
Log.d("video", "setOnErrorListener ");
return true;
}
});
May be the url is not correct. Try to play that stream using vlc player. If it is played then we can look into other possible reason
Ok first thing you wanna do is clean up your layout, this is your first problem:
unmesh it first and make the problem viewable:
You should have caught the error which is missing your closing cap after center", just add a ">" ... and that's it. Clean the project and you should see rtsp video. And your Internet Permission as previously stated.
Related
My Android app has a music file that I want it playing when the Main Activity is started and I want the music file to restart when it finishes. So, basically I want it to loop over and over until the user has moved to a different activity.
Below is the class that starts the music file, but when the music is over, it does not restart ...
import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
public class MainMenu extends Activity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_menu);
MediaPlayer mPlayer = MediaPlayer.create(MainMenu.this, R.raw.mmt_menu);
mPlayer.start();
AudioManager manager = (AudioManager)this.getSystemService(Context.AUDIO_SERVICE);
if(!manager.isMusicActive())
{
mPlayer.start();
}
}
}
What do I need to do so that when the music stops it restarts again?
Thanks very much!
Call the function:
MediaPlayer.setLooping(true|false)
on the mediaplayerObject after you called MediaPlayer.prepare()
Example:
Uri mediaUri = createUri(context, R.raw.media); // Audiofile in raw
folder Mediaplayer mPlayer = new MediaPlayer();
mPlayer.setDataSource(context, mediaUri);
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mPlayer.prepare();
mPlayer.setLooping(true);
mPlayer.start();
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I am using HTML5 video tag with mp4 video, some phone playing this video good... nut some phone not playing this video...
i have doubt that inside the application which video player will work to play? have any inbuily video player or need to install any codec for that or it using browser video player?
It uses phone's codecs only. Problem is while decoding as it is not using android videoview and overlay.
Try setting harwareacceleration to true for you app. It should work. But some phones may not have hardware acceleration and so the decode fails.
But correct approach is to write the android videoplayer to play view like this:
VideoPlayerActivity.java:
package com.camera.manual;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;
public class VideoPlayerActivity extends Activity {
private boolean mResumed = false;
private boolean mFocused = false;
private boolean mControlResumed = false;
private VideoView videoView = null;
private int stopPosition = 0;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(R.style.Theme_TransparentVideo);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
setContentView(R.layout.video_view);
videoView =(VideoView)findViewById(R.id.myvideoview);
MediaController mediaController= new MediaController(this);
mediaController.setAnchorView(videoView);
Uri uri=Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.slow);
videoView.setMediaController(mediaController);
videoView.setVideoURI(uri);
videoView.requestFocus();
videoView.start();
}
#Override
public void onPause() {
super.onPause();
mResumed = false;
if (mControlResumed) {
if (null != videoView)
videoView.pause();
stopPosition = videoView.getCurrentPosition();
mControlResumed = false;
}
}
#Override
public void onResume() {
super.onResume();
mResumed = true;
if (mFocused && mResumed && !mControlResumed) {
if(null != videoView) {
//videoView.resume();
videoView.seekTo(stopPosition);
videoView.start();
}
mControlResumed = true;
}
}
#Override
public void onWindowFocusChanged(boolean hasFocus) {
mFocused = hasFocus;
if (mFocused && mResumed && !mControlResumed) {
if (null != videoView) {
//videoView.resume();
videoView.seekTo(stopPosition);
videoView.start();
}
mControlResumed = true;
}
}
}
video_view.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<VideoView android:id="#+id/myvideoview"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_gravity="center" />
</FrameLayout>
and in JavaScriptInterface define a function like this:
public void launchVideoView () {
Intent intent = new Intent();
intent.setClass(mContext, VideoPlayerActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
mContext.startActivity(intent);
}
Then call this function when you have to play video
i am new to android i want to develop application of video player in this video player i need to integrate seekbar. i was stuck in this scenario. the scenario is the seekbar seeking up to video length,at the end of the video(video is finished to play ) seekbar set to zero plz help me how can i implement this scenario.
i do not know how to implement
1)how to set seekbar seeking up to video length/duration
2)if video is fully played how to set seekbar in start position/initial position(before seeking video position)
3)how can i get video is fully played
please any one help me as soon as possible
i found these links may be they help you:
How to play a video using videoview controlled by a seekBar or Progress bar?
How to use a Seekbar in android as a seekBar as well as a progressBar simultaneously?
How can i change the position of a progressBar, after the duration of a videoView?
package com.coderzheaven.pack;
import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.net.Uri;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;
public class VideoViewDemo extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
showVideo();
}
private void showVideo()
{
VideoView vd = (VideoView)findViewById(R.id.videoview);
Uri uri = Uri.parse("android.resource://"+ getApplication().getPackageName() +"/"+R.raw.asal);
MediaController mc = new MediaController(this);
vd.setMediaController(mc);
vd.setVideoURI(uri);
vd.start();
vd.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
//you can set seekbar position here with help of vd.seekTo(progress);
Toast.makeText(getApplicationContext(), "Video completed", Toast.LENGTH_LONG).show();
}
});
}
}
1)how to set seekbar seeking up to video length/duration
you can acheive this by using this code:
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
long duration = videoView.getDuration();
}
});
if this works mark this as answer for others help
I have used MUSIC_PLAYER in my application by starting like this:
Intent myint = new Intent("android.intent.action.MUSIC_PLAYER");
startActivity(myint);
And I want to pause this player. How do I do that?
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Thread mtr = new Thread(){
public void run()
{
try {
Intent myint = new Intent("android.intent.action.MUSIC_PLAYER");
startActivity(myint);
sleep(50000);
// Here I want to pause my application after those 50 seconds.
}
catch(InterruptedException e){
e.printStackTrace();
}
finally{
Intent myint = new Intent("nextscreen");
startActivity(myint);
}
}
};
mtr.start();
}
}
In Samsung devices, you can control the music player with these intents:
ICS:
"com.android.music.musicservicecommand.play"
"com.android.music.musicservicecommand.pause"
"com.android.music.musicservicecommand.next"
...
JB:
"com.sec.android.app.music.musicservicecommand.play"
"com.sec.android.app.music.musicservicecommand.pause"
"com.sec.android.app.music.musicservicecommand.next"
...
context.sendBroadcast(new Intent("com.sec.android.app.music.musicservicecommand.pause"));
Another solution is requestAudioFocus in AudioManager (do not forget to release it with abandonAudioFocus). For example:
...
((AudioManager)getSystemService(Context.AUDIO_SERVICE)).requestAudioFocus(listener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
...
...
((AudioManager)getSystemService(Context.AUDIO_SERVICE)).abandonAudioFocus(listener);
...
You can't do that. Pausing the music player is an internal action inside the application available to the user exclusively, so it cannot be accessed/called from outside of the application.
You can, however, mute the music audio stream using the AudioManager class by calling setStreamMute(AudioManager.STREAM_MUSIC, true).
Code sample:
AudioManager am = (AudioManager)MainActivity.this.getSystemService(Context.AUDIO_SERVICE);
am.setStreamMute(AudioManager.STREAM_MUSIC, true);
See answer here, you can send broadcast to pause or stop the default music player very easy.
This is a critical issue pertaining to an application we did. The code snippet attached here was working like a charm for the past several months. All of a sudden, it gives us the message in mediaplayer that video cannot be played.
Can anyone please suggest what need to be done, in order that this code will work for me by playing the video with no hassles.
Looking forward for your valuable comments, help and suggestions,
Please follow this link in order to view the code which was working perfectly -- http://pastebin.com/NVT8eBC0
import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.Uri;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoView;
public class videoact extends Activity implements OnPreparedListener{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MediaController mc = new MediaController(this);
VideoView vv = (VideoView)findViewById(R.id.VideoView01);
try
{
Uri ur = Uri.parse("http://youtube.com/get_video?video_id=ZmX7zLCrcAI&t=vjVQa1PpcFPmrj_j5y370BhPYfq3qHoWsFICYcBqEl4%3D&asv=&fmt=18");
vv.setVideoURI(ur);
vv.setMediaController(mc);
vv.requestFocus();
vv.start();
mc.show();
}
catch(Exception e)
{
System.out.print(e.getMessage() + "error");
}
}
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.start();
}
}
You can't play YouTube videos like that. Use the Android YouTube Player Api or the IFrame API in a WebView.
Using the get_video endpoint is against youtube's terms of service.
https://www.youtube.com/static?template=terms
You agree not to access Content through any technology or means other than the video playback pages of the Service itself, the Embeddable Player, or other explicitly authorized means YouTube may designate.