简体   繁体   中英

Android mediaPlayer service not working

im trying to play 1 of 2 sounds , correct.mp3 or incorrect.mp3, which is stored in my assets folder. This is the code for my service:

public class MinesweeperSound extends Service {

    MediaPlayer player;

    public int onStartCommand(Intent intent, int flags, int startId) {
        try {
            String audioFile = null;
            audioFile = intent.getStringExtra("filename");
            AssetFileDescriptor afd = getAssets().openFd(audioFile);
            player = new MediaPlayer();
            player.setDataSource(afd.getFileDescriptor());
            player.prepare();
            player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mediaPlayer) {
                    mediaPlayer.start();
                }
            });
            player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mediaPlayer) {
                    mediaPlayer.release();
                }
            });
        }catch (IOException e){
            e.printStackTrace();
        }
        return Service.START_STICKY;
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

here is how i call it in Activity:

Intent correctIntent = new Intent(getApplicationContext(), MinesweeperSound.class);
                    correctIntent.putExtra("filename", "correct.mp3");
                    startService(correctIntent);

for some reason it is not playing any sound, any help is good, thanks

You should set on prepare listener before calling prepare

  player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { @Override public void onPrepared(MediaPlayer mediaPlayer) { mediaPlayer.start(); } });
    player.prepare();

MediaPlayer.prepare() is a synchronous method. If you use it then you dont need a onPrepareListener.

Otherwise use

prepareAsync() 

instead of

prepare()

i finally figured it out. I needed to add parameters to the .setDataSource method. here is the working code:

MediaPlayer player = null;

    public int onStartCommand(Intent intent, int flags, int startId){
        try {
            String audioFile = null;
            audioFile = intent.getStringExtra(PowerUpUtils.MINESWEEPER_TILE_RESULT);
            AssetFileDescriptor afd = getBaseContext().getAssets().openFd(audioFile);
            player = new MediaPlayer();
            player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
            player.prepare();
        }catch (IOException e){
            e.printStackTrace();
        }
        player.start();
        return Service.START_STICKY;
    }

also, calling start() after the try-catch block seemed to be part of the fix.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM