Playing video's audio in background during screen off, android? - android

I have an application which uses exoplayer to play videos.
When the user is on player page and presses the power button to close the screen I want the audio to keep playing in the background and notification should be visible to user with controls of play, pause , video metadata etc similar to what we have for every music playing app.
I can keep the audio of video playing from exoplayer using setPlayWhenReady(true).
But I am stuck in for notification. Should I be using MediaBrowserServiceCompat or I will have to create custom notification to handle it?

I think what you should do is to create ForegroundService and create notification as you mentioned to have the ForegroundService working. I think this post might help you:
https://androidwave.com/foreground-service-android-example/

Use a foreground service to implement something like this.This can help you

Related

Android ExoPlayer demo: continue playback with screen locked

I want to modify the ExoPlayer demo to allow audio playback to continue when the screen gets locked (this should work regardless of whether the media being played is audio or video). Based on some hints (e.g. 1, 2) this is what I came up with so far:
https://github.com/google/ExoPlayer/compare/release-v2...sedubois:background-playback
This is directly based on the ExoPlayer demo code, currently in version 2.12.1. It adds a "foreground" permission, registers a service, and creates a notification in that service (according to the documentation this notification is required for background playback). The service is started when initializing the player.
I can start the player and the demo looks as illustrated below with an audio stream. At this point the notification gets created properly, it shows the player details (title, description, progress bar) and control buttons (see picture below), which work (I can play/pause, restart from the beginning and skip backwards/forwards).
However the playback still stops when I minimize the application or when I lock the screen by pressing the power button. What is the proper way to make this work?
Just to make sure we are on same page. I'm testing on dev-v2 branch of exoplayer repo here
onStop() method in PlayerActivity.java is releasing player, So when app goes in background then onStop() is being called and it is releasing player with method releasePlayer.
If you'll comment out that method i.e. dont release player in onStop(), then player will play in background.

How to play audio with ImageButton and Exoplayer

I am able to play audio using exoplayer inside and Activity
My Activity that play the music. The activity also contains MediaSessionCompat.Callback that let for exemple notification to play that audio is available in this gist
and also using App Widget.
PendingIntent playPausePendingIntent = MediaButtonReceiver.buildMediaButtonPendingIntent(context,
PlaybackStateCompat.ACTION_PLAY_PAUSE);
views.setOnClickPendingIntent(R.id.btnPlay, playPausePendingIntent);
views.setImageViewResource(R.id.btnPlay, icon);
My problem is how can I play ongoing music inside an ImageButton when I leave the activity?
Please find image below.
Best practice for audio apps is having the player in a foreground services. This service runs independent from activities and hence the player survives when your activity is destroyed.
So to answer your question: your image button starts a foreground Service or sends a message to a service to play audi.
You may consider using a MediaBrowserService which is actually designed for this: https://developer.android.com/guide/topics/media-apps/audio-app/building-an-audio-app.html
The Anroid Universal Audio Player (UAMP) is a sample app which does just this: https://github.com/googlesamples/android-UniversalMusicPlayer/tree/master/mobile/src/main/java/com/example/android/uamp

Unable to play media player in Background

I have Implemented A media player But the Problem is that when i press back button .music stop.but it runs when the app is in background.
Proper approach is to implement media-player inside service. That way music playback will persist when users uses other applications or gets outside of your app.
Remember to call startForeground() with ongoing notification.

how to detect when Music is played using Native Music Player?

I am making an app in which I've made a Service which plays Music from URLs. The thing is that my music Service is playing music correctly BUT when user plays any song with Native music player then BOTH(Native Player and My Music Service) are playing music simultaneously. I want to stop My Music Service when user started playing music with native player.
Is there any Broadcast Intent which i can register to Detect the
music player is started?
Is it possible to detect Music player Started?
Any Other Solution?
Any suggestions would appreciated.
I'll suggest a different approach, that I believe it's the correct approach.
the issue on your approach is that you're suggesting to check for one specific app. And there're tons of different music players, plus radio players, plus video players, plus games... and all of those should stop your music in case they want to play something.
So how you do it?
It's all explained in the Android Developers website.
You have to register an OnAudioFocusChangeListener, so, whenever a different app request to have the audio focus, your app can stop the music.
Step 1: Detect if the user has opened native music app. For this , you need to know the package name of your native music app.
Then refer to my answer here: Android how to know an app has been started and range apps priority according the starting times
Using that , the list taskinfo will have the list of all running activities, and as explained there, the first element of the list will be the activity in the foreground.
STEP 2: Once you detect native music app being activated using STEP 1 (by polling for it in the background) , then stop your app's service.
NOTE: You should do this in a background (using asynctask) or another service.
NOTE 2: The limitation of this method is that you can't actually stop the music player when the user clicks play in the native music app, since this method will help you detect only if the native music app is opened or not.

how to know when audio is started playing in android

In my application I want to get start time of music player when user started and end time when it stops.I don't want to start any music player in my app. I just want to track user activity in device. So i want my application to get any notification when user started the music .
Do I get any intent for music player started and it stopped.Or do I get any intent for when user opens music files.
Is there any other method other than intent to capture the start time and and time when user starts music player.
Well you can implement the interface 'AudioManager.OnAudioFocusChangeListener' in which there is a method 'onAudioFocusChange' which lets you know if audio focus has been changed, and it can also tell if focus was gained or lost.
See this link, it explains the Audio Focus in detail. An application must gain audio focus through a request, and you can implement the 'AudioManager.OnAudioFocusChangeListener' and if there is focus gain, you can detect if the media player is running, (because some other application such as you tube may gain focus to play its audio), see this to see how to detect which application or service is currently running. You can find if audio has started to play, and if it was the media player or not.
Hope this helps. It was interesting question and I have learned some new things while searching for the answer!

Categories

Resources