Wish Mediaplayer IsInitialized function

udg

Expert
Licensed User
Longtime User
Hi all,

please find attached a very short and schematic app that shows why an IsInitialized function could be useful for the Mediaplayer object.

A java exception is raised when you exit the app and soon after that you launch it again.
This derives from the code in the FirstTime section in Create sub that is not executed under the above situation due to the fact the app is still in memory and so FirstTime return False because the process was not yet destructed by the garbage collector.

I undertsnd the "problem" could be circumvented in various ways, but maybe testing for mp.isInitialized followed by the Init/load istructions where appropriate could make it simpler and more readable.

Umberto
 

Attachments

  • mplayertest.zip
    80.9 KB · Views: 160

udg

Expert
Licensed User
Longtime User
Why do you call mp.Release in Activity_Pause?

To release any mp-related memory when the user closes the app.
Mp is a process global. Did I post the wrong code?

Anyway, my test is as follows:
B4X:
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   Dim mp As MediaPlayer
   Dim CanPlay As Boolean
End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
  Dim btnStart As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
   'Do not forget to load the layout file created with the visual designer. For example:
   'Activity.LoadLayout("Layout1")
  If FirstTime Then
     mp.Initialize
     mp.Load(File.DirAssets,"provasound3.ogg")
     mp.Looping = True
   End If
   CanPlay = False
   btnStart.Initialize("Music")
   Activity.AddView(btnStart,10dip,10dip,50dip,50dip)
   btnStart.Text = "Play"
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
  If UserClosed Then
    mp.Release
  End If  
End Sub

Sub Music_Click
  CanPlay = Not(CanPlay)
   If CanPlay Then
    mp.Play
    btnStart.Text = "Stop"
   Else
    mp.Pause
    btnStart.Text = "Play"
   End If
End Sub

Can you explain more about the GC role? I mean, my understanding is that the OS activates GC when it's time to recover some working memory. And that's the time when any "pending" process gets deleted. With pending here I mean those like the above, where the user exited an app but the OS leaves it there just in case it is launched again.

I understand that eliminating the call to mp.release solves the above problem for the situation outlined, so my request depends more on when it is suggested to eventually call Release.

Umberto
 
Last edited:

udg

Expert
Licensed User
Longtime User
Ok, thank you.
So when a process global object has a release method, we are encouraged not to use it unless we are sure we don't need it any more.
Am I right?

Umberto
 

udg

Expert
Licensed User
Longtime User
Thank you very much.

Out of curiosity, when you designed B4A why did you reserve a proper space for Sub Process_Globals but didn't associate a correspondig Process_Create/Destroy where to use that (at first) confusing FirstTime boolean?
I mean, clearly distinguishing between process and activities would have been easier to understand for first time users.
Mine is only curiosity, not a new wishing.. :)

Umberto
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
This is a good question.
I'm not sure what is simpler, two "starting points" - Sub Process_Create and Sub Activity_Create or a single one combined like we now have.
I think that in both cases the developer needs to learn the difference between activity globals and process globals.
...
The more I think about it now, I like the idea of adding a Process_Create sub. It can solve a common cause of errors where the app starts from a service and calls such as Main.SQL1.ExecQuery fail because SQL1 is initialized in Activity_Create.
 

udg

Expert
Licensed User
Longtime User
Hi Erel,
I understand how difficult could be to introduce a different approach on a well-established product like B4A, so it's understandable it deserves all the time for a thorough and deep thinking prior to take any decision.

If at any time you will feel ok to change the starting approach, may I suggest the introduction of a separate "Process only" module?

That way we will have a single point for process global vars, a process_create function (and maybe a process_destroy one) common to all applications where activities, services and other modules will live independently (sort of) but at the same time could count on the existence of that common ground.
I don't know if Android permit it, but with the above scheme we could even have an app without any Activity (I'm thinking about a pure service that many apps could use once started).

Well, I am pretty sure you don't need my hints and eventually will find a proper way to manage a sharable evolution for your great products.

Umberto
 
Top