如何通过通知按钮控制mediaPlayer?

时间:2016-08-19 13:05:26

标签: android android-notifications

这是我的代码: -

SongsListActivity.xml: -

package com.justforyou.bestnarutosongs;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.NotificationCompat;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RemoteViews;
import android.widget.SeekBar;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

public class SongsListActivity extends AppCompatActivity
{

public boolean IsPaused = false;
public int Old_Song = -326523;
public NotificationManager notificationManager;    
public ImageView ImageD;    
public Notification notification;
public Runnable UpdateSongTime;    
public int IIII, II, startTime, C_Image, C_Repeat, Repeat_Value = 0, Repeat_N, P_and_P_N;
public String C_Name, C_Rate;    
public Handler myHandler = new Handler();
public SeekBar seekBar;
public TextView Current_Time, Duration, NameD, RateD;
public ImageButton Stop;
public Songs song;
public ArrayList<Songs> Song;
public ImageButton P_and_P = null, Repeat = null, Next = null, Previous = null;    
private MediaPlayer mediaplayer;
private AudioManager mAudioManager;
private AudioManager.OnAudioFocusChangeListener mOnAudioFocusChangeListener = new AudioManager.OnAudioFocusChangeListener()
{
    @Override
    public void onAudioFocusChange(int focusChange){
        if(focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK || focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT){
            mediaplayer.pause();
            IsPaused = true;              
            P_and_P.setImageResource(R.drawable.ic_play_arrow_white_48dp);
        }
        else if(focusChange == AudioManager.AUDIOFOCUS_GAIN){
            mediaplayer.start();
        }
        else if(focusChange == AudioManager.AUDIOFOCUS_LOSS){
            if(mediaplayer != null)
            {
                mediaplayer.pause();
                IsPaused = true;
                if (Repeat_Value == 0) {
                    showNotification(C_Name, C_Rate, C_Image, R.drawable.ic_play_arrow_white_48dp, R.drawable.ic_repeat_white_48dp);
                } else {
                    showNotification(C_Name, C_Rate, C_Image, R.drawable.ic_play_arrow_white_48dp, R.drawable.ic_repeat_one_white_48dp);
                }
                P_and_P.setImageResource(R.drawable.ic_play_arrow_white_48dp);

            }
        }
    }
};


public void showNotification(String Name, String Rate, int Image_Source, int PandP, int Repeat) {

    RemoteViews remoteview = new RemoteViews(getPackageName(), R.layout.notification_layout);

    PendingIntent pi = PendingIntent.getActivity(this, 0, new Intent(this, SongsListActivity.class), 0);
    notification = new NotificationCompat.Builder(this)
            .setContent(remoteview)
            .setPriority(2)
            .setTicker(NameD.getText())
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(NameD.getText())
            .setContentText(RateD.getText())
            .setContentIntent(pi)
            .setAutoCancel(false)
            .setCustomBigContentView(remoteview)
            .build();

    notification.bigContentView = remoteview;
    remoteview.setImageViewResource(R.id.Repeat_N, Repeat);
    remoteview.setImageViewResource(R.id.P_and_P_N, PandP);
    remoteview.setTextViewText(R.id.Name_N, Name);
    remoteview.setTextViewText(R.id.Rate_N, Rate);
    remoteview.setImageViewResource(R.id.Image_N, Image_Source);
    notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    notificationManager.notify(111111, notification);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_songs_list);

    Repeat = (ImageButton) findViewById(R.id.Repeat);
    Next = (ImageButton) findViewById(R.id.Next);
    Previous = (ImageButton) findViewById(R.id.Previous);
    NameD = (TextView) findViewById(R.id.NameD);
    ImageD = (ImageView) findViewById(R.id.ImageD);
    RateD = (TextView) findViewById(R.id.RateD);
    P_and_P = (ImageButton) findViewById(R.id.P_and_P);
    Stop = (ImageButton) findViewById(R.id.Stop);
    Duration = (TextView) findViewById(R.id.Duration);
    Current_Time = (TextView) findViewById(R.id.Current_Time);
    seekBar = (SeekBar) findViewById(R.id.SeekBar);
    seekBar.setClickable(false);
    Repeat_N = R.drawable.ic_repeat_white_48dp;
    P_and_P_N = R.drawable.ic_pause_white_48dp;

    mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

    Song = new ArrayList<>();

    Song.add(new Songs("Alone", "Rate: 4.0", R.raw.alone, R.drawable.alone));
    Song.add(new Songs("Anger", "Rate: 4.5", R.raw.anger, R.drawable.anger ));
    Song.add(new Songs("Bad Situation", "Rate: 3.6", R.raw.bad_situation, R.drawable.badsitu));
    Song.add(new Songs("Beni Soubi", "Rate: 4.0", R.raw.beni_soubi, R.drawable.beni));
    Song.add(new Songs("Confronting", "Rate: 3.2", R.raw.confronting, R.drawable.confronting));
    Song.add(new Songs("Daylight Of Konoha", "Rate: 4.7", R.raw.d_o_k, R.drawable.d_o_k ));
    Song.add(new Songs("Dark Clouds", "Rate: 3.1", R.raw.dark_clouds, R.drawable.darkc));
    Song.add(new Songs("Dept. To The Front Lines", "Rate: 5.0", R.raw.departure_to_the_front_lines, R.drawable.d_t_t_f_l));
    Song.add(new Songs("Despair", "Rate: 3.6", R.raw.despair, R.drawable.despair));                

    SongsAdapter SongAdapter = new SongsAdapter(this, Song, R.color.Main_Color);
    ListView listView = (ListView) findViewById(R.id.list_Col);
    listView.setAdapter(SongAdapter);

    seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        int ProgressChanged = 0;

        @Override
        public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
            ProgressChanged = i;
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            mediaplayer.seekTo(ProgressChanged);

        }
    });

    Repeat.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (Repeat_Value == 0) {
                Repeat_Value = 1;
                Repeat.setImageResource(R.drawable.ic_repeat_one_white_48dp);
                if (mediaplayer != null) {
                    if (Repeat_Value == 0) {
                        showNotification(C_Name, C_Rate, C_Image, R.drawable.ic_play_arrow_white_48dp, R.drawable.ic_repeat_white_48dp);
                    } else {
                        showNotification(C_Name, C_Rate, C_Image, R.drawable.ic_play_arrow_white_48dp, R.drawable.ic_repeat_one_white_48dp);
                    }
                }
            } else {
                Repeat_Value = 0;
                Repeat.setImageResource(R.drawable.ic_repeat_white_48dp);
            }
        }
    });
    Next.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {

                                    if (mediaplayer != null) {
                                        mediaplayer.release();
                                        mediaplayer = null;

                                        if (II == (Song.size() - 1)) {
                                            II = 0;
                                        } else {
                                            II += 1;
                                        }
                                        music_player(II);

                                    }
                                }
                            }
    );

    Previous.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View view) {

                                        if (mediaplayer != null) {
                                            mediaplayer.release();
                                            mediaplayer = null;
                                            if (II == 0) {
                                                II = (Song.size() - 1);
                                            } else {
                                                II -= 1;
                                            }
                                            music_player(II);
                                        }
                                    }
                                }
    );

    P_and_P.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (mediaplayer != null) {
                if (!mediaplayer.isPlaying() || IsPaused) {
                    P_and_P.setImageResource(R.drawable.ic_pause_white_48dp);
                    IsPaused = false;
                    if (Repeat_Value == 0) {
                        showNotification(C_Name, C_Rate, C_Image, R.drawable.ic_pause_white_48dp, R.drawable.ic_repeat_white_48dp);
                    } else {
                        showNotification(C_Name, C_Rate, C_Image, R.drawable.ic_pause_white_48dp, R.drawable.ic_repeat_one_white_48dp);
                    }
                    mediaplayer.start();
                } else {
                    P_and_P.setImageResource(R.drawable.ic_play_arrow_white_48dp);
                    IsPaused = true;
                    if (Repeat_Value == 0) {
                        showNotification(C_Name, C_Rate, C_Image, R.drawable.ic_play_arrow_white_48dp, R.drawable.ic_repeat_white_48dp);
                    } else {
                        showNotification(C_Name, C_Rate, C_Image, R.drawable.ic_play_arrow_white_48dp, R.drawable.ic_repeat_one_white_48dp);
                    }
                    mediaplayer.pause();
                }
            }

        }
    });


    Stop.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (mediaplayer != null) {
                P_and_P.setImageResource(R.drawable.ic_play_arrow_white_48dp);
                mediaplayer.pause();
                mediaplayer.seekTo(0);
                IsPaused = true;
                if (Repeat_Value == 0) {
                    showNotification(C_Name, C_Rate, C_Image, R.drawable.ic_play_arrow_white_48dp, R.drawable.ic_repeat_white_48dp);
                } else {
                    showNotification(C_Name, C_Rate, C_Image, R.drawable.ic_play_arrow_white_48dp, R.drawable.ic_repeat_one_white_48dp);
                }
            }
        }
    });



    listView.setOnItemClickListener
            (new AdapterView.OnItemClickListener()
             {
                 @Override
                 public void onItemClick(AdapterView<?> adapterView, View view, int i, long l)
                 {

                     Log.v("1", "Just upon entering onItemClick");
                     music_player(i);
                 }
             }
            );
}

