Android not playing Video .mp4 - android

I have prepared a code to just play a simple mp4 file from my res folder. The coding is something like this:
public class VideoPlayer extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.video);
VideoView video = (VideoView)findViewById(R.id.VideoView);
Uri uri = Uri.parse("android.resource://company.software.myapp/"
+ R.raw.myvideo);
MediaController mc = new MediaController(this);
video.setMediaController(mc);
video.setVideoURI(uri);
//video.requestFocus();
video.start();
}
}
Now though there is no error in playing. The activity automatically generates a dialog saying "sorry this video cannot be played", but I can hear the audio and it plays till end. What is the problem?

Thanx a lot commonsware.com... but i found the solution to the problem... And astonishingly its the PC processor which is the culprit... I checked n a higher configuration and guess wat... it worked perfectly fine... though sometimes if we do some processing in the background the dialog box does come up but on clicking ok it starts playing the video after some time...
But i confirm that this technique of playing file from resource is ok as far as i know...
sorry to waste ur precious time in a mundane hardware problem... but hope it'll be useful for other people who get this problem...

Android supports 3gp and mp4 format, but still sometimes there are problems in playing an mp4 content.
one thing what I have found out from my research is that, this might be because the resolution problem with the video.
I think that you should re-size the resolution of your mp4 video. This might help.

I have not attempted to play a video clip out of a resource, and I am not certain that it works.
As a test, put the video clip on the SD card and use that as the source of your video.
If you get the same symptoms, then either the MP4 file has issues or it is something with your test environment (e.g., you are using the emulator and don't have a quad-core CPU).
If the SD card test works, though, then I suspect the problem is packaging it as a resource.

Related

Play a video on Android using a videoview container

Im trying to understand how to add video to my application using the VideoView container but so far I've never been able to view a video and all i receive is an error 'Can't play this video'.
I have a simple method that load's the video:-
private void setupVideo() {
VideoView videoView = (VideoView)findViewById(R.id.videoView);
videoView.setVideoPath("video/Test_Movie.m4v");
videoView.start();
}
I have tried converting the video to many H264, H263 , 3gp formats but I can find anything that works.I even have downloaded a video that said worked in a book i have but still doesn't work.
any help appreciated!
thx.

I cannot change a video using MediaPlayer without reseting

I am working with streaming video and I want to change from one source to another dinamically.
First I set the video uri to the VideoView
view.setVideoURI(myUri);
And I know that I am capable of changing it afterwards by doing (this is in onPrepare method but it could go somewhere else where I have access to the MediaPlayer).
#Override
public void onPrepared(MediaPlayer mp)
{
Uri newUri = getOtherUri();
mp.reset();
mp.setDataSource(getApplicationContext(), newUri);
mp.prepare();
mp.start();
}
The thing is, I want to change the source without reseting the mediaPlayer (I do not want to disturb the user).
I tried to create a new VideoView with the new Uri and then change one object for the other, and likewise with the media player. However, none of that seems to work.
Is it possible to replace a video while it is playing in Android?
Any comments would be appreciated.
There is no option to reset the video without using mediaplayer.stopPlayback() or mediaplayer.reset. The reason is that; the previous object of the mediaplayer has to be released before you can play other video .
I want to change the source without reseting the mediaPlayer (I do not want to disturb the user).
Well, this cannot be achieved as the mediaplayer has to be reset. So there will be lag while changing videos. And to satisfy you, you can see these behavior in any videoplayer app like youtube or mxplayer.
So the only thing you can do is to show progressbar while loading or changing video.
Hope it helps. Cheeers.:)

Movie Player Android

I have two questions. I create movie player.
This is my code:
VideoView videoView = (VideoView)this.findViewById(R.id.videoView);
MediaController mc = new MediaController(this);
videoView.setMediaController(mc);
videoView.setVideoPath("/sdcard/747.3gp");
videoView.requestFocus();
videoView.start();
As you can see I want to play movies from sd card. To check my results I use emulator 2.2. So this is my first question: always when I want to play movie, he is stuck, but sound from movie is playing correctly. This is emulator error or maybe I am doing something wrong? And second question. I want play movies this way. I execute application and I get List of movies. I choose movie and this movie is playing. How I can do this? Can you write me example? I need help :)
For your first question, this should definitely be emulator's speed problem or problem with your file itself.
And answering your second question, you can go for samples like file explorer which displays all the files from sdcard in a listview and using which you can handle to show only video files.
Here is a example of file explorer,
http://android-er.blogspot.com/2010/01/implement-simple-file-explorer-in.html
http://androidsamples.blogspot.com/2009/06/displaying-list-of-video-files-stored.html
Extract the necessary code from the links and modify it accordingly.

