I have done a lot of googling but other's solutions are not working for me.
My goal is to play a sound on demand on the alarm channel.
(So the sound volume is adjusted by the alarm volume setting)
from this thread I build the following code
mediaPlayerScan = MediaPlayer.create(getContext(), R.raw.scan_beep);
if (Build.VERSION.SDK_INT >= 21) {
mediaPlayerScan.setAudioAttributes(new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_ALARM)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.build());
} else {
mediaPlayerScan.setAudioStreamType(AudioManager.STREAM_ALARM);
}
It still plays on the music channel.
(IE volume is adjusted in music setting not alarm)
My intuition is that i'm missing a permission or something, but I haven't found such a permission.
I'm testing on a Google Pixel 1
Thanks,
Nathan
Edit:
Thanks to #jeffery-blattman the following code works for me
mediaPlayerScan = new MediaPlayer();
try {
mediaPlayerScan.setDataSource(getContext(),
Uri.parse(getString(R.string.res_path) + R.raw.scan_beep));
if (Build.VERSION.SDK_INT >= 21) {
mediaPlayerScan.setAudioAttributes(new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_ALARM)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.build());
} else {
mediaPlayerScan.setAudioStreamType(AudioManager.STREAM_ALARM);
}
mediaPlayerScan.prepare();
} catch (IOException e) {
e.printStackTrace();
}
The problem is that create() puts the MediaPlayer in a state where it won't accept the attributes (it calls prepare() for you). You need to use the more verbose mechanism of creating the player.
final MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(...);
AudioAttributes attrs = new AudioAttributes.Builder().setUsage(usage).build();
mediaPlayer.setAudioAttributes(attrs);
new AsyncTask<Void,Void,Boolean>() {
#Override
protected Boolean doInBackground(Void... voids) {
try {
mediaPlayer.prepare();
return true;
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
#Override
protected void onPostExecute(Boolean prepared) {
if (prepared) {
mediaPlayer.start();
}
}
}.execute();
I upvoted Jeffrey Blattman's answer above, but here is the full code that shows how to use setDataSource with an audio file that you put into your app resources (raw). Also a couple of other things I picked up in my travels...
static public void playAlarmSound () {
final MediaPlayer mediaPlayer = new MediaPlayer();
new AsyncTask<Void, Void, Boolean>() {
#Override
protected Boolean doInBackground(Void... voids) {
try {
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mediaPlayer.reset();
mediaPlayer.release();
}
});
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mediaPlayer.start();
}
});
AssetFileDescriptor afd = getContext().getResources().openRawResourceFd(R.raw.nameofyourresource);
if (afd == null) return false;
mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
afd.close();
if (Build.VERSION.SDK_INT >= 21) {
mediaPlayer.setAudioAttributes(new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_ALARM)
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.build());
} else {
mediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
}
mediaPlayer.setVolume(1.0f, 1.0f);
mediaPlayer.prepare();
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
}.execute();
}
Related
I made for some time an app with a media player. But after a number of updates, I realized that the space required for downloading the app got very big. I decided to change the method and make the players play the music with the internet from a URL link, but I have no idea how to manage that.
I believe that I have to set mediaplayer.setDataSource and then put the link. But after that it doesn't work with my code. Does anyone know how to convert my code to work in this method?
A part of my current code:
mediaPlayer = MediaPlayer.create(this,R.raw.storm);
runnable = new Runnable() {
#Override
public void run() {
seekBar.setProgress(mediaPlayer.getCurrentPosition());
handler.postDelayed(this,500);
}
};
int duration = mediaPlayer.getDuration();
String sDuration = convertFormat(duration);
playerDuration.setText(sDuration);
btPlay.setOnClickListener(v -> {
mediaPlayer.start();
seekBar.setMax(mediaPlayer.getDuration());
handler.postDelayed(runnable, 0);
btPlay.setVisibility(View.GONE);
btPause.setVisibility(View.VISIBLE);
});
btPause.setOnClickListener(v -> {
mediaPlayer.pause();
btPlay.setVisibility(View.VISIBLE);
btPause.setVisibility(View.GONE);
handler.removeCallbacks(runnable);
});
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean b) {
if (b) {
mediaPlayer.seekTo((int) progress);
}
playerPosition.setText(convertFormat(mediaPlayer.getCurrentPosition()));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
mediaPlayer.setOnCompletionListener(mp -> {
btPause.setVisibility((View.GONE));
btPlay.setVisibility(View.VISIBLE);
mediaPlayer.seekTo(0);
});
}
Please try the below code to play music via URL
Kotlin:
val mediaPlayer = MediaPlayer()
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC)
try {
mediaPlayer.setDataSource(yourSongUrl)
mediaPlayer.prepare()
mediaPlayer.start()
} catch (e: IOException) {
e.printStackTrace()
}
Log.v(TAG,"Music is streaming")
Java:
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(yourSongUrl);
mediaPlayer.prepare();
mediaPlayer.start();
} catch (IOException e) {
e.printStackTrace();
}
Log.v(TAG,"Music is streaming")
If you target the API level 26 or above theb setAudioStreamType() is deprecated. So try the below code:
//java
mediaPlayer.setAudioAttributes(
new AudioAttributes
.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.build());
//Kotlin
mediaPlayer.setAudioAttributes(
AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.build()
)
Note: Add Internet permission in manifest.xml file
<uses-permission android:name="android.permission.INTERNET" />
last but not least, you have to wait for the music to play because it's playing the audio from the URL so it takes time and also depends on the user internet speed
I'm using MediaPlayer to play audio via an AsyncTask, and using AudioManager to set the stream. On a specific button press, I call AudioManager's setSpeakerPhoneOn() to toggle, but it doesn't switch to ear speaker. My audio always blasts through the main speaker.
I tried these solutions (as is apparent in the code below), but none worked:
Switching between earpiece and speakerphone on button
press
Audiomanager Speaker not
working
Here's my code for initializing the AudioManager in onViewCreated:
try {
audioManager = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE);
audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
audioManager.setSpeakerphoneOn(loudOn);
// loudOn is defined globally, and initialized to false
}
This is the audio playing function which uses AsyncTask:
static private void playCall(final Context context, final String[] contactParts)
{
final MediaPlayer mediaPlayer = new MediaPlayer();
new AsyncTask<Void,Void,Boolean>() {
#Override
protected Boolean doInBackground(Void... voids) {
try {
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.reset();
mp.release();
}
});
int audioFile_R_id = //get audio file id
try {
AssetFileDescriptor afd = context.getResources().openRawResourceFd(audioFile_R_id);
if (afd == null) return false;
mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
afd.close();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mediaPlayer.setAudioAttributes(new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_VOICE_COMMUNICATION)
.setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
.build());
} else {
//deprecated in API 26
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
}
mediaPlayer.prepare();
return true;
}
catch (Resources.NotFoundException rnf)
{
rnf.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
#Override
protected void onPostExecute(Boolean prepared) {
if (prepared) {
mediaPlayer.start();
}
}
}.execute();
}
And this is the button code that attempts to toggle:
loudFAB.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
loudOn = !loudOn;
audioManager.setSpeakerphoneOn(loudOn);
}
});
Change the AudioManager
try {
audioManager = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE);
audioManager.setMode(AudioManager.MODE_IN_CALL);
audioManager.setSpeakerphoneOn(loudOn);
// loudOn is defined globally, and initialized to false
}
i am creating an application that in it i need to play some sounds with low bit rates such as 16kbps.
I use MediaPlayer with below code. but it cannot play and i get Error(100, 0) in logcat. when i replace audio with 48kbps OGG file or 128kbps MP3 file all thing is ok. How i can play audio with 16kbps bit rate?
I have this problem only on Kitkat
player = new MediaPlayer();
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
try {
Class<?> cMediaTimeProvider = Class.forName("android.media.MediaTimeProvider");
Class<?> cSubtitleController = Class.forName("android.media.SubtitleController");
Class<?> iSubtitleControllerAnchor = Class.forName("android.media.SubtitleController$Anchor");
Class<?> iSubtitleControllerListener = Class.forName("android.media.SubtitleController$Listener");
Constructor constructor = cSubtitleController.getConstructor(new Class[]{Context.class, cMediaTimeProvider, iSubtitleControllerListener});
Object subtitleInstance = constructor.newInstance(activity, null, null);
Field f = cSubtitleController.getDeclaredField("mHandler");
f.setAccessible(true);
try {
f.set(subtitleInstance, new Handler());
} catch (IllegalAccessException ignored) {
} finally {
f.setAccessible(false);
}
Method setsubtitleanchor = player.getClass().getMethod("setSubtitleAnchor", cSubtitleController, iSubtitleControllerAnchor);
setsubtitleanchor.invoke(player, subtitleInstance, null);
//Log.e("", "subtitle is setted :p");
} catch (Exception e) {
}
}
player.setWakeMode(activity,
PowerManager.PARTIAL_WAKE_LOCK);
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
this.seekbar = seekbar;
player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
finalTime = player.getDuration();
timeElapsed = mediaPlayer.getCurrentPosition();
seekbar.setMax(finalTime);
init();
remaining.setText(miliToTime(finalTime));
elapsed.setText("0:00:00");
durationHandler.postDelayed(updateSeekBarTime, 100);
}
});
player.setOnErrorListener(this);
player.setDataSource(musicPath);
player.prepareAsync();
I am developing an app for Android devices using Processing 2.0, now I have playMP3(); function but need to stop the sound using stopMP3();. I tried everything but what is the best way to load an mp3 and play/stop using processing?, snd.stop(); within the stopMP3 function does not work...
import android.media.*;
import android.content.res.*;
...
MediaPlayer snd;
...
void setup()
{
MediaPlayer snd = new MediaPlayer();
}
...
void playMP3()
{
try {
AssetManager assets = this.getAssets();
AssetFileDescriptor fd = assets.openFd("loop1.mp3");
snd.setDataSource(fd.getFileDescriptor(), fd.getStartOffset(), fd.getLength());
snd.prepare();
snd.start();
snd.setLooping(true);
}
catch (IllegalArgumentException e) {
e.printStackTrace();
}
catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
void stopMP3(){
????
}
this is a simple sample for media player
private MediaPlayer m_Player;
//Start Play Function
private void startPlaying() {
m_Player = new MediaPlayer();
m_Player.setOnCompletionListener(onComplete);
try {
if(m_Player.isPlaying())
stopPlaying();
m_Player.setDataSource(m_FileName);
m_Player.prepare();
m_Player.start();
} catch (IOException e) {
}
}
//Stop Play Function
private void stopPlaying() {
m_Player.stop();
m_Player.reset();
m_Player = null;
}
//and this is onComplete Listener
MediaPlayer.OnCompletionListener onComplete = new OnCompletionListener()
{
#Override
public void onCompletion(MediaPlayer mp)
{
stopPlaying();
}
};
i hope it's helping you
I try to play a device ringtone one time with the MediaPlayer class (I have to use MediaPlayer as I use the ringtones together with other audio sources in my app) So i try the following to play a random ringtone yet I cant stop it looping, it keeps playing. Is it possible to play a ringtone only one time with MediaPlayer.
Thanks
try {
RingtoneManager mRing = new RingtoneManager(RingtoneActivity.this);
int mNumberOfRingtones = mRing.getCursor().getCount();
Uri mRingToneUri = mRing.getRingtoneUri((int) (Math.random() * mNumberOfRingtones));
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setDataSource(RingtoneActivity.this, mRingToneUri);
mMediaPlayer.prepare();
mMediaPlayer.setLooping(false);
mMediaPlayer.start();
} catch (Exception ignore) {
}
UPDATE:
I just have posted a new question related to this one as I was able to find the specific cause of the trouble in my case.
I found simple solution with use events - confirm answer since it solves your problem :)
mediaPlayer.setOnSeekCompleteListener(new MediaPlayer.OnSeekCompleteListener() {
#Override
public void onSeekComplete(MediaPlayer mediaPlayer) {
mediaPlayer.stop();
}
});
http://developer.android.com/reference/android/media/MediaPlayer.html
Remember about release().
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
mediaPlayer.release();
}
});
Full code it your want to use need set ring tone to something what you would like.
AudioManager audioManager = (AudioManager)getSystemService(AUDIO_SERVICE);
int volume = audioManager.getStreamVolume(AudioManager.STREAM_NOTIFICATION);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(view.getContext());
Uri notificationSoundUri = Uri.parse(prefs.getString("notification_sound", "DEFAULT_SOUND"));
MediaPlayer mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource(view.getContext(), notificationSoundUri);
} catch (Exception e1) {
e1.printStackTrace();
mediaPlayer.release();
return;
}
mediaPlayer.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
mediaPlayer.release();
}
});
try {
mediaPlayer.prepare();
} catch (Exception e1) {
e1.printStackTrace();
mediaPlayer.release();
return;
}
mediaPlayer.setOnSeekCompleteListener(new MediaPlayer.OnSeekCompleteListener() {
#Override
public void onSeekComplete(MediaPlayer mediaPlayer) {
// TODO Auto-generated method stub
mediaPlayer.stop();
mediaPlayer.start();
}
});
mediaPlayer.setVolume(volume, volume);
mediaPlayer.start();
use Notifications
NotificationManager nManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder notifBuilder =
new NotificationCompat.Builder(context);
notifBuilder.setSound(sound);
notification = notifBuilder.build();
nManager.notify(SOME_INT_CONSTANT, notification);