public void music_player(int I) {

    UpdateSongTime = new Runnable() {
        public void run() {
            if(mediaplayer != null) {
                startTime = mediaplayer.getCurrentPosition();

                if ((TimeUnit.MILLISECONDS.toSeconds((long) startTime)) -
                        TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes((long) startTime)) < 10) {
                    Current_Time.setText(String.format("0%d:0%d",
                            TimeUnit.MILLISECONDS.toMinutes((long) startTime),
                            TimeUnit.MILLISECONDS.toSeconds((long) startTime) -
                                    TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.
                                            toMinutes((long) startTime)))
                    );
                } else {
                    Current_Time.setText(String.format("0%d:%d",
                            TimeUnit.MILLISECONDS.toMinutes((long) startTime),
                            TimeUnit.MILLISECONDS.toSeconds((long) startTime) -
                                    TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.
                                            toMinutes((long) startTime))));
                }
            }

            seekBar.setProgress((int) startTime);
            myHandler.postDelayed(this, 100);
        }
    };


    II = I;
    IIII = II;
    int Current_Song;
    song = Song.get(I);

    //If mediaPlayer is not used before, this will make oldsong as present song.
    if (Old_Song == -326523) {
        Old_Song = song.getSong();
    }

    Current_Song = song.getSong();
    C_Name = song.getNameOfSong();
    C_Image = song.getImage();
    C_Rate = song.getDeveloperRate();
    C_Repeat = R.drawable.ic_repeat_white_48dp;
    P_and_P_N = R.drawable.ic_pause_white_48dp;

    //If mediaPlayer is paused.
    if (IsPaused) {
        P_and_P.setImageResource(R.drawable.ic_pause_white_48dp);
        P_and_P_N = R.drawable.ic_pause_white_48dp;
        //If the song paused is same as the new song.
        if (Current_Song == Old_Song) {
            mediaplayer.start();
            NameD.setText(C_Name);
            RateD.setText(C_Rate);
            ImageD.setImageResource(C_Image);
        }

        //If the song Paused is not the new song.
        else {
            if (mediaplayer != null) {
                mediaplayer.release();
                mediaplayer = null;
            }

            int result = mAudioManager.requestAudioFocus(mOnAudioFocusChangeListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);

            if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
                mediaplayer = mediaplayer.create(SongsListActivity.this, song.getSong());
                seekBar.setMax(mediaplayer.getDuration());
                Old_Song = song.getSong();
                NameD.setText(C_Name);
                RateD.setText(C_Rate);
                ImageD.setImageResource(C_Image);
                mediaplayer.start();
                if (Repeat_Value == 0) {
                    showNotification(C_Name, C_Rate, C_Image, P_and_P_N, R.drawable.ic_repeat_white_48dp);
                } else {
                    showNotification(C_Name, C_Rate, C_Image, P_and_P_N, R.drawable.ic_repeat_one_white_48dp);
                }

                if ((TimeUnit.MILLISECONDS.toSeconds((long) mediaplayer.getDuration()) -
                        TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes((long) mediaplayer.getDuration()))) < 10) {
                    Duration.setText(String.format("0%d:0%d",
                            TimeUnit.MILLISECONDS.toMinutes((long) mediaplayer.getDuration()),
                            TimeUnit.MILLISECONDS.toSeconds((long) mediaplayer.getDuration()) -
                                    TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes((long) mediaplayer.getDuration()))));
                } else {
                    Duration.setText(String.format("0%d:%d",
                            TimeUnit.MILLISECONDS.toMinutes((long) mediaplayer.getDuration()),
                            TimeUnit.MILLISECONDS.toSeconds((long) mediaplayer.getDuration()) -
                                    TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes((long) mediaplayer.getDuration()))));
                }

                seekBar.setProgress((int) startTime);
                myHandler.postDelayed(UpdateSongTime, 100);

                mediaplayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer mediaPlayer) {
                        P_and_P.setImageResource(R.drawable.ic_play_arrow_white_48dp);
                        P_and_P_N = R.drawable.ic_play_arrow_white_48dp;
                        IsPaused = true;
                        if (mediaplayer != null) {
                            mediaplayer.release();
                            mediaplayer = null;
                        }
                        if (Repeat_Value == 0) {
                            if (II == (Song.size() - 1)) {
                                II = 0;
                            } else {
                                II += 1;
                            }
                        } else {
                            II = IIII;
                        }
                        IsPaused = false;
                        music_player(II);
                    }
                });
            }
        }
        IsPaused = false;
        if (Repeat_Value == 0) {
            showNotification(C_Name, C_Rate, C_Image, P_and_P_N, R.drawable.ic_repeat_white_48dp);
        } else {
            showNotification(C_Name, C_Rate, C_Image, P_and_P_N, R.drawable.ic_repeat_one_white_48dp);
        }

    } else if (mediaplayer != null) {
        //If mediaPlayer is already Playing a song.
        if (mediaplayer.isPlaying()) {
            P_and_P.setImageResource(R.drawable.ic_play_arrow_white_48dp);
            P_and_P_N = R.drawable.ic_play_arrow_white_48dp;
            mediaplayer.pause();
            IsPaused = true;
            if (Repeat_Value == 0) {
                showNotification(C_Name, C_Rate, C_Image, P_and_P_N, R.drawable.ic_repeat_white_48dp);
            } else {
                showNotification(C_Name, C_Rate, C_Image, P_and_P_N, R.drawable.ic_repeat_one_white_48dp);
            }
            if (Current_Song != Old_Song) {
                NameD.setText(song.getNameOfSong());
                RateD.setText(song.getDeveloperRate());
                ImageD.setImageResource(song.getImage());
                if (mediaplayer != null) {
                    mediaplayer.release();
                    mediaplayer = null;
                }
                P_and_P.setImageResource(R.drawable.ic_pause_white_48dp);
                P_and_P_N = R.drawable.ic_pause_white_48dp;
                IsPaused = false;
                if (Repeat_Value == 0) {
                    showNotification(C_Name, C_Rate, C_Image, P_and_P_N, R.drawable.ic_repeat_white_48dp);
                } else {
                    showNotification(C_Name, C_Rate, C_Image, P_and_P_N, R.drawable.ic_repeat_one_white_48dp);
                }
                music_player(II);

            }
        }
    }

    //If mediaPlayer is used for first time and if mediaPlayer is neither paused
    else {

        if (mediaplayer != null) {
            mediaplayer.release();
            mediaplayer = null;
        }

        int result = mAudioManager.requestAudioFocus(mOnAudioFocusChangeListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);

        if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
            mediaplayer = mediaplayer.create(SongsListActivity.this, song.getSong());
            seekBar.setMax(mediaplayer.getDuration());
            Old_Song = song.getSong();
            NameD.setText(song.getNameOfSong());
            RateD.setText(song.getDeveloperRate());
            ImageD.setImageResource(song.getImage());
            mediaplayer.start();
            if (Repeat_Value == 0) {
                showNotification(C_Name, C_Rate, C_Image, P_and_P_N, R.drawable.ic_repeat_white_48dp);
            } else {
                showNotification(C_Name, C_Rate, C_Image, P_and_P_N, R.drawable.ic_repeat_one_white_48dp);
            }
            if ((TimeUnit.MILLISECONDS.toSeconds((long) mediaplayer.getDuration()) -
                    TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes((long) mediaplayer.getDuration()))) < 10) {
                Duration.setText(String.format("0%d:0%d",
                        TimeUnit.MILLISECONDS.toMinutes((long) mediaplayer.getDuration()),
                        TimeUnit.MILLISECONDS.toSeconds((long) mediaplayer.getDuration()) -
                                TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes((long) mediaplayer.getDuration()))));
            } else {
                Duration.setText(String.format("0%d:%d",
                        TimeUnit.MILLISECONDS.toMinutes((long) mediaplayer.getDuration()),
                        TimeUnit.MILLISECONDS.toSeconds((long) mediaplayer.getDuration()) -
                                TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes((long) mediaplayer.getDuration()))));
            }

            seekBar.setProgress((int) startTime);
            myHandler.postDelayed(UpdateSongTime, 100);

            P_and_P.setImageResource(R.drawable.ic_pause_white_48dp);
            P_and_P_N = R.drawable.ic_pause_white_48dp;

            mediaplayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mediaPlayer) {
                    P_and_P.setImageResource(R.drawable.ic_play_arrow_white_48dp);
                    IsPaused = true;
                    if (Repeat_Value == 0) {
                        showNotification(C_Name, C_Rate, C_Image, P_and_P_N, R.drawable.ic_repeat_white_48dp);
                    } else {
                        showNotification(C_Name, C_Rate, C_Image, P_and_P_N, R.drawable.ic_repeat_one_white_48dp);
                    }
                    if (mediaplayer != null) {
                        mediaplayer.release();
                        mediaplayer = null;
                    }

                    if (Repeat_Value == 0) {
                        if (II == (Song.size() - 1)) {
                            II = 0;
                        } else {
                            II += 1;
                        }
                    } else {
                        II = IIII;
                    }
                    IsPaused = false;
                    if (Repeat_Value == 0) {
                        showNotification(C_Name, C_Rate, C_Image, P_and_P_N, R.drawable.ic_repeat_white_48dp);
                    } else {
                        showNotification(C_Name, C_Rate, C_Image, P_and_P_N, R.drawable.ic_repeat_one_white_48dp);
                    }
                    music_player(II);
                }
            });
        }
    }
}


