2016-09-29 77 views
0

老實說,我不知道爲什麼,但任何解決方案對於同樣的問題,我已經在stackoverflow不工作對我來說 我想點擊播放/暫停按鈕切換,當爲我的電臺播放我的流時。Android更改播放/暫停按鈕圖標點擊時

我不知道我在做什麼錯。

目前它是分開的,因爲我的許多嘗試都失敗了。

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/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> 

回答

0

您應該用一個按鈕替換兩個按鈕。我將它命名爲bt_play_pause。然後,將兩個圖像添加到資源文件夾,它們是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; 
     } 
    } 
} 
+0

呀,只是我不知道該怎麼做,並在那裏把這段代碼。我完全是初學者。 –

+0

我已經有了這些圖像。 play.png和pause.png繪製。 我只需要main.xml的代碼以及在我的.java文件中替換什麼,以便它可以工作。 您的代碼將不會幫助 –

+0

我試着用這個代碼 'boolean paused = true; iv_start_and_pause.setOnClickListener(新View.OnClickListener(){ @覆蓋 公共無效的onClick(視圖v){ 如果(暫停){ mediaPlayer.start(); iv_start_and_pause.setImageResource(R.drawable.pause); 暫停= FALSE; } 否則,如果(暫停!){ mediaPlayper.pause(); iv_start_and_pause.setImageResource(R.drawable.start); 暫停= TRUE; }} });' –

0

變化<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) 
... 
imgBtn.setImageResource(R.drawable.pause); 
       //when MediaPlayer stared, pause button is shown 
       } 
       else{ 
       if(mPlayer!=null && mPlayer.isPlaying()){ 
        mPlayer.stop(); 
        paused = true; 
        imgBtn.setImageResource(R.drawable.start); 
        //when MediaPlayer paused, startbutton is shown 
       } 
+1

你意識到我不知道該把它放在哪裏? –

+0

這個答案是最準確的,因爲如果不將Button標籤更改爲ImageButton,則永遠不會顯示圖像按鈕。此外,我從你的上述代碼複製並修改它... – DevMike

+0

我知道,但在你之前,另一個答案提供了完整的代碼,我只是用你所建議的圖像按鈕替換按鈕。 –