VideoView doesn't work at all, Mediaplayer is a no-go

All your problems with Audio, Video and Images.

VideoView doesn't work at all, Mediaplayer is a no-go

Postby dwljpl » Tue Jul 27, 2010 3:54 pm

Does anyone have a working example of a simple video player? I'm building a small app and I'm packaging a 4MB video along with the app in the raw resource folder. Everything I try either plays only the audio from the video or plays nothing at all.

1. Can video be played on the emulator?
2. Can I package a video along with the application?
3. Is there a WORKING demo of how to play a local video from an app?

I'm using a 3gp and mp4 video format but neither works. Android 2.1 using Galileo 3.5.2.

Wasted far too much time on this already with terrible examples all over the web that don't seem to work at all. Documentation is pretty lacking as well when it describes "enter the path to the video". Well, what is the path? I'm not familiar with the filesystem, does it reference C:\ as the root like a typical PC?

Mobile development would be much more satisfying if there wasn't so much bad information out there on how to build things. :?
dwljpl
Freshman
Freshman
 
Posts: 7
Joined: Tue Jul 27, 2010 3:45 pm

Top

Re: VideoView doesn't work at all, Mediaplayer is a no-go

Postby blundell » Wed Jul 28, 2010 3:25 pm

Ok so you quote 'enter the path to video' that mean's you've read the Video example on dev.and :-)
http://developer.android.com/resources/ ... Video.html

Now you want to use an internal resource, you simply do:
Code: Select all
player.setDataSource(this,Uri.parse("android.resource://com.your.package/" + R.raw.video1));


et voila

and don't tell me that example code doesn't work, cos it does! :p


1) yes , sometimes , no - I've seen it work on a 2.2 emulator and work on all phones 1.5 >
2) yes
3) yes see my post and link above :-)
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Re: VideoView doesn't work at all, Mediaplayer is a no-go

Postby dwljpl » Mon Aug 02, 2010 2:10 pm

This is what I mean. There are bits and pieces strewn everywhere but I have yet to see a working example that's just an activity and a layout that will play a local resource. You cleared up the path variable for me but once again I get "The application VideoTest has stopped unexpectedly..... blah blah blah.....".

I'm not saying the example doesn't work, I'm saying it's not obvious to a user who doesn't work in Java daily to determine what is breaking where. I would have thought examples of playing video would be everywhere but even the example on the Android developers site (which is only two lines.....) doesn't work and is poorly documented. :|

Thanks for trying to help, maybe I'm just dense....
dwljpl
Freshman
Freshman
 
Posts: 7
Joined: Tue Jul 27, 2010 3:45 pm

Re: VideoView doesn't work at all, Mediaplayer is a no-go

Postby blundell » Mon Aug 02, 2010 2:18 pm

For local media playback this is the simplest it's gonna get:
Code: Select all

import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.media.MediaPlayer.OnVideoSizeChangedListener;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Toast;

public class Video extends Activity implements OnBufferingUpdateListener, OnCompletionListener, OnPreparedListener,
      OnVideoSizeChangedListener, SurfaceHolder.Callback {

   private static final String TAG = "MediaPlayerDemo";
   private int mVideoWidth;
   private int mVideoHeight;
   private MediaPlayer mMediaPlayer;
   private SurfaceView mPreview;
   private SurfaceHolder holder;
   private String path;
   private static final String MEDIA = "media";
   private boolean mIsVideoSizeKnown = false;
   private boolean mIsVideoReadyToBePlayed = false;

   /**
    *
    * Called when the activity is first created.
    */
   @Override
   public void onCreate(Bundle icicle) {
      super.onCreate(icicle);
      setContentView(R.layout.main);
      mPreview = (SurfaceView) findViewById(R.id.surface);
      holder = mPreview.getHolder();
      holder.addCallback(this);
      holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
   }

   private void playVideo() {
      doCleanUp();
      try {

         // Create a new media player and set the listeners
         mMediaPlayer = new MediaPlayer();
         mMediaPlayer.setDataSource(this, Uri.parse("android.resource://com.your.package/" + R.raw.video1)); // CHANGE THIS TO DESIRED
         mMediaPlayer.setDisplay(holder);
         mMediaPlayer.prepare();
         mMediaPlayer.setOnBufferingUpdateListener(this);
         mMediaPlayer.setOnCompletionListener(this);
         mMediaPlayer.setOnPreparedListener(this);
         mMediaPlayer.setOnVideoSizeChangedListener(this);
         mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

      } catch (Exception e) {
         Log.e(TAG, "error: " + e.getMessage(), e);
      }
   }

   public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
      Log.v(TAG, "onVideoSizeChanged called");
      if (width == 0 || height == 0) {
         Log.e(TAG, "invalid video width(" + width + ") or height(" + height + ")");
         return;
      }
      mIsVideoSizeKnown = true;
      mVideoWidth = width;
      mVideoHeight = height;
      if (mIsVideoReadyToBePlayed && mIsVideoSizeKnown) {
         startVideoPlayback(); // THIS CALL HAS TO BE HERE not in onCreate
      }
   }

   public void onPrepared(MediaPlayer mediaplayer) {
      Log.d(TAG, "onPrepared called");
      mIsVideoReadyToBePlayed = true;
      if (mIsVideoReadyToBePlayed && mIsVideoSizeKnown) {
         startVideoPlayback(); // THIS CALL HAS TO BE HERE not in onCreate
      }
   }

   public void surfaceCreated(SurfaceHolder holder) {
      Log.d(TAG, "surfaceCreated called");
      playVideo(); // THIS CALL HAS TO BE HERE not in
                                 // onCreate

   }

   @Override
   protected void onPause() {
      super.onPause();
      releaseMediaPlayer();
      doCleanUp();
   }

   @Override
   protected void onDestroy() {
      super.onDestroy();
      releaseMediaPlayer();
      doCleanUp();
   }

   private void releaseMediaPlayer() {
      if (mMediaPlayer != null) {
         mMediaPlayer.release();
         mMediaPlayer = null;
      }
   }

   private void doCleanUp() {
      mVideoWidth = 0;
      mVideoHeight = 0;
      mIsVideoReadyToBePlayed = false;
      mIsVideoSizeKnown = false;
   }

   private void startVideoPlayback() {
      Log.v(TAG, "startVideoPlayback");
      holder.setFixedSize(mVideoWidth, mVideoHeight);
      mMediaPlayer.start();
   }

   // Methods you have to happen but aren't used in this example
   
   @Override
   public void onBufferingUpdate(MediaPlayer mp, int percent) {
      // TODO Auto-generated method stub
      
   }

   @Override
   public void onCompletion(MediaPlayer mp) {
      // TODO Auto-generated method stub
      
   }

   @Override
   public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
      // TODO Auto-generated method stub
      
   }

   @Override
   public void surfaceDestroyed(SurfaceHolder holder) {
      // TODO Auto-generated method stub
      
   }
}