@Override
protected void onDestroy() {
    super.onDestroy();
    if(mediaplayer != null)
    {
        mediaplayer.release();
        mediaplayer = null;
        mAudioManager.abandonAudioFocus(mOnAudioFocusChangeListener);
        notificationManager.cancelAll();
    }
    Log.e("Checkup", "onDestroy: working");
}
}

notification_layout.xml: -

<?xml version="1.0" encoding="utf-8"?>    
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#2196F3">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:background="#2196F3"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/Image_N"
        android:layout_width="100dp"
        android:layout_height="100dp"
        tools:ignore="ContentDescription" />

    <LinearLayout
        android:id="@+id/text_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:id="@+id/Name_N"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:gravity="bottom"
            android:textAppearance="?android:textAppearanceMedium"
            android:textColor="@android:color/white"
            android:textStyle="bold"
            android:maxLength="25"/>

        <TextView
            android:id="@+id/Rate_N"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:gravity="top"
            android:textAppearance="?android:textAppearanceMedium"
            android:textColor="#F44336" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">

            <ImageButton
                android:id="@+id/Repeat_N"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:src="@drawable/ic_repeat_white_48dp"
                android:background="#2196F3"/>

            <ImageButton
                android:id="@+id/Previous_N"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:src="@drawable/ic_skip_previous_white_48dp"
                android:background="#2196F3"/>

            <ImageButton
                android:id="@+id/P_and_P_N"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:src="@drawable/ic_play_arrow_white_48dp"
                android:background="#2196F3"/>

            <ImageButton
                android:id="@+id/Next_N"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:src="@drawable/ic_skip_next_white_48dp"
                android:background="#2196F3"/>
        </LinearLayout>

    </LinearLayout>

</LinearLayout>

现在我的问题是: -

当我点击通知上的ImageButtons然后他们执行各自的工作时,我应该怎么做。(我查看了许多地方以获得解决方案,甚至尝试实施它们但我没有成功。)

1 个答案:

答案 0 :(得分:0)

这是一个坏主意,你应该使用Service来播放音频。它没有运行或删除,它将在第一次按钮点击时启动,Activity只是一个UI,你不应该阻止屏幕旋转。使用PendingIntent.getService(..)获取通知按钮的PendingIntent,每个包装的Intent应该有动作,表示播放动作的类型。