Android改变播放/暂停按钮图标时点击



老实说,我不知道为什么,但任何解决同样的问题,我已经在stackoverflow上不适合我我试图有播放/暂停按钮切换时点击,当播放我的流为我的广播电台。

我不知道我做错了什么。

目前它是独立的,因为我的许多尝试都失败了。

main.xml(很多人称之为activity main)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/bg"
    android:weightSum="1">
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/logo"
        android:id="@+id/imageView4" />
    <Button
        android:id="@+id/play"
        android:layout_margin="10dip"
        android:background="@drawable/play"
        android:layout_width="50dp"
        android:layout_height="50dp" />
    <Button
        android:id="@+id/stop"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_margin="10dip"
        android:background="@drawable/pause" />
</LinearLayout>

MusicAndroidActivity.java

    package clever.radio;
    import java.io.IOException;
    import android.app.Activity;
    import android.graphics.BitmapFactory;
    import android.media.AudioManager;
    import android.media.MediaPlayer;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.webkit.WebView;
    import android.widget.Button;
    import android.widget.Toast;
    import static android.R.attr.id;
    public class MusicAndroidActivity extends Activity {
        static MediaPlayer mPlayer;
        Button buttonPlay;
        Button buttonStop;
        String url = "http://radioclever.stream:8000/stream";
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            buttonPlay = (Button) findViewById(R.id.play);
            buttonPlay.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    mPlayer = new MediaPlayer();
                    mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                    try {
                        mPlayer.setDataSource(url);
                    } catch (IllegalArgumentException e) {
                        Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
                    } catch (SecurityException e) {
                        Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
                    } catch (IllegalStateException e) {
                        Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    try {
                        mPlayer.prepare();
                    } catch (IllegalStateException e) {
                        Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
                    } catch (IOException e) {
                        Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
                    }
                    mPlayer.start();
                }
            });
            buttonStop = (Button) findViewById(R.id.stop);
            buttonStop.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    if(mPlayer!=null && mPlayer.isPlaying()){
                        mPlayer.stop();
                    }
                }
            });
        }
        protected void onDestroy() {
            super.onDestroy();
            // TODO Auto-generated method stub
            if (mPlayer != null) {
                mPlayer.release();
                mPlayer = null;
            }
        }
    }

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="clever.radio"
    android:versionCode="1"
    android:versionName="1.1" >
    <uses-sdk
        android:minSdkVersion="9"
        android:targetSdkVersion="15" />
    <uses-permission android:name="android.permission.INTERNET" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MusicAndroidActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

<Button...改为<ImageButton…您的xml现在看起来应该是这样的。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/bg"
    android:weightSum="1">

    <ImageButton
        android:id="@+id/play_or_pause"
        android:layout_margin="10dip"
        android:background="@drawable/play"
        android:layout_width="50dp"
        android:layout_height="50dp" />

</LinearLayout>

并在Java代码中引用

ImageButton imgBtn = (ImageButton)findViewById(R.id.play_or_pause)
...
if ( mPlayer != null && mPlayer.isPlaying() ) {
    // When MediaPlayer paused, start button is shown
    mPlayer.stop();
    paused = true;
    imgBtn.setImageResource(R.drawable.start);
} else {
    // When MediaPlayer started, pause button is shown
    imgBtn.setImageResource(R.drawable.pause);
}

你应该用一个按钮代替两个按钮。我把它命名为bt_play_pause。然后,将两个图像添加到Resource文件夹中,它们是pause.png和start.png,可能是。看看我的代码。希望它能帮到你。

main。xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/bg"
android:weightSum="1">
<ImageView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:src="@drawable/logo"
    android:id="@+id/imageView4" />
<Button
    android:id="@+id/bt_start_pause"
    android:layout_margin="10dip"
    android:background="@drawable/play"
    android:layout_width="50dp"
    android:layout_height="50dp" />
</LinearLayout>

MusicAndroidActivity.java

public class MusicAndroidActivity extends Activity {
    static MediaPlayer mPlayer;
    Button bt_start_pause;
    boolean paused = true;
    String url = "http://radioclever.stream:8000/stream";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        bt_start_pause= (Button) findViewById(R.id.bt_start_pause);
        bt_start_pause.setImageResource(R.drawable.start); bt_start_pause.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                if(paused){
                mPlayer = new MediaPlayer();
                mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                try {
                    mPlayer.setDataSource(url);
                } catch (IllegalArgumentException e) {
                    Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
                } catch (SecurityException e) {
                    Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
                } catch (IllegalStateException e) {
                    Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    mPlayer.prepare();
                } catch (IllegalStateException e) {
                    Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
                } catch (IOException e) {
                    Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
                }
                mPlayer.start();
                paused = false;
                bt_start_pause.setImageResource(R.drawable.pause);
                //when MediaPlayer stared, pause button is shown
                }
                else{
                if(mPlayer!=null && mPlayer.isPlaying()){
                    mPlayer.stop();
                    paused = true;
                    bt_start_pause.setImageResource(R.drawable.start);
                   //when MediaPlayer paused, startbutton is shown
                }
                }
            }
        });
    }
    protected void onDestroy() {
        super.onDestroy();
        // TODO Auto-generated method stub
        if (mPlayer != null) {
            mPlayer.release();
            mPlayer = null;
        }
    }
}

最新更新