im making a media player that plays mp3s mixes stored on firebase. i can make the links play no problem. but when i press the item again i want it to be the equivalent of pressing stop. but for some reason it does not stop it starts a new instance of the media. can some one tell me what ive done wrong please.
my code
in my on create
mMediaplayer = null;
then my method
private void fetchAudioUrlFromFirebase() throws IOException {
String mp3 = mp3url;
mMediaplayer = new MediaPlayer();
mMediaplayer.setDataSource(mp3);
mMediaplayer.prepare();//prepare to play
if (mMediaplayer.isPlaying()) {
stopPlaying();
} else {
playMedia();
}
}
private void stopPlaying() {
if (mMediaplayer != null) {
mMediaplayer.stop();
}
}
private void playMedia() {
mMediaplayer.start();
}
}
then in item onclick
try {
fetchAudioUrlFromFirebase();
} catch (IOException e) {
e.printStackTrace();
}
issue : lets assume media player is already playing so
// song is playing
mMediaplayer = new MediaPlayer(); // you created a new player
mMediaplayer.setDataSource(mp3);
mMediaplayer.prepare();//prepare to play
if (mMediaplayer.isPlaying()) { // new player is not in playing state
stopPlaying(); // so you always checking the state of new player
} else {
playMedia();
}
check first then create
if (mMediaplayer!=null && mMediaplayer.isPlaying()) {
stopPlaying();
}
mMediaplayer = new MediaPlayer();
mMediaplayer.setDataSource(mp3);
mMediaplayer.prepare();//prepare to play
playMedia();
so the logic can be reduced to
if (mMediaplayer!=null && mMediaplayer.isPlaying()) {
mMediaplayer.stop();
mMediaplayer.release();
}
mMediaplayer = new MediaPlayer();
mMediaplayer.setDataSource(mp3);
mMediaplayer.prepare();//prepare to play
mMediaplayer.start();
Related
Hello I am creating a media player app in which I retrieved the audio from Firebase database in a seperate fragment. But when I switches to that fragment it freezes for 5-10 seconds before start. It is too irritating. So I put the Media Player in a new thread, but I have use of media player outside the thread which causes error. THANKS...
Here is a sample of code =>
new Thread(new Runnable() {
#Override
public void run() {
mediaPlayer.setAudioAttributes(
new AudioAttributes
.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.build());
try {
mediaPlayer.setDataSource(audioUrl);
} catch (IOException e) {
e.printStackTrace();
}
try {
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
runnable = new Runnable() {
#Override
public void run() {
//set progress on seekbar
seekBar.setProgress(mediaPlayer.getCurrentPosition());
//Handle post delay for 0.5 seconds
handler.postDelayed(this, 50);
}
};
}
}).start();
//Get duration of media player
int duration = mediaPlayer.getDuration();
//Convert milliseconds to minutes and seconds
String sDuration = convertFormat(duration);
//Set duration on text view
tv_seekbar_end.setText(sDuration);
play_audio.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
play_audio.setVisibility(View.INVISIBLE);
pause_audio.setVisibility(View.VISIBLE);
if (!mediaPlayer.isPlaying()) {
mediaPlayer.start();
}
//Set max. on seek bar
seekBar.setMax(mediaPlayer.getDuration());
//Start handler
handler.postDelayed(runnable, 0);
}
});
Please try the below way, the below code is in kotlin but you can use this way in java
mPlayer = MediaPlayer.create(mContext, myUri(uriAudio))
if (Build.VERSION.SDK_INT >= 21) {
val audioAttributes: AudioAttributes =
AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_MEDIA)
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.build()
mPlayer!!.setAudioAttributes(audioAttributes)
} else {
mPlayer!!.setAudioStreamType(AudioManager.STREAM_MUSIC)
}
if (mPlayer != null && !mPlayer!!.isPlaying) {
mPlayer!!.start()
}
please check this link may help you more
https://stackoverflow.com/a/68096841/4042384
I am building an application that requires an alarm, and if the user has muted their device or set it to DND, I want to know how to override it.
I am really struggling with clear details on how the permissions on the sound and notification system work, where they overlap etc. I would like to have the user enable the right to have their DND preferences overridden.
To be clear. Is there an call I can make to override the sound or notification settings set buy the user at the device level and if there is, what permissions if any do I need?
I am sorry if this is too general and understand if it is delisted.
Try:
private void playSound(Context context, int soundResId, int volFactor) {
if (context != null) {
try {
MediaPlayer mediaPlayer = new MediaPlayer();
AssetFileDescriptor assetFileDescriptor = context.getResources().openRawResourceFd(soundResId);
if (assetFileDescriptor != null) {
mediaPlayer.setDataSource(assetFileDescriptor.getFileDescriptor(), assetFileDescriptor.getStartOffset(), assetFileDescriptor.getLength());
assetFileDescriptor.close();
}
if (mediaPlayer != null) {
mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
try {
mediaPlayer.reset();
mediaPlayer.release();
} catch (Exception e) {
e.printStackTrace();
} finally {
mp = null;
}
}
});
mediaPlayer.setOnErrorListener(new OnErrorListener() {
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
try {
mediaPlayer.reset();
mediaPlayer.release();
} catch (Exception e) {
e.printStackTrace();
} finally {
mp = null;
}
return true;
}
});
AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
am.setStreamVolume(AudioManager.STREAM_ALARM, am.getStreamMaxVolume(AudioManager.STREAM_MUSIC)/volFactor, 0);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
mediaPlayer.prepare();
mediaPlayer.start();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Also in manifest:
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
i have many Imageviews to play sound when clicked and i managed to play it.
but the problem that when i want to click on another image which supposed to play another sound, it stops the sound but i can't resume if it was clicked on the same play button.
Here is the code I've managed to get it to work so far:
audio1 = (ImageView) popupView.findViewById(R.id.lesson1_audio_gramar_1);
assert audio1 != null;
audio1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mMediaPlayer == null) {
mMediaPlayer = MediaPlayer.create(Lesson1Activity.this, R.raw.lesson1);
}
if (mMediaPlayer.isPlaying()) {
mMediaPlayer.pause();
audio1.setImageResource(R.drawable.ic_play_circle_filled_black_48dp);
try {
mMediaPlayer.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} else {
mMediaPlayer.start();
audio1.setImageResource(R.drawable.ic_pause_circle_filled_black_48dp);
mMediaPlayer.setOnCompletionListener(mCompletionListener);
}
}
});
audio2 = (ImageView) popupView.findViewById(R.id.lesson1_audio_gramar_2);
assert audio2 != null;
audio2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mMediaPlayer == null) {
mMediaPlayer = MediaPlayer.create(Lesson1Activity.this, R.raw.lesson2);
}
if (mMediaPlayer.isPlaying()) {
mMediaPlayer.pause();
audio2.setImageResource(R.drawable.ic_play_circle_filled_black_48dp);
try {
mMediaPlayer.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} else {
mMediaPlayer.start();
audio2.setImageResource(R.drawable.ic_pause_circle_filled_black_48dp);
mMediaPlayer.setOnCompletionListener(mCompletionListener);
}
}
});
To play another track, follow this simple steps:
if (mediaPlayer != null) {
if(mediaPlayer.isPlaying()){
mediaPlayer.stop();
mediaPlayer.reset()
mediaPlayer.setDataSource(new music to play);
mediaPlayer.prepare();
}
}
Then call mediaPlayer.start() in the onPrepared() method
can someone help me with this. i'm doing a project in which user can watch video and i use surface view, and media player.. Now, i want to have a small screen that plays a video and continues playing on the big screen.. what i have in my code now, is i have two different surface view on the same activity when i click play button it plays video but when i click full screen it does'nt continue playing, its just play the video from the start.
public class MainActivity extends Activity implements OnClickListener ,
SurfaceHolder.Callback{
//For surface
MediaPlayer mediaPlayer, mediaPlayer_fullscreen;
SurfaceView surfaceView, surfaceView_fullscreen;
SurfaceHolder surfaceHolder, surfaceholder_fullscreen;
boolean pausing = false;
boolean pausing_2 = false;
boolean reset = false;
public static int media_length;
//FULL SCREEN
Button play, pause;
ImageButton mExit, resume;
public static int mediaPlayer_length;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//For Surface
getWindow().setFormat(PixelFormat.UNKNOWN);
surfaceView = (SurfaceView)findViewById(R.id.surfaceView1);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setFixedSize(176, 144);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
mediaPlayer = new MediaPlayer();
//FULLSCREEN
//For Surface
surfaceView_fullscreen =
(SurfaceView)findViewById(R.id.surfaceView2);
surfaceholder_fullscreen = surfaceView_fullscreen.getHolder();
surfaceholder_fullscreen.addCallback(this);
surfaceholder_fullscreen.setFixedSize(300, 300);
surfaceholder_fullscreen.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
mediaPlayer_fullscreen = new MediaPlayer();
media_length = mediaPlayer.getCurrentPosition();
}
#Override
public void onClick(View v) {
switch(v.getId()){
case R.id.play:
playvideo();
break;
case R.id.view_fullscreen:
mediaPlayer.release();
linearlayout_head.setVisibility(View.GONE);
linearlayout_fullscreen.setVisibility(View.VISIBLE);
playvideo_fullscreen();
break;
}
public void playvideo() {
path_path = EditText_path.getText().toString();
pausing = false;
//reset = false;
if(mediaPlayer.isPlaying()){
mediaPlayer.reset();
}
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDisplay(surfaceHolder);
try {
mediaPlayer.setDataSource(getDataSource(path_path));
Log.d("LOGPLAY", path_path);
mediaPlayer.prepare();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.start();
//mediaPlayer.pause();
//media_length = mediaPlayer.getCurrentPosition();
}
public void playvideo_fullscreen() {
pausing_2 = false;
//reset = false;
if(mediaPlayer_fullscreen.isPlaying()){
mediaPlayer_fullscreen.reset();
//mediaPlayer_fullscreen.seekTo(media_length);
}
mediaPlayer_fullscreen.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer_fullscreen.setDisplay(surfaceholder_fullscreen);
try {
mediaPlayer_fullscreen.setDataSource(getDataSource(path_path));
Log.d("LOGPLAYFullscreen", path_path);
mediaPlayer_fullscreen.prepare();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//mediaPlayer_fullscreen.start();
mediaPlayer_fullscreen.seekTo(media_length);
}
can someone help me with this please. Thank you.
When you switch between two views media player will not resume where you are paused. So you must do it manual. You will get the current time of video using
MediaPlayer mp=new MediaPlayer();
int currentTime=mp.getCurrentPosition();
After switching the view just seek the video to the 'currentTime'.
mp.seekTo(currentTime);
I published an app recently, and users are reporting crashes because my program included the method setNextMediaPlayer(). I now realize that this only works for API 16+, and my app supports API 8+. I was wondering if there is an alternate way of achieving the same effect.
My app has some text to speech, and what I was doing was creating an ArrayList of MediaPlayers that each had a short sound file, and then playing them one after an other. If I remove this method from the code, the audio becomes too choppy to understand.
I was thinking about using the SoundPool class, but there is no OnCompleteListener, so I'm not sure how I would do it.
So basically my question is: Is there a way to transition seamlessly between audio files without using the setNextMediaPlayer() method?
Thanks very much for your time!
EDIT
I added this code that I found
private class CompatMediaPlayer extends MediaPlayer implements OnCompletionListener {
private boolean mCompatMode = true;
private MediaPlayer mNextPlayer;
private OnCompletionListener mCompletion;
public CompatMediaPlayer() {
try {
MediaPlayer.class.getMethod("setNextMediaPlayer", MediaPlayer.class);
mCompatMode = false;
} catch (NoSuchMethodException e) {
mCompatMode = true;
super.setOnCompletionListener(this);
}
}
public void setNextMediaPlayer(MediaPlayer next) {
if (mCompatMode) {
mNextPlayer = next;
} else {
super.setNextMediaPlayer(next);
}
}
#Override
public void setOnCompletionListener(OnCompletionListener listener) {
if (mCompatMode) {
mCompletion = listener;
} else {
super.setOnCompletionListener(listener);
}
}
#Override
public void onCompletion(MediaPlayer mp) {
if (mNextPlayer != null) {
// as it turns out, starting a new MediaPlayer on the completion
// of a previous player ends up slightly overlapping the two
// playbacks, so slightly delaying the start of the next player
// gives a better user experience
SystemClock.sleep(50);
mNextPlayer.start();
}
mCompletion.onCompletion(this);
}
}
But now how do I add the audio files? I tried this:
// assigns a file to each media player
mediaplayers = new ArrayList<CompatMediaPlayer>();
for (int i = 0; i < files.size(); i++) {
mediaplayers.add((CompatMediaPlayer) CompatMediaPlayer.create(this, files.get(i)));
}
but am getting a class cast exception because MediaPlayer cannot be casted to CompatMediaPlayer.
Create Compat player that will work with onCompletionListener to start the next player like:
public void onCompletion(MediaPlayer mp) {
if (mCompatMode && mNextPlayer != null) {
mNextPlayer.prepare();
mNextPlayer.start();
}
}
Somewhere in your constructor check if there is method (or check SDK version) named "setNextMediaPlayer"
mCompatMode = Build.VERSION.SDK_INT < 16;
Define method like this one:
public void setNextMediaPlayer(MediaPlayer next) {
if (mCompatMode) {
mNextPlayer = next;
} else {
super.setNextMediaPlayer(next);
}
}
if (mediaPlayer != null && mediaPlayer.isPlaying()) {
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = null;
}
mediaPlayer = new MediaPlayer();
try {
AssetFileDescriptor descriptor = context.getAssets().openFd("play1.mp3");
mediaPlayer.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength());
descriptor.close();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.start();
MediaPlayer md = new MediaPlayer();
try {
AssetFileDescriptor descriptor = context.getAssets().openFd("play2.mp3");
md.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength());
descriptor.close();
md.setAudioStreamType(AudioManager.STREAM_MUSIC);
md.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.setNextMediaPlayer(md);
MediaPlayer md1 = new MediaPlayer();
try {
AssetFileDescriptor descriptor = context.getAssets().openFd("play3.mp3");
md1.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength());
descriptor.close();
md1.setAudioStreamType(AudioManager.STREAM_MUSIC);
md1.prepare();
} catch (IOException e) {
e.printStackTrace();
} md.setNextMediaPlayer(md1);