public class PlayAudio extends BroadcastReceiver{
private Context mContext;
MediaPlayer mp;
#Override
public void onReceive(Context context, Intent intent) {
mContext = context;
mIntent = intent;
playSound("sound.mp3");
}
private void playSound(String file){
mp = MediaPlayer.create(mContext, Uri.parse("file://"+file));
if (mp.isPlaying())
{
mp.stop();
}
mp.setLooping(true);
try {
mp.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mp.start();
}
}
I am getting an error when i check if mp is playing. How can i check if MediaPlayer(mp) is playing so that i can stop it?
First a bit of confusion in the above code
In the above code new instance of media player will get created each time you call playsound() what you need to do is instantiate mp only once in oncreate() of playaudio class and use it repeatedly.
Also if you use create to instantiate mediaplayer you prepare gets called internally you should not call it again.http://developer.android.com/reference/android/media/MediaPlayer.html#create(android.content.Context, android.net.Uri)
Check if mp is null after you do create to verify if mediaplayer instance was created successfully.
Related
I have a Service which takes in an audio file and plays it with MediaPlayer. This is how I call my Service:
private void playAudio(String url) throws Exception {
Intent music = new Intent(this,MusicService.class);
music.putExtra("paths", path);
startService(music);
}
This is my Service class:
class MusicService extends Service implements OnCompletionListener {
MediaPlayer mediaPlayer;
String musicFile;
#Override
public void onCreate() {
mediaPlayer = new MediaPlayer();
mediaPlayer.setOnCompletionListener(this);
Toast.makeText(getApplicationContext(), "hello", Toast.LENGTH_LONG).show();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Bundle e = intent.getExtras();
musicFile= e.getString("paths");
try {
mediaPlayer.prepare();
mediaPlayer.setDataSource(musicFile);
} catch (IllegalArgumentException i) {
// TODO Auto-generated catch block
i.printStackTrace();
} catch (IllegalStateException i) {
// TODO Auto-generated catch block
i.printStackTrace();
} catch (IOException i) {
// TODO Auto-generated catch block
i.printStackTrace();
}
if (!mediaPlayer.isPlaying()) {
mediaPlayer.start();
}
return START_STICKY;
}
#Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
}
#Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
}
The Service is never getting executed, the Toast is never shown, and the MediaPlayer does not play.
I declare my it in my manifest like this:
<service android:name=".MusicService" android:enabled="true"></service>
I get a force close error, and this IllegalAccessException in my logs:
java.lang.RuntimeException: Unable to instantiate service unjustentertainment.com.MusicService:
java.lang.IllegalAccessException: access to class not allowed
The exception you get is because the system could not init your service (call its constructor) because its not accessible.
As it says here:
...Make sure the class is
declared public...
The class you posted is not public, so make it public.
You need to make your class public.
i.e.
public class MusicService extends Service implements OnCompletionListener {
MediaPlayer mediaPlayer;
String musicFile;
I have a game and I use separate MediaPlayer for each sound
Here it is :
public class SoundSample {
public final static String CORRECT="sounds/correct.wav";
public final static String PASS="sounds/pass.wav";
public final static String CLOCK_OUT="sounds/clock_out.mp3";
public final static String ALARM="sounds/alarm.mp3";
public final static String COUNTDOWN_TO_START="sounds/countdown_to_start.mp3";
public final static String GO="sounds/go.mp3";
private String assetFile;
private MediaPlayer mediaPlayer;
public SoundSample(String sound){
assetFile = sound;
try {
AssetFileDescriptor afd = MainActivity.activity.getAssets().openFd(sound);
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
mediaPlayer.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void play(){
if (mediaPlayer.isPlaying()){
mediaPlayer.stop();
}
mediaPlayer.start();
}
public void release(){
mediaPlayer.release();
mediaPlayer.reset();
mediaPlayer = null;
}
}
so, I use
correctSound = new SoundSample(SoundSample.CORRECT);
and then
correctSound.play();
So, at a second use it stops playing on many phones and never play again. I mean, debugger shows that mediaPlayer.start(); is executed but the sounds never play again.
it works well, however , on every Android 6 phone I tested, so it's probably only a android 6- or 5- issue
Is there any way I can correct this? I know I can use SoundPool, but it also has some issues
You have to recreate the media player , try like below
if (mediaPlayer.isPlaying()) {
mediaPlayer.reset();
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.yourAudio);
}
mediaPlayer.start();
Try to add code
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
I tried almost everything found on the internet and I can't stop the media player once it starts. I'm using broadcast receiver and I'm controlling the media player using SMS. Here is my code.
public class Receiver extends BroadcastReceiver{
String body;
String address;
public static final String SMS_EXTRA_NAME="pdus";
MediaPlayer mp = new MediaPlayer();
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
SharedPreferences obj1=context.getSharedPreferences("mypref", Context.MODE_PRIVATE);
String newstring=obj1.getString("key1", null);
String name=newstring;
Bundle bund=intent.getExtras();
String space="";
if(bund!=null)
{
Object[] smsExtra=(Object[])bund.get(SMS_EXTRA_NAME);
for(int i=0;i<smsExtra.length;i++)
{
SmsMessage sms=SmsMessage.createFromPdu((byte[])smsExtra[i]);
body=sms.getMessageBody().toString();
address=sms.getOriginatingAddress();
if(body.equals("ON"))
{
if(mp.isPlaying())
{
mp.stop();
}
try {
mp.reset();
AssetFileDescriptor afd;
afd = context.getAssets().openFd("file.mp3");
mp.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
mp.prepare();
mp.start();
mp.setLooping(true);
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
else if(body.equals("OFF"))
{
if (mp.isPlaying()==true||mp!=null)
{
try{
mp.stop();
mp.release();
} catch(Exception e){
System.out.println("Exception"+e);
}
}
}
}
}
}
}
The media player is turning on when I send "ON", but it won't turn off. And yes I have given the required permissions in the Manifest file.
The BroadcastReciever it stays alive for around 9 seconds, you should not create big operation in it. However, you can let it start an operation like start acitivty or service and there you play a track or start download a file ...etc
If you want to only start a player and no need for user interaction, I suggest that you start a service and there you play your what you want.
I spent a lot of time studying this problem, and found out that:
The problem here is that I create a MediaPlayer inside a thread that is managed by the IntentService. And at the time of starting playback the thread is no longer valid.
So the way out is:
final Handler handler = new Handler(getMainLooper());
handler.post(new Runnable() {
#Override
public void run() {
mediaPlayer.start();
}
});
handler.postDelayed(new Runnable() {
#Override
public void run() {
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
}
}
, 30 * 1000);
It helped me stop the mediaplayer.
I am using a method to initialize and prepare a MediaPlayer (defined as class variable). It takes a String which is the name of the song as its parameter and it sets the DataSource to that particular path.
public void create_and_prepare_song(String x) {
if (mp != null)
mp.release();
mp = new MediaPlayer();
String filePath = Environment.getExternalStorageDirectory().getPath() + "/songsfolder/" + x + ".mp3";
try {
mp.setDataSource(filePath);
} catch (IOException e) {
e.printStackTrace();
}
try {
mp.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Now, somewhere inside my code I use this method to initialize and prepare "1.mp3". Then, as soon as this song is finished, I use an onCompletionListener so that "2.mp3" is played after, and "3.mp3" then. I use:
create_and_prepare_song("1");
i = 2;
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
if (i <= 3) {
String int_to_String = i + "";
create_and_prepare_song(int_to_String); //1
mp.setOnCompletionListener(this);
mp.start();
i++;
}
}
});
mp.start();
Now, this causes app to stop from working! However, if I just copy and paste the body of the method to the //1 line instead, the app works fine, playing first, second and third song. Why is that so? Thanks a lot
Note that you are creating a new player inside of create_and_prepare_song() but the old one is still referenced inside of the listener.
That's because the global variable and the onCompletion parameter has the same name: mp.
Here's piece of code that works:
package com.example.simon.mplaylist;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import java.io.IOException;
/**
* Created by Simon on 2014 Jul 20.
*/
public class MainActivity extends Activity {
static int currentSong;
static MediaPlayer mediaPlayer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (mediaPlayer != null)
return;
currentSong = 1;
create_and_prepare_song(currentSong);
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
if (currentSong++ >= 3) {
mediaPlayer = null;
return;
}
create_and_prepare_song(currentSong);
mediaPlayer.setOnCompletionListener(this);
mediaPlayer.start();
}
});
mediaPlayer.start();
}
public void create_and_prepare_song(int songNum) {
if (mediaPlayer != null)
mediaPlayer.release();
mediaPlayer = new MediaPlayer();
String storage = Environment.getExternalStorageDirectory().getPath();
String filePath = storage + "/songsfolder/" + String.valueOf(songNum) + ".mp3";
try {
mediaPlayer.setDataSource(filePath);
} catch (IOException e) {
e.printStackTrace();
}
try {
mediaPlayer.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
NOTE also that I defined the MediaPlayer as static and also am checking # onCreate for it's presence. That way you avoid multiple songs playing at once if the activity is recreated (e.g. screen rotation).
You really shouldn't be starting actual playback inside create_and_prepare_song(). It should be started inside onPrepared() since there might be some delay from the call to prepare() and the media being ready to play.
I have a Service which takes in an audio file and plays it with MediaPlayer. This is how I call my Service:
private void playAudio(String url) throws Exception {
Intent music = new Intent(this,MusicService.class);
music.putExtra("paths", path);
startService(music);
}
This is my Service class:
class MusicService extends Service implements OnCompletionListener {
MediaPlayer mediaPlayer;
String musicFile;
#Override
public void onCreate() {
mediaPlayer = new MediaPlayer();
mediaPlayer.setOnCompletionListener(this);
Toast.makeText(getApplicationContext(), "hello", Toast.LENGTH_LONG).show();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Bundle e = intent.getExtras();
musicFile= e.getString("paths");
try {
mediaPlayer.prepare();
mediaPlayer.setDataSource(musicFile);
} catch (IllegalArgumentException i) {
// TODO Auto-generated catch block
i.printStackTrace();
} catch (IllegalStateException i) {
// TODO Auto-generated catch block
i.printStackTrace();
} catch (IOException i) {
// TODO Auto-generated catch block
i.printStackTrace();
}
if (!mediaPlayer.isPlaying()) {
mediaPlayer.start();
}
return START_STICKY;
}
#Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
}
#Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
}
The Service is never getting executed, the Toast is never shown, and the MediaPlayer does not play.
I declare my it in my manifest like this:
<service android:name=".MusicService" android:enabled="true"></service>
I get a force close error, and this IllegalAccessException in my logs:
java.lang.RuntimeException: Unable to instantiate service unjustentertainment.com.MusicService:
java.lang.IllegalAccessException: access to class not allowed
The exception you get is because the system could not init your service (call its constructor) because its not accessible.
As it says here:
...Make sure the class is
declared public...
The class you posted is not public, so make it public.
You need to make your class public.
i.e.
public class MusicService extends Service implements OnCompletionListener {
MediaPlayer mediaPlayer;
String musicFile;