MediaPlayer causes rendering problem in Android Studio - android

When I create a MediaPlayer, using the code below, Android Studio keeps showing rendering problem
#Composable
#Preview
fun TestScreen(){
val context = LocalContext.current
val audio = MediaPlayer.create(context, R.raw.testaudio1)
Button(onClick = {audio.start()}) {
Text(text = "play")
}
}
the testaudio1 is an mp3 which I put in a newly made android resource directory in res
res
The render problem is shown below
render-problem
How do I fix this?

To resolve this issue, you may need to make sure that your MediaPlayer is properly initialized and managed in a background thread.
Additionally, you may also need to make sure that you are using the correct context when creating the MediaPlayer object and that you are handling the lifecycle of the MediaPlayer correctly to prevent memory leaks.

Related

How to add Sound Effects with Kotlin in Android

How do you program sound effects in kotlin android Looked all over Yt and there all in Java
Add/Import your sound effect file to your project and use MediaPlayer like this one
private var BgMusic:MediaPlayer? = null
fun playBgMusic(){
BgMusic = MediaPlayer.create(this, R.raw.gamebgmusic)
BgMusic?.setOnPreparedListener{
BgMusic?.start()
musicPlaying = true
}
BgMusic?.setOnCompletionListener {
BgMusic?.start()
}
}
fun pauseBgMusic(){
BgMusic!!.pause()
}
fun resumeBgMusic(){
BgMusic!!.start()
}
Add your sound effect file into your 'resources' folder in your app project, then call it from whichever activity or fragment you would like to execute it in with SoundPool, a public class that enables you to load the audio resource into memory and play it!
I link you here the SoundPool documentation as well as a blogpost that shows it in action.
Here's also a Kotlin tutorialspoint article.

Why does Android Studio display "only be called from with the same library group" when I remove #SuppressLint("RestrictedApi")?

The Code A is from CameraX project, you can see source code.
Android Studio will display "only be called from with the same library group" when I remove #SuppressLint("RestrictedApi"), you can see Image 1.
Why can't I remove #SuppressLint("RestrictedApi") in Code A ? What deos a restriction API mean?
Code A
#SuppressLint("RestrictedApi")
private fun updateCameraUi() {
...
// Listener for button used to switch cameras
controls.findViewById<ImageButton>(R.id.camera_switch_button).setOnClickListener {
lensFacing = if (CameraX.LensFacing.FRONT == lensFacing) {
CameraX.LensFacing.BACK
} else {
CameraX.LensFacing.FRONT
}
try {
// Only bind use cases if we can query a camera with this orientation
CameraX.getCameraWithLensFacing(lensFacing)
// Unbind all use cases and bind them again with the new lens facing configuration
CameraX.unbindAll()
bindCameraUseCases()
} catch (exc: Exception) {
// Do nothing
}
}
}
Image 1
There have been breaking changes in the library since the tutorial was made.
Reverting the package version to 1.0.0-alpha06, same as the tutorial, solves the problem.
These are issues with the library that don't affect your code.
In several code examples using these APIs there is often a #SuppressLint("RestrictedApi") in the file hiding the warning.
The projects should still compile and run as they should, although you must make sure that you are using the correct dependency version. The APIs are changing quite frequently still, and if you're referencing an example it might be using an older version which has since changed.
Your best bet is to look directly at the source code and if the method you are calling is declared as public then you probably won't have a problem.

Samsung AOD and lock screen media metadata Unknown

