I try to run file mp3 with volume of ringtone (not of media, alarm or notification), but it not working. My code:
AudioManager mAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
mAudioManager.setMode(AudioManager.MODE_IN_CALL);
mAudioManager.setStreamVolume(
AudioManager.STREAM_RING,
mAudioManager.getStreamMaxVolume(AudioManager.STREAM_RING),
AudioManager.FLAG_PLAY_SOUND);
mAudioManager.setSpeakerphoneOn(true);
MediaPlayer mp = new MediaPlayer();
try {
mp.setDataSource(this, Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.nhaccho));
} catch (IOException e) {
e.printStackTrace();
}
mp.setAudioStreamType(AudioManager.STREAM_RING);
mp.setLooping(true);
try {
mp.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mp.start();
please help. thanks!
Related
I am having an odd issue where my audio file sometimes plays and sometimes does not play.
The catch is that when it decides to not play, the LogCat gives me this message:
Should have subtitle controller already set
This is my code to play the sound:
public void sound(){
//store the sound file name
String filename=f_fruit_ar.m4a;
//All sounds stored in assets folder
try{
AssetFileDescriptor afd = getAssets().openFd(filename);
MediaPlayer player = new MediaPlayer();
player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
player.prepare();
player.start();
}
});
}
catch (Exception e) {
e.printStackTrace();
}
}
I think the problem is your not releasing the resources once player is finished playing you need to release the resources.
EXAMPLE:
public void sound(){
//store the sound file name
String filename=f_fruit_ar.m4a;
//All sounds stored in assets folder
try{
AssetFileDescriptor afd = getAssets().openFd(filename);
MediaPlayer player = new MediaPlayer();
player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
player.prepare();
player.start();
player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
player.release();
}
});
}
catch (Exception e) {
e.printStackTrace();
}
}
mediaPlayer=new MediaPlayer();
mediaPlayer.reset();
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mediaPlayer.start();
}
});
try {
mediaPlayer.setDataSource("192.168.191.1/test.mp3");
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.prepareAsync();
above is my code. It works well from such url: http://programmerguru.com/android-tutorial/wp-content/uploads/2013/04/hosannatelugu.mp3
But it don't work when I try to access mp3 file in my server, 192.168.191.1/test.mp3, and I use wamp for my server.
I had the same requirement and it is working on my end :
MediaPlayer player = new MediaPlayer();
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setDataSource("mp3 link"
);
player.prepare();
player.start();
} catch (Exception e) {
// TODO: handle exception
}
This is the function that suppose to play some sound when a notification is set
private void playRingtone() {
AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
if (am.getRingerMode() == AudioManager.RINGER_MODE_NORMAL) {
try {
MediaPlayer mp = MediaPlayer.create(this, R.raw.notification_ringtone);
mp.start();
} catch (IllegalStateException e) {
Log.d(TAG, "MediaPlayer problem");
e.printStackTrace();
}
}
}
using the debugger everything goes smoothly but no sound is played. WHY???
MediaPlayer mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource("android.resource://YOURPACKAGE_NAME/raw/"+file anme);
mediaPlayer.prepare();
mediaPlayer.start();
}catch(Exceptions e){
}
Don't get the wrong sound when playing, when I enter text in English then I'll get everything and the result gives sootvetstvuyushaya word,
but when I change the url to: http://translate.google.com/translate_tts?tl=ru&q=привет and open computer browser then I'm fine but when I enter the url in the source code that gives me no intelligible speech, give that to me to do
This is my code:
public void onClick(View v) {
MediaPlayer player = new MediaPlayer();
try {
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setDataSource("http://translate.google.com/translate_tts?tl=ru&q=привет");
player.prepare();
player.start();
} catch (Exception e)
{
Toast.makeText(this, "speaking error!!", Toast.LENGTH_LONG).show();
}
}
Thread x;
MediaPlayer mediaPlayer;
x=new Thread(){
public void run(){
try{
url1="http://www.translate.google.com/translate_tts?ie=UTF-8&q="this is word which is speech"%0A&tl="this is language"&prev=input";
mediaPlayer=new MediaPlayer();
mediaPlayer.reset();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(url1);
mediaPlayer.prepare(); // might take long! (for buffering, etc)
mediaPlayer.start();
} catch (IllegalArgumentException e) {
mediaPlayer.reset();
} catch (IllegalStateException e) {
mediaPlayer.reset();
} catch (IOException e) {
mediaPlayer.reset();
}
finally{
// x.suspend();
}
}
};
then you can call like this :
x.start();
My media player delays its start when kept idle for some time, also some time it does not play the audio.
Earlier I used setDataSource(), but this time I am using create().
For reference this is the code I am using:
AssetFileDescriptor afd = _context.getResources().openRawResourceFd(this._soundResource.get(this._toBePlayed));
mp = new MediaPlayer();
mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
afd.close();
mp.setOnPreparedListener(new OnPreparedListener(){
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
mp.setOnCompletionListener(new OnCompletionListener(){
#Override
public void onCompletion(MediaPlayer mp) {
mp.reset();
mp.release();
}
});
mp.prepareAsync();
I also had the delay problem for start(). I fixed it with mp.seekTo(0) in onPrepared like this:
mp.setOnPreparedListener(new OnPreparedListener(){
#Override
public void onPrepared(MediaPlayer mp) {
mp.seekTo(0);
mp.start();
}
});
I use a FileInputStream. You didn't set the AudioStreamType. Here is how I setup my MediaPlayer.
public void setupMediaPlayer(){
mp = new MediaPlayer();
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
fis = new FileInputStream(tempFilePath);
mp.setDataSource(fis.getFD());
mp.prepare();
mp.setOnPreparedListener(this);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
tempFilePath is the path to the file.
I also implement setOnPreparedListener in this class which makes your code a lot more readable in my opinion.
Also Great link to reference: MediaPlayer State Diagram
I think if you would change
mp.prepareAsync();
to
mp.prepare();
and put
mp.start();
after the mp.prepare(); your code will work. Give it a try.