Android: how to Play Mp3 sound as a notification? - java

I have a countdown that, when it is done, it invokes the following onFinish method:
public void onFinish() {
azanCountdownH1.setText("Get ready now");
}
In order to be able to play a mp3 file at the end of the countdown, I changed the onFinish method to:
public void onFinish() {
playNotification();
azanCountdownH1.setText("Get ready now");
}
Where playNotification looks exactly like:
public static void playNotification(){
MediaPlayer mp = MediaPlayer.create(MyApplicationContext.getAppContext(), R.raw.azan);
mp.start();
}
But it is not working !!
On Android Moniter on Android-Studio it is showing the following:
Any suugestion on what or where the bug might be ?!

the Context you pass to MediaPlayer.create() is null. I would suggest to pass the Context to your playNotification method:
public void onFinish() {
playNotification(this);
azanCountdownH1.setText("Get ready now");
}
public static void playNotification(Context context){
MediaPlayer mp = MediaPlayer.create(context, R.raw.azan);
mp.start();
}

You might be passing a wrong Context so just replace Activity.this with your Activity name
public static void playNotification(){
MediaPlayer mediaplayer = MediaPlayer.create(Activity.this, R.raw.azan);
if(mediaplayer == null) {
Log.v(TAG, "Create() on MediaPlayer failed.");
} else {
mediaplayer.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaplayer) {
mediaplayer.stop();
mediaplayer.release();
}
});
mediaplayer.start();
}
}

Try to this code hope this can help you..
public void onFinish() {
azanCountdownH1.setText("Get ready now");
playNotification(youractivity.this);
}
public void playNotification(Context context){
MediaPlayer mp = new MediaPlayer();
mp = MediaPlayer.create(context, R.raw.azan);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.start();
}

Related

MediaPlayer freezing application in loop

ISSUE:
I have problem with my code. I want to loop my MediaPlayer, but when I do it with while loop, MediaPlayer playing, but application is freezed. I know, mediaplayer have setLooping method, but I must do it with while loop because I need to change sound in code.
Do someone know what is solution for it? Thanks.
CODE:
public void play() {
while(true) {
if (player == null) {
player = MediaPlayer.create(context, R.raw.zvuk);
player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
stopPlayer();
}
});
}
player.start();
}
}
public void stopPlayer() {
if (player != null) {
player.release();
player = null;
}
}
}
you are using infinite loop, which will never end. Replace your code with this:
public void play() {
if (player == null) {
player = MediaPlayer.create(context, R.raw.zvuk);
player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
// play again or write your logic for repeat
play();
}
});
}
player.start();
}
public void stopPlayer() {
if (player != null) {
player.release();
player = null;
}
}

Livestream link MediaPlayer Android

I'm trying to get this stream to play:
MediaPlayer mp = new MediaPlayer();
try {
mp.setDataSource("http://knhc-ice.streamguys1.com/live");
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.prepareAsync();
mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener(){
#Override
public void onPrepared(MediaPlayer mp)
{
mp.start();
}
});
} catch (IOException e) {
e.printStackTrace();
}
But when the application runs it is giving me this error:
2019-03-17 17:01:05.035 5924-5924/com.example.android.c895 W/System.err: java.io.IOException: setDataSource failed.: status=0x80000000
I understand that the link that I am passing into the media player is just a single player, but I want that player to automatically play and be passed to the MediaPlayer. Is there anyway I can do this?
What I was able to figure out was put my MediaPlayer on Async Task(background thread) on my application.
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
b = (ImageButton) bottomSheet.findViewById(R.id.imageButton);
new PlayerTask().execute(s);
b.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view)
{
if(started)
{
mediaPlayer.start();
}
}
});
class PlayerTask extends AsyncTask<String, Void, Boolean>
{
#Override
protected Boolean doInBackground(String... strings) {
try
{
mediaPlayer.setDataSource(strings[0]);
mediaPlayer.prepare();
prepared = true;
} catch(IOException e)
{
e.printStackTrace();
}
return prepared;
}
#Override
protected void onPostExecute(Boolean aBoolean)
{
super.onPostExecute(aBoolean);
mediaPlayer.start();
}
}
Since the codes are almost identically the same, could anyone answer why this works and not just on the main thread?

One public media player class to be used across all activities

