Android Question YouTube Library and Media Player Audio mix

chris ash

Member
Licensed User
Longtime User
Hi guys

I need a little help to know whether something is possible or not....

So the idea behind the app is to have multiple Mediaplayers each playing a different audio file, which in this case are fruit machine sounds. This is to give the ambeinece of being inside an actual fruit machine arcade. The app has a number of mediaplayers within it, these are activated, loaded with an audio file and then played simultaneously.

What am then doing is it using the YouTube Library to then link these audio clips to a corresponding video file stored on YouTube, thus letting them see the video that corresponds to one of the audio files, but whilst still having the other sounds playing in the background (the ambience). This again on the whole seems to work well.

The Problem

The problem arises when the user presses the home button whilst watching the YouTube video. Everything closes as it should but the Mediaplayer keeps playing. Is there any way to monitor for this or a smilar occurrence so as to stop/pause the Media player as and when needed? I appreciate you cant catch the press of the home button but is there some other way to identify that the Media should not longer be playing (for example when the YouTube activity is paused etc) and stop them?

The dream is to keep the Media players playing alongside the YouTube video. This does work but I seem to be very liable to losing control of the Media players when the YouTube video is being watched :-( If the user presses back when watching the YouTube clip it comes back to the app app activity and it picks up where it left off and the mediaplayers are all stopped when needed.

I have attached the basics of what I have so far...

Any help or thoughts would be much appreciated

CAsh
 

William Lancee

Well-Known Member
Licensed User
Longtime User
In Klaus's Booklets "B4X Basic Language" here is the description of "Activity_Pause".
If you put the code for stopping and closing the mediaplayers there, you may get close to the effect you want.
If you want to keep playing after the Back button, but not in the other 3 cases, test for "UserClosed = True".
For cases 1 and 3, you can RESTART playing in "Activity_Resume", there will be a brief break for an orientation change.

Activity_Pause is called every time the activity moves from the foreground to the background. This can happen because:
1. A different activity was started.
2. The Home button was pressed.
3. A configuration changed event was raised (orientation changed for example).
4. The Back button was pressed.
In scenarios 1 and 2, the activity will be paused and for now kept in memory as it is expected to be reused later.
In scenario 3 the activity will be paused, destroyed and then created (and resumed) again.
In scenario 4 the activity will be paused and destroyed. Pressing on the Back button is similar to closing the activity. In this case you do not need to save any instance specific information (the position of pacman in a PacMan game for example).
The UserClosed parameter will be true in this scenario and false in all other. Note that it will also be true when you call Activity.Finish. This method pauses and destroys the current activity, similar to the Back button.
You can use UserClosed parameter to decide which data to save and also whether to reset any related process variables to their initial state (move pacman position to the center if the position is a process variable).
 
Upvote 0

chris ash

Member
Licensed User
Longtime User
Hi William

Thanks for getting back to me. I am not sure if I did not make it clear or not but your response does not really answer the question.

The desired behaviour is that the media players continue to play whilst the video plays. Like background noise (ambience). This all works and I am fine with activity life cycle and how that works.

My question is what if anything can I do if YouTube fails to return control to my app. As my activities are not running any longer and do not resume as the user has pressed home I don't currently have a way of stopping them.

I am thinking along the lines of a service, can I identify the YouTube instance somehow and have a service that runs and monitors the status for this instance. If it sees YouTube close and my app not restarting then kill the media players... something along those lines? (No idea if this is even remotely possible btw :p )

Or does someone have a better way to implement the YouTube video that would not have this issue and YouTube would be happy for me to use?

Hope this helps clarify the request, and thanks for your support so far.

Cheers
CAsh
 
Upvote 0
Top