关于android mp3播放的东西

时间:2016-07-29 03:47:37

标签: android

@Override
protected void onResume() {
    playOrderSound();
    super.onResume();
}
private void playOrderSound(Context context){
    player= MediaPlayer.create(context, R.raw.order);
    player.setAudioStreamType(AudioManager.STREAM_MUSIC);
    player.start();
    player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {
            player.reset();
            player.release();
        }
    });
    player.setOnErrorListener(new MediaPlayer.OnErrorListener() {
        @Override
        public boolean onError(MediaPlayer mp, int what, int extra) {
            player.start();
            return false;
        }
    });
}

我将声源放在原始目录中并使用mediaPlayer播放它。当服务器向我发送消息时。我开始一个新的活动并播放mp3资源,但它不起作用。 新活动从lauchmode singletask开始。

1 个答案:

答案 0 :(得分:0)

试试这个好玩的

   @Override
        protected void onResume() {

            super.onResume();
            playOrderSound(Mainactivity.this);



        }

        private void playOrderSound(Context context){

            player = MediaPlayer.create(context, R.raw.acc);
            player.start();


            player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    player.reset();
                    player.release();
                }
            });
            player.setOnErrorListener(new MediaPlayer.OnErrorListener() {
                @Override
                public boolean onError(MediaPlayer mp, int what, int extra) {
                    player.start();
                    return false;
                }
            });
        }
        protected void onDestroy() {
            super.onDestroy();


            player.stop();

            }