android multiple videoView problem, Galaxy Tab specific

I am having problems with multiple videoViews specifically on Galaxy Tab.
In my app, I have two different video files which I want to play simultaneously. So I designed my app to have two videoViews side by side. Tried to run it on two non Galaxy tabs and it worked. Easy as that.
But then, I tried to test it on my Galaxy Tab and the problem comes out. The two video file doesn't play. At some point, the first video file plays and then stops and pops up the Cannot Play Video error. I spent almost two days looking for the cause of the problem and I failed. That's why I resorted to guessing what could be the cause.
My suspicion was that it cannot render two videos at the same time, so I tried to play only the sound of the first file using MediaPlayer and play the other one in the videoView. And I think my suspicioin was right because it works, the first video file plays only the sound, and the other one plays the full video and the sound.
I am looking for someone with this kind of problem, or someone who knows a workaround for this. I will post my simple code here for you to take a look at it. I would really appreciate your help! Thanks in advance.
junmats.
final videoView v1 = (VideoView) findViewById(R.id.videoView1);
final videoView v2 = (VideoView) findViewById(R.id.videoView2);
Thread th1 = new Thread(new Runnable() {
#Override
public void run() {
Uri uriFile = Uri.parse(myFile);
v1.setVideoURI(uriFile);
v1.start();
}
});
th1.start();
Thread th2 = new Thread(new Runnable() {
#Override
public void run() {
Uri uriFile = Uri.parse(
v2.setVideoURI(uriFile);
v2.start();
}
});
th2.start();
This seems to be dependent on the kernel version so it may not be supported on certain devices... http://code.google.com/p/android/issues/detail?id=17802
You may have to wait for an update.

How to play videos in android from assets folder or raw folder?

I am trying to play a video in android emulator
I have the video in my assets folder as well as the raw folder
But after doing some research still i cant play video in my emulator
i am working on android 2.1
My video format is mp4 so i don't think that should be a problem
Could anyone just give me an example code so that i can understand a bit more?
The problem is that the VideoView that I need to display the Video will take only a URI or a File path to point to the Video.
If I save the video in the raw or assets folder I can only get an input stream or a file descriptor and it seems nothing of that can be used to initialize the VideoView.
Update
I took a closer look at the MediaPlayer example and tried to start a MediaPlayer with a FileDescriptor to the assets files as in the code below:
SurfaceView videoView = (SurfaceView) findViewById(gettingStarted)
SurfaceHolder holder = videoView.getHolder();
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
final MediaPlayer player = new MediaPlayer();
player.setDisplay(holder);
player.setDataSource(getAssets().openFd(fileName).getFileDescriptor());
player.prepareAsync();
player.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
Now I get a the following exception:
java.io.FileNotFoundException: This file can not be opened as a file descriptor; it is probably compressed
It seems there is no other way then copying the file to the sdcard on startup and that seems like a waste of time and memory.
## Perfectly Working since Android 1.6 ##
getWindow().setFormat(PixelFormat.TRANSLUCENT);
VideoView videoHolder = new VideoView(this);
//if you want the controls to appear
videoHolder.setMediaController(new MediaController(this));
Uri video = getUriFromRawFile(context, R.raw.your_raw_file);
//if your file is named sherif.mp4 and placed in /raw
//use R.raw.sherif
videoHolder.setVideoURI(video);
setContentView(videoHolder);
videoHolder.start();
And then
public static Uri getUriFromRawFile(Context context, #ResRaw int rawResourceId) {
return Uri.Builder()
.scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)
.authority(context.getPackageName())
.path(String.valueOf(rawResourceId))
.build();
}
## Check complete tutorial ##
String UrlPath="android.resource://"+getPackageName()+"/"+R.raw.ur file name;
videocontainer.setVideoURI(Uri.parse(UrlPath));
videocontainer.start();
where videocontainer of type videoview.
Try:
AssetFileDescriptor afd = getAssets().openFd(fileName);
player.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(), afd.getLength());
PlayVideoActivity.java:
public class PlayVideoActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play_video);
VideoView videoView = (VideoView) findViewById(R.id.video_view);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
videoView.setMediaController(mediaController);
videoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.documentariesandyou));
videoView.start();
}
}
activity_play_video.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" >
<VideoView
android:id="#+id/video_view"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</VideoView>
</LinearLayout>
If I remember well, I had the same kind of issue when loading stuff from the asset folder but with a database. It seems that the stuff in your asset folder can have 2 stats : compressed or not.
If it is compressed, then you are allowed 1 Mo of memory to uncompress it, otherwise you will get this kind of exception. There are several bug reports about that because the documentation is not clear. So if you still want to to use your format, you have to either use an uncompressed version, or give an extension like .mp3 or .png to your file. I know it's a bit crazy but I load a database with a .mp3 extension and it works perfectly fine. This other solution is to package your application with a special option to tell it not to compress certain extension. But then you need to build your app manually and add "zip -0" option.
The advantage of an uncompressed assest is that the phase of zip-align before publication of an application will align the data correctly so that when loaded in memory it can be directly mapped.
So, solutions :
change the extension of the file to .mp3 or .png and see if it works
build your app manually and use the zip-0 option
Did you try to put manually Video on SDCard and try to play video store on SDCard ?
If it's working you can find the way to copy from Raw to SDcard here :
android-copy-rawfile-to-sdcard-video-mp4.
I found it :
Uri raw_uri = Uri.parse(<package_name>/+R.raw.<video_file_name>);
Personnaly Android found the resource, but can't play it for now. My file is a .m4v
VideoView myVideo = (VideoView) rootView.findViewById(R.id.definition_video_view);
//Set video name (no extension)
String myVideoName = "my_video";
//Set app package
String myAppPackage = "com.myapp";
//Get video URI from raw directory
Uri myVideoUri = Uri.parse("android.resource://"+myAppPackage+"/raw/"+myVideoName);
//Set the video URI
myVideo.setVideoURI(myVideoUri);
//Play the video
myVideo.start();
https://gist.github.com/jrejaud/b5eb1b013c27a1f3ae5f
I think that you need to look at this -- it should have everything you want.
EDIT: If you don't want to look at the link -- this pretty much sums up what you'd like.
MediaPlayer mp = MediaPlayer.create(context, R.raw.sound_file_1);
mp.start();
But I still recommend reading the information at the link.
It sounds maybe like you have the same issue as i do. instead of using MP4, is 3GPP possible? i think i used like HandBrake or something as the video converter... you just need to make sure you have the right encoder, like H.264x or something. sorry for being a little vague, it's been a while. Also, if it's possible, don't bother worrying about android 2.1 anymore, and also, something things just WILL NOT WORK IN EMULATOR. so if it works on a lot of devices, then just assume it works (especially with different manufacurers)
here, you can read my problem and how i solved the issue (after a long time and no one had an answer). i explained in a lot more detail here:
android media player not working
In the fileName you must put the relative path to the file (without /asset)
for example:
player.setDataSource(
getAssets().openFd(**"media/video.mp4"**).getFileDescriptor()
);
Use the MediaPlayer API and the sample code.
Put the media file in raw folder.
Get the file descriptor to the file.
mediaplayer.setDataSource(fd,offset,length); - its a three
argument constructor.
Then when onPreared , mediaplayer.start();
MainCode
Uri raw_uri=Uri.parse("android.resource://<package_name>/+R.raw.<video_file_name>);
myVideoView=(VideoView)findViewbyID(R.idV.Video_view);
myVideoView.setVideoURI(raw_uri);
myVideoView.setMediaController(new MediaController(this));
myVideoView.start();
myVideoView.requestFocus();
XML
<?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">
<VideoView
android:id="+#/Video_View"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>

Categories

Resources