I have a very simple mediaplayer that play background. It calls file from the apk, but I want it to play from any directory like as music or sdcard.
Here is my code:
private MediaPlayer mpintro;
.
.
mpintro = MediaPlayer.create(this, R.raw.intro);
mpintro.setLooping(true);
mpintro.start();
It works like this:
mpintro = MediaPlayer.create(this, Uri.parse(Environment.getExternalStorageDirectory().getPath()+ "/Music/intro.mp3"));
mpintro.setLooping(true);
mpintro.start();
It did not work properly as string filepath...
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();
mycontext=application.this. use.
Here is the code to set up a MediaPlayer to play off of the SD card:
String PATH_TO_FILE = "/sdcard/music.mp3";
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(PATH_TO_FILE);
mediaPlayer.prepare();
mediaPlayer.start()
You can see the full example here. Let me know if you have any problems.
Use the code below it worked for me.
MediaPlayer mp = new MediaPlayer();
mp.setDataSource("/mnt/sdcard/yourdirectory/youraudiofile.mp3");
mp.prepare();
mp.start();
I use this class for Audio play. If your audio location is raw
folder.
Call method for play:
new AudioPlayer().play(mContext, getResources().getIdentifier(alphabetItemList.get(mPosition)
.getDetail().get(0).getAudio(),"raw", getPackageName()));
AudioPlayer.java class:
public class AudioPlayer {
private MediaPlayer mMediaPlayer;
public void stop() {
if (mMediaPlayer != null) {
mMediaPlayer.release();
mMediaPlayer = null;
}
}
// mothod for raw folder (R.raw.fileName)
public void play(Context context, int rid){
stop();
mMediaPlayer = MediaPlayer.create(context, rid);
mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
stop();
}
});
mMediaPlayer.start();
}
// mothod for other folder
public void play(Context context, String name) {
stop();
//mMediaPlayer = MediaPlayer.create(c, rid);
mMediaPlayer = MediaPlayer.create(context, Uri.parse("android.resource://"+ context.getPackageName()+"/your_file/"+name+".mp3"));
mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
stop();
}
});
mMediaPlayer.start();
}
}
2020 - NOV
This worked for me:
final File file = new File(getFilesDir(), "test.wav");//OR path to existing file
mediaPlayer = MediaPlayer.create(getApplicationContext(), Uri.fromFile(file));
mediaPlayer.start();
In Kotlin:
1) The file is in a resource folder :
var nowLesson =resources.getIdentifier("test.mp3", "raw", packageName)
mediaPlayer = MediaPlayer.create(applicationContext, nowLesson)
mediaPlayer.start()
2) The file is in a Path (file path)
val file = File(
this.getDir("Music", MODE_PRIVATE),
"/t.mp3"
)
mediaPlayer = MediaPlayer.create(this,Uri.fromFile(file ))
mediaPlayer.start()
Related
I tried the below code to play the mp3 file but it plays all the ringtones.
Can anyone help me why the ringtone is being played?
MediaPlayer mMediaPlayer1 = new MediaPlayer();
AssetFileDescriptor fd = context.getResources().openRawResourceFd(R.raw.dialtone);
FileInputStream in = new FileInputStream(fd.getFileDescriptor());
mMediaPlayer1.setDataSource(in.getFD());
mMediaPlayer1.setLooping(true);
mMediaPlayer1.prepare();
mMediaPlayer1.start();`
Try this:
public static void startSound(Context context, int soundResId) {
if (mMediaPlayer != null) {
mMediaPlayer.reset();
mMediaPlayer = null;
}
mMediaPlayer = MediaPlayer.create(context, soundResId);
mMediaPlayer.start();
}
then
startSound(this, R.raw.dialtone);
when the sound playback is repeated for a long time. when the example is clicked 10 times He does not play. what is the reason why it plays when you restart the application
mediaPlayer = MediaPlayer.create(mContext, R.raw.click);
mediaPlayer.setVolume(7f, 7f);
mediaPlayer.start();
mediaPlayer.reset() will help you!
Ex:
public static void startSound(Context context, int soundResId) {
if (mMediaPlayer != null) {
mMediaPlayer.reset();
mMediaPlayer = null;
}
mMediaPlayer = MediaPlayer.create(context, soundResId);
mMediaPlayer.start();
}
i am playing mp3 sound using default media player of android, my code for plying sound below
MediaPlayer mp=MediaPlayer.create(hello.this,R.raw.abc);
if(mp!=null)
{
mp.start();
}
i need to play same sound on same button click. when i click continuously on button after some time sound is not working and i getting error MediaPlayer(7395): error (-19, 0).
Does any body have idea for this issue,
Please send me
Thank in advance.
call
mp.reset();
because ever time you are decalaring object
or declare on class level Mediaplayer OBject
Make global variable of mediaplayer and try this way
MediaPlayer mp;
if (mp!=null) {
mp.stop();
mp.release();
}
mp= MediaPlayer.create(hello.this,R.raw.abc);
mp.start();
mp = new MediaPlayer();
mp.create(this, R.raw.testmed);
mp.setVolume(100, 100);
mp.setOnPreparedListener(this);
mp.prepare();
Then you will need to define this and it should work:
public void onPrepared(MediaPlayer player) {
mp.start();
}
you can play music like below
// for play the song
MediaPlayer mp = new MediaPlayer();
try
{
mp.reset();
mp.setDataSource(songPath);
mp.prepare();
mp.start();
btnPlay.setBackgroundResource(R.drawable.img_btn_pause);
} catch (IOException e) {
Log.v(getString(R.string.app_name), e.getMessage());
}
I have found solution for this issue. i have correct this issue using below code, May help other also.
Thanks
Call Method like
PlaySound(R.raw.abc);
//Method
private void PlaySound(int Sound_id)
{
mplayer = MediaPlayer.create(Act_Oceanwaves.this,Sound_id);
if(mplayer!=null)
{
mplayer.start();
}
mplayer.setOnCompletionListener(new OnCompletionListener()
{
#Override
public void onCompletion(MediaPlayer mp)
{
mp.release();
}
});
}
I have a mp3 file in my android mobile, lets it's a xyz.mp3 somewhere in my sdcard.
How to play it through my application?
Simply you can use MediaPlayer and play the audio file. Check out this nice example for playing Audio:
public void audioPlayer(String path, String fileName){
//set up MediaPlayer
MediaPlayer mp = new MediaPlayer();
try {
mp.setDataSource(path + File.separator + fileName);
mp.prepare();
mp.start();
} catch (Exception e) {
e.printStackTrace();
}
}
If the audio is in the local raw resource:
MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.sound_file_1);
mediaPlayer.start(); // no need to call prepare(); create() does that for you
To play from a URI available locally in the system:
Uri myUri = ....; // initialize Uri here
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(getApplicationContext(), myUri);
mediaPlayer.prepare();
mediaPlayer.start();
#Niranjan, If you are using a raw file from res/raw folder, ie., reading a file stored inside the project, we can use:
mediaplayer.setDataSource(context, Uri.parse("android.resource://urpackagename/res/raw/urmp3name");
If you have to use from SD card:
MediaPlayer mediaPlayer = new MediaPlayer();
File path = android.os.Environment.getExternalStorageDirectory();
mediaPlayer.setDataSource(path + "urmp3filename");
See this related question: MediaPlayer issue between raw folder and sdcard on android
public class MainActivity extends Activity implements OnClickListener {
Button play;
MediaPlayer mp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
play=(Button)findViewById(R.id.button1);
play.setOnClickListener(this);
}
#Override
public void onClick(View arg0)
{
mp=MediaPlayer.create(getApplicationContext(),R.raw.song);// the song is a filename which i have pasted inside a folder **raw** created under the **res** folder.//
mp.start();
}
#Override
protected void onDestroy() {
mp.release();
super.onDestroy();
}
}
The replay from https://stackoverflow.com/users/726863/lalit-poptani is great one, it worked the first time, but as I used to have the full path of the file, I did it this way
public void audioPlayer(String path){
//set up MediaPlayer
MediaPlayer mp = new MediaPlayer();
try {
mp.setDataSource(path );
mp.prepare();
mp.start();
} catch (Exception e) {
e.printStackTrace();
}
}
Credit to http://www.helloandroid.com/tutorials/how-play-video-and-audio-android
E/MediaPlayer(20473): error (-19, 0)
I/MyApp (20473): Decoding lala.mp3
I/StagefrightPlayer( 68): setDataSource('mypath')
E/AudioFlinger( 68): no more track names available
E/AudioTrack( 68): AudioFlinger could not create track, status: -12
E/AudioSink( 68): Unable to create audio track
Does any1 know why i'm getting this? This usually happens after playing about 100+ of so audio files using mediaPLayer. I'm playing it like this
public RenderResultFormat DoIt() {
if(mp!=null){
mp.release();
mp = null;
}
AudioRenderer mr = new AudioRenderer(); mp = mr.AudioRenderer(filePath);}
private class AudioRenderer extends Activity {
private MediaPlayer AudioRenderer(String filePath) {
//delcare mediaplayer variables, path etc
mp= MediaPlayer.create(this, path);
if(mp != null){
int duration = mp.getDuration();
mp.start();
try {
Thread.sleep(duration);
} catch (InterruptedException e2) {
e2.printStackTrace();
System.out.println("I've been interrupted >:(");
}
}
}return mp;}
Am i missing something? Quite new to android development. Thank you
This solution works well for me (playing a ressource). Just implement an OnCompletionListener
private void playbeep(int id) {
MediaPlayer mPlayer;
mPlayer = MediaPlayer.create(context, id);
mPlayer.start();
mPlayer.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}