0

我一直在寻找如何显示/隐藏我为此 mediaPlayer 创建的按钮,但找不到好的示例……我希望控件像 mediaController 一样消失并重新出现在屏幕点击上。对 Java 和 Android 来说非常新,播放器和控件按预期工作,但现在我需要在屏幕点击时隐藏/显示它们。感谢您的帮助(XML 位于 Java 代码下方)

    public class Audio_Image extends BT_activity_base implements
      MediaPlayer.OnCompletionListener, MediaPlayer.OnPreparedListener,
      MediaPlayer.OnErrorListener, MediaPlayer.OnBufferingUpdateListener {

     private String TAG = getClass().getSimpleName();
     private MediaPlayer mp = null;
     private boolean mShowing = false;

     private Button play;
     private Button pause;
     private Button stop;

     private String dataURL = "";
     public String thisActivityName = "Audio_Image";

     @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);


            //reference to base layout..
           LinearLayout baseView = (LinearLayout)findViewById(R.id.baseView);

           //setup background colors...
           BT_viewUtilities.updateBackgroundColorsForScreen(this, this.screenData);

           //setup background images..
           if(backgroundImageWorkerThread == null){
               backgroundImageWorkerThread = new BackgroundImageWorkerThread();
               backgroundImageWorkerThread.start();
           }            

           //setup navigation bar...
           LinearLayout navBar = BT_viewUtilities.getNavBarForScreen(this, this.screenData);
           if(navBar != null){
               baseView.addView(navBar);
           }

           //inflate this screens layout file...
           LayoutInflater vi = (LayoutInflater)thisActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
           View thisScreensView = vi.inflate(R.layout.audio_image, null);

           //add the view to the base view...
           baseView.addView(thisScreensView);

         //fill JSON properties...
                dataURL = BT_strings.getJsonPropertyValue(this.screenData.getJsonObject(), "dataURL", "");




      play = (Button) findViewById(R.id.play);
      pause = (Button) findViewById(R.id.pause);
      stop = (Button) findViewById(R.id.stop);

      play.setOnClickListener(new View.OnClickListener() {
       public void onClick(View view) {
        play();



       }
      });

      pause.setOnClickListener(new View.OnClickListener() {
       public void onClick(View view) { 

           pause();

       }
      });

      stop.setOnClickListener(new View.OnClickListener() {
       public void onClick(View view) {
        stop();
       }
      });
     }

     private void play() {
         showProgress("Starting audio stream...", 
                 "Please be patient. If you're on 3G this could take a bit.\nWi-Fi is much better for streaming.");

      Uri myUri = Uri.parse(dataURL);
      try {
       if (mp == null) {
        this.mp = new MediaPlayer();
       } else {
        mp.stop();
        mp.reset();
       }
       mp.setDataSource(this, myUri); // Go to Initialized state
       mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
       mp.setOnPreparedListener(this);
       mp.setOnBufferingUpdateListener(this);

       mp.setOnErrorListener(this);
       mp.prepareAsync();

       Log.d(TAG, "LoadClip Done");
      } catch (Throwable t) {
       Log.d(TAG, t.toString());
      }
     }

     @Override
     public void onPrepared(MediaPlayer mp) { 
      Log.d(TAG, "Stream is prepared");
      mp.start();
      mp.setLooping(true);
      hideProgress();
     }

     private void pause() {
         if(mp!=null) 
         if (mp.isPlaying()) { pause.setText("Pause/Resume");
             mp.pause();
          } else { 
              mp.start();
          }

     }



     private void stop() {
         if(mp!=null) mp.stop();

     }

     @Override
     public void onDestroy() {
      super.onDestroy();
      stop();

     }

     public void onCompletion(MediaPlayer mp) {
         stop();

     }

     public boolean onError(MediaPlayer mp, int what, int extra) {
      StringBuilder sb = new StringBuilder();
      sb.append("Media Player Error: ");
      switch (what) {
      case MediaPlayer.MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK:
       sb.append("Not Valid for Progressive Playback");
       break;
      case MediaPlayer.MEDIA_ERROR_SERVER_DIED:
       sb.append("Server Died");
       break;
      case MediaPlayer.MEDIA_ERROR_UNKNOWN:
       sb.append("Unknown");
       break;
      default:
       sb.append(" Non standard (");
       sb.append(what);
       sb.append(")");
      }
      sb.append(" (" + what + ") ");
      sb.append(extra);
      Log.e(TAG, sb.toString());
      return true;
     }

     public void onBufferingUpdate(MediaPlayer mp, int percent) {
      Log.d(TAG, "PlayerService onBufferingUpdate : " + percent + "%");
     }

        }

XML 文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"

 android:layout_width="fill_parent"
 android:layout_height="fill_parent">
 <Button
  android:text="Play"
  android:id="@+id/play"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:textSize="20sp">

 </Button>
 <Button
  android:text="Pause"
  android:id="@+id/pause"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:textSize="20sp"></Button>
 <Button
  android:text="Stop"
  android:id="@+id/stop"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:textSize="20sp"></Button>
</LinearLayout>
4

1 回答 1

1

就我而言,我将media player通过将其设为LinearLayout. 这样我可以重用它,并在setVisiblity()这里显示/隐藏是示例

public Player(Context context) {
    super(context);
    this.context = context;
    LayoutInflater.from(context).inflate(R.layout.player, this, true);
    progressText = (TextView) findViewById(R.id.playerProcessText);
    progressControl = (SeekBar) findViewById(R.id.playerSeekbar);
    btnPlay = (Button) findViewById(R.id.playerPlay);
    btnStop = (Button) findViewById(R.id.playerStop);
    btnClose = (Button) findViewById(R.id.playerClose);
    initListener();
}

public void setFilename(String filename) {
    this.filename = filename;
}

private void initListener() {
    // TODO Auto-generated method stub
    btnPlay.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if (isStop && !isPlay && !isPaused) {
                release();
                setPlayer();
                isStop = false;
                btnPlay.setText("PlayAfterStop");
                onEverySecond.run();
            }
            if (!isPaused) {
                mediaPlayer.pause();
                btnPlay.setText("PauseWhilePlay");
                isPaused = true;

            } else if (isPlay) {
                mediaPlayer.start();
                btnPlay.setText("StartAfterPause");
                isPaused = false;
                isPlay = true;
                onEverySecond.run();
            }
        }
    });

    btnStop.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            setStop();

        }
    });

    btnClose.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            setStop();
            Player.this.setVisibility(View.GONE);
        }
    })

.....................

在此处输入图像描述

于 2012-06-03T03:13:40.300 回答