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);
Related
Getting E/MediaPlayer: Error (1,-2147483648) while trying to play android MediaPlayer . Permission is granted , file format is 3gp . I have been trying to apply answer given to this topic in my code but nothing solve the issue.
player = new MediaPlayer();
File file = new File(filePath);
file.setReadable(true,false);
FileInputStream inputStream = new FileInputStream(file);
player.setDataSource(inputStream.getFD());
inputStream.close();
player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
flag=true;
}
});
player.prepareAsync();
change sequence of the line as below
player = new MediaPlayer();
File file = new File(filePath);
file.setReadable(true,false);
FileInputStream inputStream = new FileInputStream(file);
player.setDataSource(inputStream.getFD());
inputStream.close();
player.prepareAsync();
player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
flag=true;
}
});
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 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()
i'm developing a little quizz app and i would like to play sound depending on if the user answers the question correctly or not.
I've got many sound files in "assets/".
What I would like to do is to play just one of these sounds.
I've been able to play the sound, but not only one, both sound (correct and incorrect) are played one after the other.
How can i play just one?
Here's my source:
public void audioPlayer(){
//set up MediaPlayer
mp = new MediaPlayer();
try {
String mp3File = "correct.mp3";
AssetManager assetMan = getAssets();
FileInputStream mp3Stream = assetMan.openFd(mp3File).createInputStream();
mp.setDataSource(mp3Stream.getFD());
mp.prepare();
mp.start();
} catch (Exception e) {
e.printStackTrace();
}
}
Put your sound files i.e fail and applause in raw folder and use the following code :
private AssetFileDescriptor sPlaySoundDescriptor = null;
private AssetFileDescriptor sFailSoundDescriptor = null;
private MediaPlayer mp = null;
public static final int FAIL_SOUND = 1;
public static final int APPLAUSE_SOUND = 2;
public void audioPlayer(int soundType){
//set up MediaPlayer
mp = new MediaPlayer();
AssetFileDescriptor soundFileDescriptor = null;
try {
if (sFailSoundDescriptor == null) {
sFailSoundDescriptor = context.getResources().
openRawResourceFd(R.raw.fail);
}
if (sApplauseSoundDescriptor == null) {
sApplauseSoundDescriptor = context.getResources().
openRawResourceFd(R.raw.applause);
}
switch (soundType) {
case FAIL_SOUND:
soundFileDescriptor = sFailSoundDescriptor;
break;
case APPLAUSE_SOUND:
soundFileDescriptor = sApplauseSoundDescriptor;
break;
}
mp.reset();
mp.setDataSource(soundFileDescriptor.getFileDescriptor(),
soundFileDescriptor.getStartOffset(),
soundFileDescriptor.getDeclaredLength());
mp.prepare();
mp.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer player) {
player.seekTo(0);
player.start();
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
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