So I'm trying to play a basic avi video in android, it seems to run fine on Windows Media Player, VLC, etc. so it doesn't look like it's requiring any complicated codecs. I have a video view in my app and that's it, and I have my video in my resources directory under:
res/raw/my_video.avi
This is the code I'm using to load my video:
setContentView(R.layout.activity_main);
VideoView videoView = (VideoView) findViewById(R.id.videoView1);
Uri video = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.my_video);
videoView.setVideoURI(video);
videoView.start();
And it does not work. I get a popup saying "Can't play this video" along with a logcat message:
03-10 01:42:12.102: E/(185): Failed to open file 'android.resource://com.securespaces.android.bootstrap/2130968576'. (No such file or directory)
03-10 01:42:12.102: E/MediaPlayer(9737): error (1, -2147483648)
03-10 01:42:12.142: E/MediaPlayer(9737): Error (1,-2147483648)
I'm running this on a Nexus 5 running 4.4.2 stock by the way. I'm following instructions that I found in other stack over flow questions here: How to play videos in android from assets folder or raw folder? with some minor tweaks so that I am using a VideoView that I grab from a layout file.
I'm really stumped as to why this isn't working. I've browsed through a few questions on this subject, but this feels like something that should be a duplicate. I'm running this on a Nexus 5 running 4.4.2 stock by the way.
To clarify the question, I'm wondering what I am doing wrong, or is there an alternative for just playing a simple AVI video?
Write your package name statically and check whether video plays or not.
Try out as below:
VideoView videoView = (VideoView) findViewById(R.id.videoView1);
Uri video = Uri.parse("android.resource://com.securespaces.android.bootstrap/" + R.raw.my_video);
videoView.setVideoURI(video);
videoView.setMediaController(new MediaController(this));
videoView.requestFocus();
Implement this :
public static void getVideoFromRaw(String rawPath) {
try {
// Start the MediaController
MediaController mediacontroller = new MediaController(mContext);
mediacontroller.setAnchorView(mVideoview);
// Get the URL from String VideoURL
Uri mVideo = Uri.parse(rawPath);
mVideoview.setMediaController(mediacontroller);
mVideoview.setVideoURI(mVideo);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
mVideoview.requestFocus();
mVideoview.setOnPreparedListener(new OnPreparedListener() {
// Close the progress bar and play the video
public void onPrepared(MediaPlayer mp) {
mVideoview.start();
}
});
mVideoview.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
}
});
}
Thanks
Related
Here is my code on full screen video.
It work just fine in emulator but not in real android phone.
public void videoClick(View view){
Intent mIntent = new Intent(getBaseContext(),VideoFullscreenActivity.class);
videoUri = Uri.parse("android.resource://tk.myessentialoils.ideasapp/raw/"+ contentStringList[count][2]);
mIntent.putExtra("videoUri",videoUri);
startActivity(mIntent);
}
My thinking is that the Uri problem.
Xiaomi android have different uri than other.
Some Huawei phone also not functioning well.
So is there any alternative to get the file instead?
Perhaps a work around that will work on all version of android.
Edit 1
as per Vivek Mishra suggestions,
tried the below
Intent mIntent = new Intent(getBaseContext(),VideoFullscreenActivity.class);
String path = "file:///android_asset/"+ contentStringList[count][2];
videoUri = Uri.parse(path);
mIntent.putExtra("videoUri",videoUri);
startActivity(mIntent);
However i got this error >> Can't play this video
as per How to load videos from assets folder? (to play them with VideoView) asset folder cannot play video
Edit 2
same question as Nullpointerexception, i cant get the Media player to work with my code.
Uri videoUri = getIntent().getParcelableExtra("videoUri");
VideoView videoView=findViewById(R.id.myvideoview);
videoView.setVideoURI(videoUri);
//videoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.full));
MediaController mediaController = new MediaController(this);
videoView.setMediaController(mediaController);
videoView.start();
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.setLooping(true);
}
});
how do i implement the below code in my code above.
AssetFileDescriptor afd;
try {
afd = getAssets().openFd("v.mp4");
player.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),
afd.getLength());
player.prepareAsync();
player.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
} catch (Exception e) { e.printStackTrace();}
I have project which have html5 video needed to play in native Android Player similar how it does on iOS.
Is there way to implement it?
I will be grateful for any adivice or solution.Thanks.
Yes if you bring some codecs for your file format (.Flv I think) because android does not have those codes.
For a mp4 file use something like this:
try {
setContentView(R.layout.videodisplay);
String link="http://s1133.photobucket.com/albums/m590/Anniebabycupcakez/?action=view& current=1376992942447_242.mp4";
VideoView videoView = (VideoView) findViewById(R.id.VideoView);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
Uri video = Uri.parse(link);
videoView.setMediaController(mediaController);
videoView.setVideoURI(video);
videoView.start();
} catch (Exception e) {
// TODO: handle exception
Toast.makeText(this, "Error connecting", Toast.LENGTH_SHORT).show();
}
I need to play a live stream on devices with 2.x and greater versions. This states that it's impossible to play live streams on devices with Android 2.x.
What're my options here ? Especially I'm interested in streaming audio - what format should i pick and in conjunction with which protocol ?
P.S. I've tried Vitamio - don't want to make customers download third party libraries.
UPD
How come I can play this stream "http://188.138.112.71:9018/" ?
try this example for RTSP streaming (the url should support RTSP) for video change the code to support just audio
public class MultimediaActivity extends Activity {
private static final String RTSP = "rtsp://url here";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.multimedia);
//***VideoView to video element inside Multimedia.xml file
VideoView videoView = (VideoView) findViewById(R.id.video);
Log.v("Video", "***Video to Play:: " + RTSP);
MediaController mc = new MediaController(this);
mc.setAnchorView(videoView);
Uri video = Uri.parse(RTSP);
videoView.setMediaController(mc);
videoView.setVideoURI(video);
videoView.start();
}
}
EDIT:
Live Audio streaming using MediaPlayer in Android
Live Audio streaming in android, from 1.6 sdk onwards is become so easy. In setDataSource() API directly pass the url and audio will play without any issues.
The complete code snippet is,
public class AudioStream extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String url = "http://www.songblasts.com/songs/hindi/t/three-idiots/01-Aal_Izz_Well-(SongsBlasts.Com).mp3";
MediaPlayer mp = new MediaPlayer();
try {
mp.setDataSource(url);
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.prepare();
mp.start();
} catch (Exception e) {
Log.i("Exception", "Exception in streaming mediaplayer e = " + e);
}
}
}
You can use RTSP protocol which is supported by Android native media player.
player = new MediaPlayer();
player.reset();
player.setDataSource(intent.getStringExtra("Path"));
player.prepare();
player.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
player.start();
}
});
Where path would be your rtsp audio streaming url.
Is there any way I can play a video that has been stored in the raw file, without the use of the uri. If not how do i properly set the uri. Let's say i have file named movie and I want to play it with a videoviewer. How would i do this? Also would i have to write the data to the sd card. or can i just play it from the raw.
Why cant I just do
uri uri.parse("android.resources://"+this.getApplicationContext().getPackageName()+movie);
This is what I have it as now if you could can you point out the errors to me so i know and i wont have to have this problem any more
VideoView videoview = (VideoView) findViewById(R.id.videoView1);
videoview.setKeepScreenOn(true);
videoview.setVideoPath("android.raw://com.example.movievp8");
MediaController controller = new MediaController(videoview.getContext());
videoview.setMediaController(controller);
videoview.start();
videoview.requestFocus();
Here is something that works for me:
videoResource = R.raw.some_video;
String uri = String.format("android.resource://%s/%d", context.getPackageName(), videoResource);
animationCanvas = (VideoView) ui.findViewById(R.id.animation_canvas);
animationCanvas.setVisibility(View.VISIBLE);
animationCanvas.setVideoURI(Uri.parse(uri));
animationCanvas.setOnCompletionListener(this);
animationCanvas.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
animationCanvas.start();
}
});
I want that when i will pass the utube url to media player it will automatically load the video and play in it.
Example: http://www.youtube.com/watch?v=WAG8e_53le4
This type of url i want to play in android media player
If you want to play YouTube video in VideoView, you need rtsp url from youtube.
You can get the rtsp from Youtube Gdata feed. Although I find it quite strange that sometime the rtsp isn't available in the feed.
This works for me. It starts when the activity is created.
progressDialog = ProgressDialog.show(StevenBActivity.this, "", "Buffering video...", true);
getWindow().setFormat(PixelFormat.TRANSLUCENT);
//video "DON'T DO IT" 1,315 views
video_url = "rtsp://v2.cache4.c.youtube.com/CjgLENy73wIaLwlPMIWk2hSzNRMYJCAkFEIJbXYtZ29vZ2xlSARSB3Jlc3VsdHNg6KnB9MbH8sVODA==/0/0/0/video.3gp";
//http://www.youtube.com/watch?v=NJNzBQvJpUI&list=UUy_BjNhdQh9-bfRuzuk53pg&index=9&feature=plcp
try {
final VideoView videoView =(VideoView)findViewById(R.id.videoView1);
mediaController = new MediaController(StevenBActivity.this);
mediaController.setAnchorView(videoView);
// Set video link (mp4 format )
Uri video = Uri.parse(video_url);
videoView.setMediaController(mediaController);
videoView.setVideoURI(video);
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.getMessage());
}
}