MediaPlayer / VideoView not playing video? - java

I'm new to android development, self taught so can expect a few errors here and there but none so irritating as this. I've looked over my code a thousand times, searched high and low across multiple websites, books and forums for an answer but I still get the same error so this is a last resort.
I just want to play a hardcoded path to a video in an activity, which is part of my video portfolio app. (The hardcoded path is just for testing, later I will call each video from the related button press, but only after I sort the player out!).
Here is my code:
String path = "android.resource://mysite/res/raw/video1";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_videoplayer);
VideoView view = (VideoView) this.findViewById(R.id.vv);
MediaController controller = (MediaController) new MediaController(this);
controller.setMediaPlayer(view);
view.setVideoPath(path);
view.requestFocus();
view.start();
}
This snippet is inside my VideoPlayer class, where vv is the VideoView in the XML and video1 is the video to be played. The video is h.264 mp4, 1 minute long and 3mb in size and can be played normally through the default player.
XML:
<VideoView
android:id="#+id/vv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
The activity loads but returns the error "Can't play this video".
Any help I greatly appreciate.

VideoView view = (VideoView) this.findViewById(R.id.vv);
view.setVideoPath(path);
view.setMediaController(new MediaController(this));
view.requestFocus();
view.start();
Use like this. hope this will give you some solution.

According Android's official documentation Video decoding support for MP4(h.264) added from android 3.0+, so I think you are playing this video on version below 3.0. Try it on the device which has android os version 3.0+.

Try this..
video0=(VideoView)findViewById(R.id.vv);
video0.setMediaController(new MediaController(this));
video0.setVideoURI(Uri.parse("android.resource://" +getPackageName()+ "/" +R.raw.video1));
video0.requestFocus();
video0.start();

Did you try
MediaController mc = new MediaController(this);
mc.setAnchorView(vv);

Related

Android - Playing Youtube Videos using Youtube Player API in ListView

I have tried using WebViews to display Youtube Videos in ListView, what happens is when i scroll off screen and back to the cell the Video was located the Video disappears meaning that the WebView does not render back the WebView with the Video in it. So i tried using YoutubePlayerAPI but i am finding it difficult to understand how i can insert these Videos using the YoutubePlayerAPI.
Could someone please advise?
I have tried using YoutubePlayerView in my ArrayAdapter but i get Inflate errors, i have also created YoutubeBaseActivity and YoutubeFragment but cant understand how i get the layout or ui to display these videos in my ListView Cells.
How i did it
Download the youtube android SDK from here then get the jar file and copy it to libs folder on your app.
Enter this in dependency in gradle.build under dependencies
compile files('libs/YouTubeAndroidPlayerApi.jar')
Syc and build.
Create an activity that extends YoutubeBaseActivity like below and implement the YoutubePlayer.OnInitializedListener as shown below. Override the methods on the interace. Android studio can help you do this.
public class Display extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {
Then, when it loads successfully,
#Override
public void onInitializationSuccess(Provider provider, YouTubePlayer player, boolean b) {
if (!b) {
player.cueVideo(videoID);
}
}
This will load the youtube player and you play on the API
Happy coding, sorry am late

Android - Animate VideoView or MediaController

I want to animate the displaying and hiding of a VideoView.
My View is wrapped in a MediaController.
My code:
VideoView videoView = (VideoView) findViewById(R.id.myvideoview);
mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
videoView.setMediaController(mediaController);
videoView.setOnCompletionListener(myVideoViewCompletionListener);
I tried to animate with this:
Animation anim = AnimationUtils.loadAnimation(context, R.anim.my_anim);
videoview.startAnimation(anim);
I also tried to animate the MediaController but no luck so far.
I found just un- or not quite answered posts around the net.
Does anybody has an idea what I might be doing wrong?
Thanks.
Ron

Android MediaPlayer will not play sound on newer APIs

I've been using this code and it plays sound just fine on Froyo and Gingerbread (and I assume Honeycomb as well as my friends have used it):
MediaPlayer mp = MediaPlayer.create(this, R.raw.click);
Button clicker = (Button) findViewById(R.id.clicker);
clicker.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
mp.start();
}
});
The audio I'm using is in WAV format. I've checked it to make sure it's not corrupted and it's fine. This code and sound file still run correctly on Gingerbread.
On Ice Cream Sandwich and JellyBean devices (a galaxy nexus and a nexus 7 respectively) this code does not work. No sound is played. There's nothing put in logcat. I've searched through the Internet and asked my friends for ideas and I can't come up with anything.
Thank you in advance for your time!
Wrap the call in an IllegalStateException, run it thru the debugger and see what you are getting. Also set a boolean isPlaying=mp.isPlaying(); and check its value. Also try a mp.reset() before starting and see it it works.
Also see http://developer.android.com/reference/android/media/MediaPlayer.html#setOnErrorListener%28android.media.MediaPlayer.OnErrorListener%29
Go ahead and inplement MediaPlayer.OnErrorListener and register the method with the media player. See what error you get.