I do have my own application for media playback - it's using MediaSessionCompat (with combination with ExoPlayer and it's MediaSessionConnector plugin).
On Samsung phone I experience a small issue with AOD (Always On Display) and lock screen. Both contains a small media controller (three buttons) and track title and album - I assume it works with MediaSession.
My problem is that it always shows Unknown / Unknown for title / album (but buttons are working correctly). I'm sure that I'm feeding correctly MediaSession metadata, as it is used in Activities, where using onMetadataChange callback and it contains correct title.
I'm somehow lost, not sure where to look for issue and fix. It's clearly in my app, because other players are working fine (showing title on AOD), but I do not know what else I need to do apart of settings metadata in MediaSession?
So shortly
Exoplayer uses MediaDescriptionCompat to get meta from play queue. Then it maps it to MediaMetadataCompat and title is mapped to key MEDIA_KEY_DISPLAY_TITLE , where Samsung is using key MEDIA_KEY_TITLE only.
Solution was to add MEDIA_KEY_TITLE to MediaDescriptionCompat.extras.
Another item displayed in Samsung AOD, lock screen is MediaMetadataCompat.METADATA_KEY_ARTIST
Another example of useless complexity in Android - why we need to two classes for metadata, which are almost the same MediaMetadataCompat vs MediaDescriptionCompat?
#Ivan's answer worked for me. Here is an example in Kotlin:
mediaSessionConnector = MediaSessionConnector(mediaSession).also {
it.setPlayer(player)
}
mediaSessionConnector.setQueueNavigator(object : TimelineQueueNavigator(mediaSession){
override fun getMediaDescription(player: Player, windowIndex: Int): MediaDescriptionCompat {
val extra = Bundle()
extra.putString(MediaMetadataCompat.METADATA_KEY_ARTIST, "podcastEpisode")
return MediaDescriptionCompat.Builder().setExtras(extra).setTitle("podcastTitle").build()
}
})
This issue was also very helpful.

Libgdx Freetype Font Generator Outline acting weird

I'm using asset manager to generate my font using object
FreeTypeFontGeneratorLoader, specifically I'm using this current parameter
FreeTypeFontLoaderParameter parameter = new FreeTypeFontLoaderParameter();
parameter.fontFileName = "fonts/myFont.ttf";
parameter.fontParameters.size = size;
parameter.fontParameters.minFilter = Texture.TextureFilter.Linear;
parameter.fontParameters.magFilter = Texture.TextureFilter.Linear;
if(withStroke) {
parameter.fontParameters.borderStraight = true;
parameter.fontParameters.borderColor = Color.BLACK;
parameter.fontParameters.borderWidth = 1.5f;
}
parameter.fontParameters.color = Assets.fontColor;
(then I load my font in static variable in Assets class)
everything is normal if the app runs for the first time,
here is the generated text with outline screenshot:
screenshot
but after I close my app using this line
Gdx.app.exit();
and then open my app again, the outline is now starting to acting weird
here is the screenshot of weird generated font:
screenshot
I'm already trying to dispose my asset manager in event dispose of ApplicationListener in Gdx, but sometime my font still acting weird.
if I clear my processing run in all task android, then the font is normal again.
Can you tell me possible solutions of why it is happening, and do you have any suggestions?
Thx
Android sometimes keeps static (final) variables even when the app lost its context.
When using static variables: make sure you dispose when required and set them to null afterwards. Or do not use static for assets.
Since this problem happen because my app not closed completely because this line :
Gdx.app.exit();
is just doing this :
Schedule an exit from the application. On android, this will cause a call to pause() and dispose() some time in the future, it will not immediately finish your application"
that's why I change my way to close my app using this line:
System.exit(0);
then my app is loading and generating new font when user open my app again.

How to create a custom control with MvvmCross Droid VideoView

I have a viewmodel for searching for videos which works great. I have added a play button and upon clicking I want to load a view that renders a VideoView. The search viewmodel contains enough data to generate the video's URL.
What is the best way to render the VideoView?
I've found this example but it appears to be an older version of MvvmCross:
https://gist.github.com/Alphapage/3945799
Should I create custom control like N=18 - Android Custom Controls - N+1 Days of MvvmCross?
http://www.youtube.com/watch?v=s1LhXdCTsn4&feature=youtube_gdata
If I create a custom control how should I pass the video's URL to the VideoView and start it playing?
I'm sure this is easy to do but I can't find a working example.
Thanks in advance
You could create a custom view as Stuart does, but rather than inheriting from View inherit from VideoView.
Then create a property called VideoUri, and when ever it's set call the SetVideoUri and start method on the base object. E.g.
Please note the following code was written in notepad, so may need some tweaking :)
public class BindableVideoView : VideoView
{
private Uri _videoUri = default(Uri);
public Uri VideoUri
{
get{ return _videoUri;}
set{
if(_videoUri!=value)
{
if(base.IsPlaying)
{
base.StopPlayback();
}
}
base.SetVideoURI(value);
base.Start();
}
}
}
You could do things like expose a property for IsPlaying and then your viewmodel could two-way bind to that so your viewmodel would know when you have finished playing the video

Categories

Resources