Updated.
After the recommendations, I decided to run media player in a new thread. Because I need Media Player only when activities are on the screen. Here is the new code:
First, my public SingleMP (Media Player) class used in multiple classes:
import android.content.Context;
import android.media.MediaPlayer;
import android.net.Uri;
public class SingleMP implements Runnable
{
public static MediaPlayer mp;
private static Context context;
private static Uri uri;
public SingleMP(Context context, Uri uri){
this.context= context;
this.uri= uri;
}
#Override
public void run(){
try {
if (mp != null) {
mp.stop();
mp.reset();
mp.release();
mp = null;
}
mp = MediaPlayer.create(context, uri);
mp.start();
} catch (Exception e) {
if (mp != null) {
mp.stop();
mp.reset();
mp.release();
mp = null;
}
e.printStackTrace();
mp = MediaPlayer.create(context, uri);
mp.start();
}
}
// Called in OnDestroy of used class.
public static void mpstop()
{
if (mp != null) {
mp.stop();
mp.reset();
mp.release();
mp = null;
}
}
}
And an example of using it in another Java class:
public class MainMenu
{
private Uri uri;
private Runnable MPthread;
public void onCreate(Bundle savedInstanceState)
{
RadioButton rbtnA = (RadioButton) findViewById(R.id.radio0);
rbtnA.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
// Assign a sound from raw folder.
uri =Uri.parse("android.resource://"+getPackageName()+"/raw/nice");
MPthread = new SingleMP(MainMenu.this, uri));
new Thread(MPthread).start();
}
}
});
RadioButton rbtnB = (RadioButton) findViewById(R.id.radio1);
rbtnB.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
// Assign a sound from raw folder.
uri =Uri.parse("android.resource://"+getPackageName()+"/raw/morning");
MPthread = new SingleMP(MainMenu.this, uri));
new Thread(MPthread).start();
}
}
});
}
#Override
protected void onDestroy() {
super.onDestroy();
if(MPthread!=null) {
SingleMP.mpstop();
}
}
}
What do you think? It seems that my UI works a little bit smoother.
Your media player instance is going to live on the main thread, which is the UI thread. This is not recommended.
I would probably create a service that would create a new thread holding the media player. Each of your activities could then bind to the service to control the media player.
See section Extending the service class.
You can also look at the media player sample.

Alternative Android media players

here's my code for a very basic android sound player, on button press I expect a sound to play, or an IOException to be caught - seems simple enough. Instead I get "QCMediaPlayer mediaplayer NOT present", so how am I meant to play my track if there's no player available - & what should I do as an alternative?
I understand it's already been raised, but no trivial solution was given.
public class MainActivity extends Activity {
private MediaPlayer player;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button motivate = (Button) this.findViewById(R.id.motivate);
motivate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) { // On click randomly select a sound then play it
try {
play();
}
catch (IOException e) {
e.printStackTrace();
}
}
});
}
public void stop() {
if (player != null) {
player.release();
player = null;
}
}
public void play() throws IOException {
stop();
int[] sound = {R.raw.a, R.raw.b, R.raw.c, R.raw.d, R.raw.e, R.raw.f, R.raw.g, R.raw.h, R.raw.i, R.raw.j, R.raw.k, R.raw.l, R.raw.m, R.raw.n, R.raw.o};
player = MediaPlayer.create(MainActivity.this, sound[0]);
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer player) {
stop();
}
});
}
}
You can try this one:https://bitbucket.org/edwardcw/libvlc-android-sample
We used it for stream playback
Okay so the answer to this was actually rather simple. MediaPlayer is an import that only 8/10 will work depending on your flavour of android and kernel. A constant is SoundPool; this will load and play a sound as required.

Android: How to stop all active Media Players?

I have a app with some buttons.
On a Tab on one of these buttons a sound will be played.
Example code:
public void button1(View a) {
MediaPlayer mp1 = MediaPlayer.create(Home.this, R.raw.button1);
mp1.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
mp1.start();
}
public void button2(View b) {
MediaPlayer mp2 = MediaPlayer.create(Home.this, R.raw.button2);
mp2.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
mp2.start();
}
...
Is there a way to stop all MediaPlayers for example on a "Stop all Sounds"-Button?
You can create a ArrayList where you put all your mediaplayers in and stop them while looping.

Categories

Resources