媒体播放器的监听器 - Android

时间:2017-05-31 12:04:07

标签: android state android-mediaplayer media

我正在尝试获取默认音频播放器侦听器控件。还可以播放/暂停等。

我为媒体创建了BroadcastReceiver。没有调用媒体播放器监听器,

<receiver android:name=".MediaStateReceiver">
            <intent-filter>
                <action android:name="android.intent.action.MEDIA_BUTTON"></action>
            </intent-filter>
        </receiver>

我已经给出了以下权限,

<uses-permission android:name="android.permission.MEDIA_CONTENT_CONTROL"/>
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />

如何获取音频播放器控件的回调。

1 个答案:

答案 0 :(得分:0)

  private ImageView btnPlay, btnForward, btnBackward, btnNext, btnPrevious;


    private MediaPlayer mp;
    private int seekForwardTime = 5000; // 5000 milliseconds
    private int seekBackwardTime = 5000; // 5000 milliseconds
    public static int currentSongIndex = 0;

    private Utilities utils;

    static int count = -1;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.player, container, false);

        // All player buttons
        btnPlay = (ImageView) view.findViewById(R.id.btnPlay);
        btnForward = (ImageView) view.findViewById(R.id.btnForward);
        btnBackward = (ImageView) view.findViewById(R.id.btnBackward);
        btnNext = (ImageView) view.findViewById(R.id.btnNext);
        btnPrevious = (ImageView) view.findViewById(R.id.btnPrevious);

        mp = new MediaPlayer();

        utils = new Utilities();

        int position = getArguments().getInt("songIndex");
        currentSongIndex = position;

        playSong(position);


        btnPlay.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {

                if (mp.isPlaying()) {
                    if (mp != null) {
                        mp.pause();
                        btnPlay.setImageResource(R.drawable.btn_play);
                    }
                } else {

                    if (mp != null) {
                        mp.start();
                        btnPlay.setImageResource(R.drawable.btn_pause);
                    }
                }

            }
        });