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.
Related
I am using sound pool to play a short sound files in android, but I want to play the sounds at for example, If I clicked a button to play a sound and clicked the button again, I don't want to play the sound as long as it is already playing . how ?
It looks like you should use a MediaPlayer Class instead of soundpool to accomplish this result, in combination with an OnClickListener.
A simple StackOverflow search returns the following:
MediaPlayer
My app plays an audio file. I want my app to pause the audio playing if another apps starts to play audio. I don't want the two audio sounds mess up each other. My app will resume the audio playing after the other app finishes playing its audio. How can I make it? Thanks.
Depending on how your application is running ie. while app is running in foreground (users is looking at your screen) or if your running a "service" which is currently in the background and you wish music to continue playing (songza, slacker radio etc.) how you handle these things will be different. Android uses internal system states to determine which apps should be out-putting audio to the speaker. All this is handled by the audio manager requestAudioFocus() releaseAudioFocus() will be the system calls you will want to look at the most. For a more detailed explanation see android developer api where you this is explained and documented quite well http://developer.android.com/guide/topics/media/mediaplayer.html
I'm trying to implement a 'click' sound when the user interacts with buttons in the main screen of my application. I have no experience with audio in Android, however several sources suggest that to simply play a clip, I need only do the following -
Put the audio file in /res/raw
I did this, I had to create a /raw folder within /res/ - the file is named menuclick.wav (so seems to meet the guidelines for a-z0-0_ filenames)
Create a MediaPlayer object
MediaPlayer mediaplayer;
mediaplayer.create(getApplicationContext(), R.raw.menuclick);
Prepare the player for playback (*within a try - catch block)
mediaplayer.prepare();
Play the file
mediaplayer.start();
A sound plays, however it is not the sound file I specified, it is an unpleasant hissing / static sound.
I tested the application again, only changing the sound file. It played some of the sound but there was still a lot of white noise. This doesn't occur when opening the sound files normally in other applications.
I seem to be following the same steps as several textbooks / examples. Any idea where the problem may lie?
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
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.