【问题标题】:How to stop background media player如何停止后台媒体播放器
【发布时间】:2015-12-10 17:34:04
【问题描述】:

嗨,我正在创建一个类似声音云的应用程序。主活动中有一个歌曲列表,当用户单击其中一首歌曲时,一个新活动将打开,如果用户单击播放按钮,则在该活动中歌曲如果用户返回主活动,歌曲也会继续播放,但如果用户选择另一首歌曲并播放,则前一首歌曲继续播放新歌曲。

当用户点击新歌的播放按钮时,如何阻止上一首歌曲在后台播放?

这是我的音频播放器活动的代码

public class Songs_Single extends AppMenu {
ListView lv;
static final String[] numbers = new String[] { "one", "two", "three",
        "four", "five", "six", "seven", "eight", "nine", "ten", "eleven",
        "twelve", "thirteen", "fourteen", "fifteen", "sixteen",
        "seventeen", "eighteen", "nineteen", "twenty", "twenty one",
        "twenty two" };
ImageView cover;
ImageView playBtn;
ImageView prevBtn;
ImageView nextBtn;
SeekBar songSeekBar;
SeekBar volumeSeekBar;
ImageView actionbar_Post_Cover;
TextView actionbar_Title;
TextView actionbar_Subtitle;
TextView SongCurrentTime;
TextView SongDuration;
protected MediaPlayer mediaPlayer;
AudioManager audioManager;
int currentSec;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    LayoutInflater inflater = (LayoutInflater) this
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View contentView = inflater.inflate(R.layout.songs_single, null, false);
    drawerLayout.addView(contentView, 0);
    ViewGroup header = (ViewGroup) inflater.inflate(R.layout.songs_single_header, lv,
            false);
    ArrayAdapter adapter = new ArrayAdapter(this,
            android.R.layout.simple_list_item_1, numbers);
    lv = (ListView) findViewById(R.id.Songs_Single_mahdi);
    lv.addHeaderView(header);
    lv.setAdapter(adapter);
    final ImageView logo = (ImageView)findViewById(R.id.actionbar_logo);
    logo.setVisibility(View.GONE);
    cover = (ImageView)findViewById(R.id.Songs_Single_Cover);
    actionbar_Post_Cover = (ImageView)findViewById(R.id.actionbar_Post_Cover);
    actionbar_Post_Cover.setVisibility(View.VISIBLE);
    actionbar_Title = (TextView) findViewById(R.id.actionbar_Title);
    actionbar_Subtitle = (TextView) findViewById(R.id.actionbar_Subtitle);
    actionbar_Subtitle.setVisibility(View.VISIBLE);
    playBtn = (ImageView)findViewById(R.id.Songs_Single_Controls_Controls_PlayBtn);
    nextBtn = (ImageView)findViewById(R.id.Songs_Single_Controls_Controls_NextBtn);
    prevBtn = (ImageView)findViewById(R.id.Songs_Single_Controls_Controls_PrevBtn);
    songSeekBar = (SeekBar)findViewById(R.id.Songs_Single_Controls_SeekBar_SeekBar);
    volumeSeekBar = (SeekBar)findViewById(R.id.Songs_Single_Controls_VolumeBar_VolumeBar);
    SongCurrentTime = (TextView) findViewById(R.id.Songs_Single_Controls_SongCurrentTime);
    SongDuration = (TextView) findViewById(R.id.Songs_Single_Controls_SongDuration);
    if (Values.coverUrl != ""){
        Picasso.with(getApplicationContext()).load(Values.coverUrl).into(cover);
        Picasso.with(getApplicationContext()).load(Values.coverUrl).into(actionbar_Post_Cover);
    }
    if (Values.postLikes != "")
        actionbar_Subtitle.setText(Values.postLikes+" Likes | "+Values.postDislikes+" Dislikes | "+Values.postViews+" Plays" );
    if ((Values.postTitle != "") && (Values.songName != ""))
        actionbar_Title.setText(Values.postTitle+" - "+Values.songName);


    mediaPlayer = new MediaPlayer();
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
    int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
    int currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
    volumeSeekBar.setMax(maxVolume);
    volumeSeekBar.setProgress(currentVolume);
    volumeSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,progress,0);
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    });
    playBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                mediaPlayer.setDataSource(Values.songPlayerUrl);
                mediaPlayer.prepare();
            } catch (IOException e) {
                e.printStackTrace();
                Toast.makeText(getApplicationContext(),"Error playing song.",Toast.LENGTH_LONG).show();
            }
            mediaPlayer.start();
            int durationMin = ((mediaPlayer.getDuration())/1000) / 60;
            int durationSec = ((mediaPlayer.getDuration())/1000) % 60;
            final int currentMin = ((mediaPlayer.getCurrentPosition())/1000) / 60;
            currentSec = ((mediaPlayer.getCurrentPosition())/1000) % 60;
            Thread t = new Thread() {
                @Override
                public void run() {
                    try {
                        while (!isInterrupted()) {
                            Thread.sleep(1000);
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                        SongCurrentTime.setText(currentMin+ " : "+currentSec);
                                }
                            });
                        }
                    } catch (InterruptedException e) {
                        //
                    }
                }
            };
            t.start();
            SongDuration.setText(durationMin+ " : "+durationSec);
        }
    });


 }
}

【问题讨论】:

    标签: android audio


    【解决方案1】:

    问题可能正在发生,因为您没有停止在 Songs_Single 活动中创建的媒体播放器,然后当您进入主活动时,您正在创建另一个媒体播放器实例并播放另一首歌曲,它将同时播放媒体播放器同时。

    要解决这个问题,您必须使用相同的MediaPlayer 来管理后台歌曲,您可以使用ServiveIntent Service 来做到这一点。

    有关服务的工作,请阅读此answer

    【讨论】:

    【解决方案2】:

    mp 是您的媒体播放器。

    if (mp != null) {
        mp.stop();
        mp.release();
        mp = null;
    }
    

    【讨论】:

    • 它给了我力量关闭
    • 我的应用程序崩溃了,我必须在哪里添加这个?
    • 当你想停止它时,你可以在那里使用它。例如单击按钮?或者当应用程序进入后台?这是你的选择
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多