Videoview stealing mediabutton event when app has MediaBrowserService - android

I am using a Videoview to play an animation video in the loop. I noticed that whenever the video is playing if any other music app is playing audio and I click on headset buttons(pause/play), the media button events are not dispatched to the music playing app. Instead, it is dispatched to my ExampleService which extends from MediaBrowserServiceCompat. Videoview is stealing those media button events. I am using even AudioManager.AUDIO_FOCUS_NONE.
I noticed when my app opens and video plays, I can see a log in the stack trace that
D/MediaSessionService: Media button session is changed to com.example.mediasessiontest/ExampleService (userId=0)
Is there a way to play video without requesting for Mediabutton event?
If I don't play video in my app, headset events are correctly routed to the app which was playing music
This is happening only on Android 8 and 9. Works fine on Android 10.
Tried even Surfaceview with Mediaplayer to play video. But observed the same issue.
private val VIDEO_BASE_PATH = "android.resource://"
videoView.setAudioFocusRequest(AudioManager.AUDIOFOCUS_NONE)
videoView.setMediaController(null)
videoView.setVideoPath(VIDEO_BASE_PATH + packageName + "/" + R.raw.test_video)
videoView.setOnPreparedListener { player ->
player.isLooping = true
player.start() //This causes the media button sesssion to be changed
}
Tried using Surfaceview with Mediaplayer as well. But same result. Looks like a bug in OS 8 and 9 ?

Related

Exoplayer change current video seamlessly

I want to change current playing video to another seamlessly. But there is a little bit delay before the next video will be playing. I don't know what the next video will be. How I can do this?
You can use playlists with ExoPlayer. This way the playback transitions at the end of item 1 to 2 without buffering.
DynamicConcatenatingMediaSource mainSource = new DynamicConcatenatingMediaSource();
mainSource.addMediaSource(mediaSource1);
player.prepare(mainSource);
// later...
mainSource.addMediaSource(mediaSource2);
As soon as you know the second media you can add it while the player is playing.
https://medium.com/google-exoplayer/dynamic-playlists-with-exoplayer-6f53e54a56c0

Android interrupt media player with ringtone manager

I have used ringtonemanager previously in android studio and it seemed to lower the volume of any music that was playing in a different app to play the alert i was trying to play, then once my alert had completed the background music would then come back to normal volume (as the default alarm/notification would do) But now a year or so later im trying to implement this again but my alert cannot be heard over the music playing in Google Play Music.
Is this a change that now requires additional parameters to function as it used to?
Im using:
Uri notification =
RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
myRM = RingtoneManager.getRingtone(this.getContext(), notification);
myRM.play();
Many Thanks
Handling Changes in Audio Output seems what fits your need.
In short, you need to request audio focus before starting playing.
...
mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
mAudioManager.requestAudioFocus(null, mStreamType, AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK);
mRingtone.play();
AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK
Used to indicate a temporary request of audio focus, anticipated to
last a short amount of time, and where it is acceptable for other
audio applications to keep playing after having lowered their output
level (also referred to as "ducking").
Remember to release audio focus when finishing..
if (mRingtone != null && mRingtone.isPlaying()) {
mRingtone.stop();
}
mRingtone = null;
if (mAudioManager != null) {
mAudioManager.abandonAudioFocus(null);
}

Android Nexus Lollipop mediaplayer is mediaplaying seeking continuously

We are trying play audio from url (m3u8 file). Media player starts fine no issues. Audio also plays cleanly. Issue starts when do seek in the player. Behavior very strange. it seeks to the proper position then starts playing audio. After while it seeks again like couples seconds (better word is skips some seconds since it jumps to the position directly) and can be observed in the media playback time counter, again plays for a while again jumps some seconds and this continues till end of the media.
We have our custom seek bar which is nothing but a progressbar, and when we do seek the progress bar we send same seek position to media player by calling onseek() method.
Note: Issue Happens only Lollipop nexus devices(tablet and phone).
Strange observation jump happens only if the time counters last position 9
(i.e if mediaplayed 12:29[mm:ss] then will jump to some other random place 12:3X[mm:ss],again mediaplayed 12:39[mm:ss] then will jump to some other random place 12:4X [mm:ss] )
Why is it happening?
You should pass the outer manifest to the player. This should resolve your issue.

Android: MediaPlayer.seekTo inaccureate on streaming video (m3u8)

I'm writing a video player using the VideoView class. I have a "forward" button and I want the player to seek a 10 seconds further, when a user presses it:
if (mVideoView.canSeekForward()) {
int position = mVideoView.getCurrentPosition() + SEEK_TIME;
mVideoView.seekTo(position);
}
But almost always the position after seeking differs much from the desired: for example if position was 33458 in the code above, in my OnSeekCompleteListener.onSeekComplete I would get something like 29742 after calling mVideoView.getCurrentPosition()
This happens with this video and any other m3u8 videos I tried (on Nexus 7, API 19 and Xperia Z, API 17). But with some other streaming videos I don't encounter this bug.
Are there any workarounds? When I tried to play this video in MX Player, it had no problems with seeking, but probably it doesn't use the standard components

How to make play music button in Android?

Just to specify the question a little more, I will give an example. I have a car stereo and when I get in the car I can just press play and it plays the last song I was listening to on my phone. So what I need is like a very general Android button that does just that. I just want this button to start media. It's difficult to explain but I know like Google Music when you open it always has a song at the bottom that you can press play. So it's kind of like just continuing a previous queue? If more details are needed please let me know. Sorry I do not have any code for this problem because I have never worked with media but if you would like the source to my app that this code will go into please visit
http://mellowdev.net
The app is called Mango. Open source.
EDIT: I have tried the following but nothing plays.
MediaPlayer mediaPlayer = new MediaPlayer();
int position = mediaPlayer.getCurrentPosition();
mediaPlayer.seekTo(position);
mediaPlayer.start();
As I understand you want to play the music from last known position (or better say from the last known time of the sound). It's something similar like the user change the orientation then you want to play the sound from current position and don't want to start the sound again.
Therfore you have two method in the MediaPlayer:
int position = mediaPlayer.getCurrentPosition();
mediaPlayer.seekTo(position):
I think this post will help you (the example is for a VideoView, but it should be similar to the MediaPlayer).
Hope this will help you :)
EDIT (2013 08 16)
// create a MediaPlayer Instance with the sound you want to play
MediaPlayer mediaPlayer = MediaPlayer.create(context, mySound);
I couldnt't exactly understand your code. But if you need to play media in android, you need to create instance of MediaPlayer and can start to play using a button. Below link will give you more insight:
http://developer.android.com/guide/topics/media/mediaplayer.html
Sorry if I mis understood your question.
To just launch music player:
Intent intent = new Intent(MediaStore.INTENT_ACTION_MUSIC_PLAYER);
startActivity(intent);

Categories

Resources