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.
Related
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.
I'm using PlayerNotificationManager for displaying playback notifications in my application. Everything is working fine but I want to add my application's logo in the notification with custom fonts.
I changed the play & pause buttons on the notification by adding drawables.xml. But can't find a way to change the font.
So, how can I change the default notification layout and characteristics that Exoplayer provides?
Edit:
I have seen this issue on Github which says that we need to extend PlayerNotificationManager in order to use custom layout. But I can't seem to get it working.
Here is my code:
private void setPlayerNotificationManager(Player player) {
playerNotificationManager = new PlayerNotificationManager(context, "123", 1234, mediaDescriptionAdapter);
playerNotificationManager.setUseNavigationActions(false);
playerNotificationManager.setUsePlayPauseActions(true);
playerNotificationManager.setRewindIncrementMs(0);
playerNotificationManager.setFastForwardIncrementMs(0);
//playerNotificationManager.setColor(Color.GREEN);
playerNotificationManager.setColorized(true);
playerNotificationManager.setUseChronometer(false);
playerNotificationManager.setSmallIcon(R.drawable.logo);
playerNotificationManager.setPlayer(player);
}
As said on the repo you need to extends the PlayerNotificationManager to do that you need to create this class and copy everything inside your project, and then add the layout you want to use as described in this link
You need to add your layout in the builder here
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.
I use https://ionicframework.com/docs/native/media/ plugin for play mp3 file in
ionic 2 project like this code :
(put animal audio file in src/assets/audio/animal.mp3)
play(){
const file: MediaObject = this.media.create('../assets/audio/animal.mp3');
file.play();
}
in html
<button (click) = "play()" >Paly</button>
but in android device and when click on play button , I cannot hear any sounds
Try to use NativeAudio plugin (https://ionicframework.com/docs/native/native-audio/). It works really nice for me. Some example:
if (this.platform.is('cordova')) {
this.nativeAudio.preloadSimple('chamada', 'assets/sounds/Umbriel.mp3');
this.nativeAudio.loop('chamada');
}
In this case, nativeAudio is injected NativeAudio module. To stop it, I do:
if (this.platform.is('cordova')) {
this.nativeAudio.stop('chamada');
this.nativeAudio.unload('chamada');
}
You can check more options on documentation. Give it a try... good luck!
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