Main.xml Layout:
Code: Select all
<?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"
   >
   <SurfaceView
      android:id="@+id/surface"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="center">
   </SurfaceView>   
</FrameLayout>
Last edited by blundell on Mon Aug 02, 2010 3:10 pm, edited 2 times in total.
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Re: VideoView doesn't work at all, Mediaplayer is a no-go

Postby dwljpl » Mon Aug 02, 2010 2:32 pm

I don't know what I'm doing wrong but this crashes too. Looking in the debug log is just a maze of data and code. Why doesn't the debugger give you some idea of where to start looking or what caused the crash. It's giving me a null pointer exception on ViewRoot.draw. :?:
dwljpl
Freshman
Freshman
 
Posts: 7
Joined: Tue Jul 27, 2010 3:45 pm

Re: VideoView doesn't work at all, Mediaplayer is a no-go

Postby blundell » Mon Aug 02, 2010 2:33 pm

Paste the Logcat into here I'll show you how to look through it meaningfully
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Top

Re: VideoView doesn't work at all, Mediaplayer is a no-go

Postby dwljpl » Mon Aug 02, 2010 2:46 pm

It looks like it gets to the surfacecreate called debug message and then blows up on the playvideo call. If I put a debug message inside playvideo I never see it displayed. I assume I need a try catch around the playvideo call.

Code: Select all
08-02 13:44:34.912: DEBUG/ddm-heap(432): Got feature list request
08-02 13:44:36.493: DEBUG/MediaPlayerDemo(432): surfaceCreated called
08-02 13:44:36.532: DEBUG/AndroidRuntime(432): Shutting down VM
08-02 13:44:36.542: WARN/dalvikvm(432): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
08-02 13:44:36.553: ERROR/AndroidRuntime(432): Uncaught handler: thread main exiting due to uncaught exception
08-02 13:44:36.632: ERROR/AndroidRuntime(432): java.lang.NullPointerException
08-02 13:44:36.632: ERROR/AndroidRuntime(432):     at com.danleister.vidtest.VideoTest.surfaceCreated(VideoTest.java:95)
08-02 13:44:36.632: ERROR/AndroidRuntime(432):     at android.view.SurfaceView.updateWindow(SurfaceView.java:454)
08-02 13:44:36.632: ERROR/AndroidRuntime(432):     at android.view.SurfaceView.dispatchDraw(SurfaceView.java:287)
08-02 13:44:36.632: ERROR/AndroidRuntime(432):     at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
08-02 13:44:36.632: ERROR/AndroidRuntime(432):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
08-02 13:44:36.632: ERROR/AndroidRuntime(432):     at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
08-02 13:44:36.632: ERROR/AndroidRuntime(432):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
08-02 13:44:36.632: ERROR/AndroidRuntime(432):     at android.view.View.draw(View.java:6538)
08-02 13:44:36.632: ERROR/AndroidRuntime(432):     at android.widget.FrameLayout.draw(FrameLayout.java:352)
08-02 13:44:36.632: ERROR/AndroidRuntime(432):     at android.view.ViewGroup.drawChild(ViewGroup.java:1531)
08-02 13:44:36.632: ERROR/AndroidRuntime(432):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
08-02 13:44:36.632: ERROR/AndroidRuntime(432):     at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
08-02 13:44:36.632: ERROR/AndroidRuntime(432):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
08-02 13:44:36.632: ERROR/AndroidRuntime(432):     at android.view.View.draw(View.java:6538)
08-02 13:44:36.632: ERROR/AndroidRuntime(432):     at android.widget.FrameLayout.draw(FrameLayout.java:352)
08-02 13:44:36.632: ERROR/AndroidRuntime(432):     at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1830)
08-02 13:44:36.632: ERROR/AndroidRuntime(432):     at android.view.ViewRoot.draw(ViewRoot.java:1349)
08-02 13:44:36.632: ERROR/AndroidRuntime(432):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1114)
08-02 13:44:36.632: ERROR/AndroidRuntime(432):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1633)
08-02 13:44:36.632: ERROR/AndroidRuntime(432):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-02 13:44:36.632: ERROR/AndroidRuntime(432):     at android.os.Looper.loop(Looper.java:123)
08-02 13:44:36.632: ERROR/AndroidRuntime(432):     at android.app.ActivityThread.main(ActivityThread.java:4363)
08-02 13:44:36.632: ERROR/AndroidRuntime(432):     at java.lang.reflect.Method.invokeNative(Native Method)
08-02 13:44:36.632: ERROR/AndroidRuntime(432):     at java.lang.reflect.Method.invoke(Method.java:521)
08-02 13:44:36.632: ERROR/AndroidRuntime(432):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
08-02 13:44:36.632: ERROR/AndroidRuntime(432):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
08-02 13:44:36.632: ERROR/AndroidRuntime(432):     at dalvik.system.NativeStart.main(Native Method)
08-02 13:44:36.692: INFO/Process(60): Sending signal. PID: 432 SIG: 3
dwljpl
Freshman
Freshman
 
