Android MediaPlayer is paused, volume buttons do not control multimedia - android

I played a sample music (.mp3) using a sample code I found on the Internet.
The code is something like
MediaPlayer mp = new ...
mp.setDataSource(...
mp.setAudioStreamType(...
mp.prepare();
mp.start();
The problem is that when the music is paused, the volume buttons do not control multimedia volume but notification volume.
mp.pause();
This behaviour is different from other music players I have tested. Samsung's built-in Music, VLC, and other programs still showed multimedia volume when the music has been paused.
How can I make multimedia volume appear when music is paused?
PS: It seems other players make pressing volume buttons control multimedia volume, no matter what the current playing status is, even playing has not been started at all. How can I do this?

I had the same problem and I solved it by putting the following code in my onCreate() function.
setVolumeControlStream(AudioManager.STREAM_MUSIC);

Related

Android MediaPlayer keeping background music playing

I use a MediaPlayer and when the MediaPlayer starts it currently stops all background music the phone is playing (ie: Spotify) and I was wondering how to have the spotify music continue playing after the MediaPlayer audio is complete
You cannot do anything on your side. This behaviour is implemented by "Spotify" devs. Although normally, sounds should not be stopped, but decreaced in volume, it is called audio ducking.
Also it is recommended to implement requesting audio focus and decide play or not to play your audio depending on the status of the audio focus.

Is it possible to have volume button control media when media not playing on Android

I am building an audio player, where the audio can be turned on and off.
When the audio is on the volume control button on the phone properly controls the media volume.
However when the audio is off, the volume button controls the phone ringer volume.
Is it possible to configure the volume button such that it will control the media volume even when media is not playing?
In your activity's onCreate() you can do:
setVolumeControlStream(AudioManager.STREAM_MUSIC);
like described in How to control media volume?

Playing music files in Android

I have a list of music files that should be played with my Android application. Actually user selects a list of files to be played for a specified time, and then clicks on 'start button'. Please tell me how I can implement it so that the following requirements are met:
The program should work, even if power button is pressed or the display is turned off.
Music files should be played one by one.
In order that the music continue after your application activities exit, you need to implement the playing itself as a Service (specifically, a started service). For playing the music, take a look at MediaPlayer. There is a discussion of using a MediaPlayer in a Service in the Media Player guide topic.
To play audio files, you can use Android's MediaPlayer class.
MediaPlayer mp = new MediaPlayer();
mp.setDataSource(audioFilePath);
mp.prepare();
mp.start();
This code snippet will load and play a single audio file. Take a look at the documentation for MediaPlayer to figure out how you might be able to leverage it for what you're trying to accomplish specifically.

Android: when implementing a music player in my app, can i make sure there is no other media playing playing music right now?

I want my app to play music while it is running, but I don't want the music to overlap with the music currently being played from a different application (android music app or other external music app such as pandora, grooveshark or winamp).
My question is: is there a way to make sure that nothing else is playing right now regardless of the source?
Thanks!
As of android 2.2 you can use AudioManager.requestAudioFocus(), other audio players should listen for this focus change request and can stop/pause/lower volume of their audio according to what type of audio your focus requests. However not all audio playing apps have bothered to implement this yet.
To be nice you should also listen for audio focus change requests from other apps and pause your apps audio accordingly.
On earlier versions of android calling mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); will usually stop any other music from playing

Android: I need a button that kills anything played by the MediaPlayer

I have an app that plays several different audio files on a loop, via several different buttons. I want to program a single button that will stop the MediaPlayer, regardless of what audio file is currently being played. Any help with this would be greatly appreciated.
Call stop() on your MediaPlayer object to stop the playback of whatever the MediaPlayer is playing.

Categories

Resources