how to play music on click of an action button - android

i am trying to play music or mp3 on click of an action button but none is working since most of the tutorials are based on button listeners. i need help regarding how to play music on click of an option menu or action bar menu. Can anyone please help me regarding this problem.

Here is a recommended solution to playing a mp3:
Link: Simple mediaplayer play mp3 from file path?
String filePath =
Environment.getExternalStorageDirectory()+"/yourfolderNAme/yopurfile.mp3";
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(filePath);
mediaPlayer.prepare();
mediaPlayer.start()
and this play from raw folder.
int resID=myContext.getResources().getIdentifier(playSoundName,"raw",myContext.getPackageName());
MediaPlayer mediaPlayer = MediaPlayer.create(myContext,resID);
mediaPlayer.prepare();
mediaPlayer.start();

Related

How to set custom music on Android?

I want to make a music player that can play all music on my memory card.
I use this code for play a music, but i can't play music from memory card, only in raw folder.
MediaPlayer mp = MediaPlayer.create(this, R.id.raw.audio.mp3)
what I should do, so i can play music from SD card ?
You'll want to do something like this
Uri song = Uri.parse(location-of-song); //location-of-song is where the music is on the sd card
mPlayer = new MediaPlayer();
mPlayer.setDataSource(getApplicationContext(), song);
mPlayer.start();
Use the version of create that takes a URI, and provide a URI to the local file you want to play.

Set AudioStreamType for an audio file played from Resource (raw folder)

[First App] I am creating a sort of Alarm app that allows user to select alarm sound from either sd-card or app-supplied sounds. Since, the app essentially plays alarms, I want the volume to be 'alarm volume' of the device. I am able to achieve this for sd card sounds. But, I am unable to setAudioStreamType for raw resource sounds.
I am using following code :
MediaPlayer m_player = new MediaPlayer();
m_player.setAudioStreamType(AudioManager.STREAM_ALARM);
switch (bin_name) { //bin_name = various user selectable music files
default:
m_player = MediaPlayer.create(context, R.raw.blu);
break;
}
m_player.setLooping(true);
m_player.start();
My blu.mp3 plays at media volume only. Upon checking the documentation for MediaPlayer.create(Context context, int resid), I found this :
Note that since prepare() is called automatically in this method, you cannot change the audio stream type (see setAudioStreamType(int)), audio session ID (see setAudioSessionId(int)) or audio attributes (see setAudioAttributes(AudioAttributes) of the new MediaPlayer.
I also tried finding code samples for above method but none of them showed how to set AudioStreamType to AudioManager.STEAM_ALARM. I will accept answers with alternative ways that simply play the sound with infinite loop. How to achieve this ?
As the documentation you are referring to says, you must create and prepare the MediaPlayer yourself. Haven't tried with the STREAM_ALARM but I'm using following snippet to play on STREAM_VOICE_CALL
Uri uri = Uri.parse("android.resource://com.example.app/" + R.raw.hdsweep);
MediaPlayer mMediaPlayer = new MediaPlayer();
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);
mMediaPlayer.setDataSource(context, uri);
mMediaPlayer.prepare();
mMediaPlayer.start()

How to get mp3 file uri and play it in android programatically

I want to make a application in which i play the .mp3 file
i play the sound by selecting it from raw or asset folder which is hard coded in code.
But i want that user pick any mp3 file anywhere from their android phone and make play it
Can any one help me
Thanks in advance
MediaPlayer mediaPlayer= MediaPlayer.create(this, R.raw.song);
mediaPlayer.start();
int duration = mediaPlayer.getDuration();
int current_position = mediaPlayer.getCurrentPosition();
mediaPlayer.pause();
Great tutorial on Media Playback through MediaPlayer:
http://www.tutorialspoint.com/android/android_mediaplayer.htm

Event on start play remote mp3 in MediaPlayer

I set in MediaPlayer source as url with remote mp3 file. I want listen, where start playing song.
At start some time need to download mp3 header (caching) and only after start playing song. How I can know about this moment? I know only simple way: check at timer isPlaying status...
If you use Android native MediaPlayer, there's an event called onPrepare which is called when the mediaplayer is ready to play your music
For example how to catch the above event on VideoView.
video = new VideoView(activity);
video.setOnPreparedListener(new OnPreparedListener() {
}

Change media volumn while play music

I use below call to play music:
vv = (VideoView)findViewById(R.id.screen_audio);
Uri music = Uri.parse(path);
vv.setVideoURI(music);
vv.start();
But when I playing, the volumn_up and volumn_down button can not press.
How can modify it to change the media volumn?
You have to use controls to change the volume. Check below example to know about it.
Implement a SeekBar to control the volume of Video Player

Categories

Resources