Posts: 7
Joined: Tue Jul 27, 2010 3:45 pm

Re: VideoView doesn't work at all, Mediaplayer is a no-go

Postby blundell » Mon Aug 02, 2010 2:47 pm

java.lang.NullPointerException
08-02 13:44:36.632: ERROR/AndroidRuntime(432): at com.danleister.vidtest.VideoTest.surfaceCreated(VideoTest.java:95)

Line 95 of your VideoTest class NPE lol
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Re: VideoView doesn't work at all, Mediaplayer is a no-go

Postby dwljpl » Mon Aug 02, 2010 2:49 pm

Ha, that debug message was a lot of help. I wrapped the playvideo call in a try, catch and it returned an error of "null"....

Yep, you're seeing the same thing I am. This is line 95:

Code: Select all
playVideo(extras.getInt(MEDIA));


ok, more testing shows it's the extras.getInt that's failing. If I hardcode an integer there the code continues to run. I don't get any playback but at least I'm getting different results. :)

08-02 13:57:21.072: DEBUG/MediaPlayerDemo(485): onPrepared called
08-02 13:57:21.682: VERBOSE/MediaPlayerDemo(485): onVideoSizeChanged called
08-02 13:57:21.692: VERBOSE/MediaPlayerDemo(485): startVideoPlayback
08-02 13:57:26.452: WARN/PlayerDriver(31): Video track fell behind
08-02 13:57:26.482: ERROR/MediaPlayer(485): error (1, 48)
08-02 13:57:26.492: ERROR/MediaPlayer(485): Error (1,48)
dwljpl
Freshman
Freshman
 
Posts: 7
Joined: Tue Jul 27, 2010 3:45 pm

Re: VideoView doesn't work at all, Mediaplayer is a no-go

Postby blundell » Mon Aug 02, 2010 3:04 pm

HA so don't lie and say the video examples don't work when your adding extra intent bundle's in. Go and play with your droids :p
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Re: VideoView doesn't work at all, Mediaplayer is a no-go

Postby dwljpl » Mon Aug 02, 2010 3:07 pm

blundell wrote:HA so don't lie and say the video examples don't work when your adding extra intent bundle's in. Go and play with your droids :p


What? What are you talking about? I pulled YOUR code and adjusted a few items to fit the file structure I had here. I'm just looking for a working example to learn from, please show me where I added extra bundles. That was straight from your example.
dwljpl
Freshman
Freshman
 
Posts: 7
Joined: Tue Jul 27, 2010 3:45 pm

Re: VideoView doesn't work at all, Mediaplayer is a no-go

Postby blundell » Mon Aug 02, 2010 3:11 pm

Touche! :-) They were leftovers from the Android example. I've removed them in the above post if you haven't done already.
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Re: VideoView doesn't work at all, Mediaplayer is a no-go

Postby dwljpl » Mon Aug 02, 2010 3:15 pm

Ok, I still get the video fell behind error now. I wonder if I'm hitting an emulator limitation at this point? Thank you for the help so far.
dwljpl
Freshman
Freshman
 
Posts: 7
Joined: Tue Jul 27, 2010 3:45 pm

Re: VideoView doesn't work at all, Mediaplayer is a no-go

Postby blundell » Mon Aug 02, 2010 3:19 pm

As I said in my original post yeah depends on the emulator. Try running it on your phone.
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 10 guests