Android Playing Movie Files In A Live Wallpaper

Is this even possible? I have tried using the MediaPlayer but it throws a NullPointerException on the MediaPlayer object. I can get audio to work but video wont.
mp=MediaPlayer.create(getApplicationContext(), R.raw.sample);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener()
{
public void onCompletion(MediaPlayer mp) {
mp.release();
playing = false;
}
});
the sample is of .mp4 type.
Anyone have an idea of why this is happening or have a suggestion for another method of getting videos to be played?
You can use the following code
VideoView videoView;
VideoView = (VideoView) findViewById (R.id.txt1);
videoView.setVideoPath(path);
videoView.setVisibility(VideoView.VISIBLE);
videoView.start();
i have tried to play mp4 on my emulator but it was not showing video but when i tried on device it work fine.
Haven't tried that before but I think you can use vlcj framework that's totally free and can play effectively almost any type of video (and of course plays .mp4 video files) .I can't give you any code in android because have never worked with android but I know java and and it just works.So here what i use in Java:
NativeLibrary.addSearchPath("libvlc",path); //To set path of libvlc
Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class);//To import libvlc
//The path can be a folder in your android project.All the files needed are in vlc player installation folder.so yes you have to install vlc in your computer to get those files but just once.
canvas = new WindowsCanvas();
panel.add(canvas);//panel is like your VideoView
canvas.setVisible(true);
canvas.setBackground(Color.black);
mediaPlayerFactory = new MediaPlayerFactory();
player12 = mediaPlayerFactory.newEmbeddedMediaPlayer();
CanvasVideoSurface videoSurface = mediaPlayerFactory.newVideoSurface(canvas);
player12.setVideoSurface(videoSurface);
player12.setPlaySubItems(true);
player12.startMedia(yourVideoPath);
player12.setAspectRatio(""+panel.getWidth()+":"+panel.getHeight()); //Those two lines are for your video to be adusted in your panel or better to your VideoView
player12.setCropGeometry(""+panel.getWidth()+":"+panel.getHeight());
The jar files you have to include in your classpath are jna-3.4.0.jar,platform-3.4.0.jar,vlcj-2.1.0.jar

How to implement an audio player for Android using MediaPlayer And MediaController?

I want to create an Android application that is a client for an Internet radio station. And I want it look native to Android? But im confused with Android API logic and documentation. What i've got is that I need MediaPlayer and MediaController classes. Am I right, and is there any good example of AUDIO player for Android?
Especially, I'm very interested how to use MediaPlayer and MediaController classes together.
UPD:
Finally I've got the code, that does exactly what I want:
Intent i = new Intent(Intent.ACTION_VIEW);
Uri u = Uri.parse(%file_uri%));
i.setData(u);
startActivity(i);
you can look at those links :
http://www.helloandroid.com/tutorials/musicdroid-audio-player-part-i
Hope it will help.
[EDIT]
You have also some example on the official android developer website :
http://developer.android.com/guide/topics/media/index.html

Categories