当button2歌曲播放时如何停止button1歌曲&反之亦然

时间:2016-04-23 12:04:58

标签: android

package com.example.mymediaplayer;

import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.widget.Button;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.MediaController;
import android.widget.VideoView;

public class MainActivity extends Activity  implements OnClickListener {

    Button button1,button2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button1 = (Button) findViewById(R.id.button11);
        button1.setOnClickListener(this);

        button2 = (Button) findViewById(R.id.button22);
        button2.setOnClickListener(this);
    }

    @Override 
    public void onClick(View view) 
    {   
        if(view==button1)
        {
            MediaPlayer mp=new MediaPlayer();
            try{
                mp.setDataSource("/sdcard/Q/kickson.mp3");
                mp.prepare();
                mp.start();
            } catch(Exception e){
                e.printStackTrace();}
            }
        if(view==button2)
        {
            MediaPlayer mp=new MediaPlayer();
            try{
                mp.setDataSource("/sdcard/Q/maleder.mp3");
                mp.prepare();
                mp.start();
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }
}

我的XML文件是

<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="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="34dp"
        android:text="Playing Audio from SD Card" />

    <Button
        android:id="@+id/button11"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="56dp"
        android:text="song 1" />

    <Button
        android:id="@+id/button22"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_centerVertical="true"
        android:text="song 2" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

试试这个

public class MainActivity extends Activity  implements OnClickListener {

Button button1,button2;
MediaPlayer mp;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    button1 = (Button) findViewById(R.id.button11);
    button1.setOnClickListener(this);
    button2 = (Button) findViewById(R.id.button22);
    button2.setOnClickListener(this);
    mp=new MediaPlayer();
}

@Override 
public void onClick(View view) 
{   
    if(view==button1)
    {

        try{
             if(mp.isPlaying()) {
             //stop or pause your media player mp.stop(); or mp.pause();
              mp.stop(); 
            mp.setDataSource("/sdcard/Q/kickson.mp3");
            mp.prepare();
            mp.start();
           } else {
            mp.setDataSource("/sdcard/Q/kickson.mp3");
            mp.prepare();
            mp.start();
           }
        } catch(Exception e){
            e.printStackTrace();}
        }
    if(view==button2)
    {

        try{
            if(mp.isPlaying()) {
             //stop or pause your media player mp.stop(); or mp.pause();
              mp.stop(); 
              mp.setDataSource("/sdcard/Q/maleder.mp3");
              mp.prepare();
              mp.start();
            } else {
              mp.setDataSource("/sdcard/Q/maleder.mp3");
              mp.prepare();
              mp.start();
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}

}