Android Question Center video with VideoView (solved)

bgsoft

Well-Known Member
Licensed User
Longtime User
Hello

I am using VideoView (Audio library) to display videos.
I would like to center it on the device. But I do not have information on height and width of the video.

By doing this:

B4X:
vv.Initialize("vv")
vv.LoadVideo(Main.DirActual, Main.NombreUnaImagen)
DoEvents
Activity.AddView(vv, 0, 0, 100%x, 100%y)
  
Log("vv.Height "& vv.Height)
Log("vv.Width "& vv.Width)

The height and width returns me the width and height of the device but not the video

Any idea?

thanks
 

bgsoft

Well-Known Member
Licensed User
Longtime User
Thanks for your answer.

Maybe I did not express myself well.

At that link, you get "stretch" the video, but lose the aspect ratio.

I want to know or the aspect ratio of the video, or the width and height. And center the video on the screen , while maintaining its aspect ratio.

thanks
 
Upvote 0

bgsoft

Well-Known Member
Licensed User
Longtime User
Thanks Erel

There are some B4A video viewer, allowing center it, or know the aspect ratio?

thanks
 
Upvote 0

bgsoft

Well-Known Member
Licensed User
Longtime User
I've already got! :)

With this code I get the width and height of the video. So I can calculate the proportion and center the video


B4X:
      Dim vidfile As String
      vidfile = File.Combine(Dir, VideoName)
   
      Dim rfl As Reflector
      Dim obj As Object
      Dim b As Bitmap
     
      obj = rfl.CreateObject("android.media.MediaMetadataRetriever")
      rfl.Target = obj
      rfl.RunMethod2("setDataSource", vidfile, "java.lang.String")
      b = rfl.RunMethod3("getFrameAtTime", 0, "java.lang.long", 3, "java.lang.int")
           
      HeightVideo= b.Height 
      WidthVideo= b.Width

regards
 
Upvote 0

RandomCoder

Well-Known Member
Licensed User
Longtime User
B4X:
      Dim vidfile As String
      vidfile = File.Combine(Dir, VideoName)
      Dim rfl As Reflector
      Dim obj As Object
      Dim b As Bitmap
   
      obj = rfl.CreateObject("android.media.MediaMetadataRetriever")
      rfl.Target = obj
      rfl.RunMethod2("setDataSource", vidfile, "java.lang.String")
      b = rfl.RunMethod3("getFrameAtTime", 0, "java.lang.long", 3, "java.lang.int")
         
      HeightVideo= b.Height
      WidthVideo= b.Width

I'd like to center my Videoview and have tried the sample code kindly provided by bgsoft but it does not work for me as I am passing it a URI instead of a local file path. I've looked at the literature for the MediaMetadatRetriever class (http://developer.android.com/reference/android/media/MediaMetadataRetriever.html) and Google'd for an answer, I'm pretty sure that it can be done but as yet I've not worked out how. I just keep getting a NoSuchMethod java exception.

This is what I think I should be trying...
public void setDataSource (Context context, Uri uri) Added in API level 10

Sets the data source as a content Uri. Call this method before the rest of the methods in this class. This method may be time-consuming.

Parameters
context the Context to use when resolving the Uri
uri the Content URI of the data you want to play

Throws

IllegalArgumentException if the Uri is invalid
SecurityException if the Uri cannot be used due to lack of permission.

And so I've attempted to use (among plenty of other things)...
B4X:
rfl.RunMethod3("setDataSource", "http", "java.lang.string", vvPath, "java.lang.string")

The variable vvPath is a valid URI as it opens the videoview with the desired file.

Can anyone help to explain where I am going wrong?

Thanks,
RandomCoder
 
Upvote 0

btbb

Member
Licensed User
Longtime User
I'd like to center my Videoview and have tried the sample code kindly provided by bgsoft but it does not work for me as I am passing it a URI instead of a local file path. I've looked at the literature for the MediaMetadatRetriever class (http://developer.android.com/reference/android/media/MediaMetadataRetriever.html) and Google'd for an answer, I'm pretty sure that it can be done but as yet I've not worked out how. I just keep getting a NoSuchMethod java exception.

This is what I think I should be trying...


And so I've attempted to use (among plenty of other things)...
B4X:
rfl.RunMethod3("setDataSource", "http", "java.lang.string", vvPath, "java.lang.string")

The variable vvPath is a valid URI as it opens the videoview with the desired file.

Can anyone help to explain where I am going wrong?

Thanks,
RandomCoder


Did you solve this problem?

btbb
 
Upvote 0

RandomCoder

Well-Known Member
Licensed User
Longtime User
Hi btbb,

I solved my problem, but not using the SetDataSource method as mentioned above as I could not get it to work and no-one else came forward with a solution.

Instead I use an intent to run MXPlayer (or any other installed media player). This has the advantage of automatically centering the video and also supports far more file types.
If no media player apps are detected then I attempt to use a VideoView and I've been able to centre it because I get the resolution information from the results of a UPNP browse request. Therefore I've done it this way instead.
Sorry that I am unable to assist getting the SetDataSource method to work with a URI.

Regards,
RandomCoder
 
Upvote 0

MarcoRome

Expert
Licensed User
Longtime User
I've already got! :)

With this code I get the width and height of the video. So I can calculate the proportion and center the video


B4X:
      Dim vidfile As String
      vidfile = File.Combine(Dir, VideoName)
  
      Dim rfl As Reflector
      Dim obj As Object
      Dim b As Bitmap
    
      obj = rfl.CreateObject("android.media.MediaMetadataRetriever")
      rfl.Target = obj
      rfl.RunMethod2("setDataSource", vidfile, "java.lang.String")
      b = rfl.RunMethod3("getFrameAtTime", 0, "java.lang.long", 3, "java.lang.int")
          
      HeightVideo= b.Height
      WidthVideo= b.Width

regards


Hi bgsoft, can you explain me, with a little example what you do to proportion and center the video ?
Thank you very much
Marco
 
Upvote 0

bgsoft

Well-Known Member
Licensed User
Longtime User
Hi Marco

Hi bgsoft, can you explain me, with a little example what you do to proportion and center the video ?

if you want to put in the center of the screen, you could do this:


B4X:
  Sub Globals
  Dim vv As VideoView
  ' .......................
  ' .......................
  ' .......................



      Dim vidfile As String
      vidfile = File.Combine(Dir, VideoName)
  
      Dim rfl As Reflector
      Dim obj As Object
      Dim b As Bitmap
    
      obj = rfl.CreateObject("android.media.MediaMetadataRetriever")
      rfl.Target = obj
      rfl.RunMethod2("setDataSource", vidfile, "java.lang.String")
      b = rfl.RunMethod3("getFrameAtTime", 0, "java.lang.long", 3, "java.lang.int")
          
      HeightVideo= b.Height
      WidthVideo= b.Width

      vv.Initialize("vv")

     Activity.AddView(vv,(100%x-WidthVideo)/2, (100%y-HeightVideo)/2,WidthVideo ,HeightVideo)

Regards
 
Upvote 0
Top