如何播放,停止和暂停在Android中的媒体播放器实例上工作?

时间:2014-02-06 05:11:21

标签: java android audio android-mediaplayer playback

我想开发媒体播放器类型的应用程序,其中有两个按钮用于播放和暂停,一个用于停止。我在我的布局文件中取两个图像按钮,并在Java文件和代码中引用它,我放在这里,但当我点击停止按钮音频停止但我想要重播音频文件;但我不能玩播放按钮。

我的Java代码

public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.activity_aarti_fragment, container, false);

        btnplay=(ImageButton)v.findViewById(R.id.btnplay);
        btnstop=(ImageButton)v.findViewById(R.id.btnstop);
        seekbar=(SeekBar)v.findViewById(R.id.seekbar);

        mp = MediaPlayer.create(getActivity(), R.raw.arti);
        btnstop.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                mp.stop();
                btnplay.setImageResource(R.drawable.ic_action_play);
                Toast.makeText(getActivity(), "Stop",Toast.LENGTH_LONG).show();

            }
        });
        btnplay.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                if(mp.isPlaying())
                {
                    mp.pause();
                    btnplay.setImageResource(R.drawable.ic_action_play);
                    Toast.makeText(getActivity(), "Pause",Toast.LENGTH_SHORT).show();
                }
                else
                {   btnplay.setImageResource(R.drawable.ic_action_pause);
                    mp.start();
                    Toast.makeText(getActivity(), "Play",Toast.LENGTH_SHORT).show();
                }
            }
        });

        return v;
    }

4 个答案:

答案 0 :(得分:1)

使用此:

private BackgroundSound mBackgroundSound;
private MediaPlayer player;
public class BackgroundSound extends AsyncTask<Void, Void, Void> {
    @Override
    protected Void doInBackground(Void... params) {
        player = MediaPlayer.create(HomePage.this, R.raw.background);
        AudioManager audioManager = (AudioManager)  getSystemService(AUDIO_SERVICE);
        float actualVolume = (float) audioManager
                .getStreamVolume(AudioManager.STREAM_MUSIC);
        float maxVolume = (float) audioManager
                .getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        float volume = actualVolume / maxVolume;
        player.setLooping(true); // Set looping
        player.setVolume(volume, volume);
        player.start();
        return null;
    }
}

播放:

        mBackgroundSound = new BackgroundSound();
        mBackgroundSound.execute();

停止:

        mBackgroundSound.cancel(true);
        if (player != null) {
             player.stop();
             player.release();
        }

暂停:

        if (player != null) {
             player.pause();
        }

答案 1 :(得分:1)

尝试此操作将对您有帮助。...

//Click Listener on Image Button with play and Pause method

 PLAY.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
           Play-Pause();
        }
    });

//Play Pause method...........

 public void Play-Pause(){
    if (mediaPlayer.isPlaying()) {
        PLAY.setImageResource(R.drawable.pause);            
        mediaPlayer.pause();
    } else {
        PLAY.setImageResource(R.drawable.play);
        mediaPlayer.start();
    }
}

答案 2 :(得分:0)

媒体播放器停止后,您需要再次调用它以使其进入可以调用start()的状态。请查看此处的状态图http://developer.android.com/reference/android/media/MediaPlayer.html#pause%28%29

答案 3 :(得分:0)

检查此代码,它是更好,更简单的解决方案!

public class MainActivity extends AppCompatActivity {
    MediaPlayer mp;
    Switch sw;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mp = MediaPlayer.create(this,R.raw.nokia);
        sw = findViewById(R.id.sw);
        sw.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mp.setLooping(true);
            }
        });
    }
    public void play(View v){
        mp.start();
    }
    public void pause(View v){
        if(mp.isPlaying())
            mp.pause();
    }
    public void stop(View v){
        if(mp.isPlaying()) {
            boolean loop=mp.isLooping();
            mp.stop();
            mp = MediaPlayer.create(this, R.raw.nokia);
            mp.setLooping(loop);
        }
    }
}