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.
Related
I've got a very basic Android application that I've been writing in Android Studio. Firstly the purpose of the app is when launched to play a video full screen on loop until the screen is touched and the application exits. It's essentially being used as a screensaver for a digital signage installation.
I've got the below code which handles playing the video but cannot seem to get any code for exiting on touch to work. I am a bit of a novice when it comes to Android development.
package halifax.screensaver.screensaver;
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.VideoView;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
VideoView videoView = findViewById(R.id.videoView);
Uri uri=Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.hab2);
videoView.setVideoURI(uri);
videoView.requestFocus();
videoView.start();
}
}
}
Any help would be greatly appreciated
have you tried setting an onclick on your videoview and finishing the activity?
videoView.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
finish();
}
});
Set an OnClickListener and call finish() to exit the app and display the screen that was open before your activity was launched. If you want to go to a previously opened activity in your own app call onBackPressed(). Or if you want the user to get the home screen/launcher call the intent:
Intent i = new Intent(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_HOME);
startActivity(i);
Try this
Use setOnTouchListener for touch event of videoview
videoView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
finish();
return false;
}
});
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();
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.
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.