1

我找不到停止隐藏搜索栏的方法。一旦歌曲播放,搜索栏会在 2-3 秒后隐藏。我对此真的很陌生,所以不太确定如何解决这个问题。当歌曲正在播放但没有运气时,我尝试将 mediaController.show() 放入我的方法中。我从下面的 Youtube 频道中找到了这段代码:

 public class MainActivity extends ListActivity implements OnPreparedListener,
  MediaController.MediaPlayerControl {

private static final String TAG = "AudioPlayer";
private ListView list;
private MainArrayAdapter adapter;
private MediaPlayer mediaPlayer;
private MediaController mediaController;
private String audioFile;
private Handler handler = new Handler();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    list = getListView();

    mediaPlayer = new MediaPlayer();
    mediaPlayer.setOnPreparedListener(this);
    mediaController = new MediaController(this);

    ArrayList<String> array_list_music = new ArrayList<String>();
    array_list_music.add("Sammy Kaye And His Orchestra" + " ### "
            + "Minka  (1941)" + " ### "
            + "http://incoming.jazz-on-line.com/a/mp3w/1941_149.mp3");
    adapter = new MainArrayAdapter(MainActivity.this, array_list_music);
    setListAdapter(adapter);

    list.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            Object item = getListView().getItemAtPosition(arg2);
            String the_list_item = item.toString();

            Toast.makeText(MainActivity.this,
                    "You clicked " + the_list_item, Toast.LENGTH_LONG)
                    .show();

            String[] aux = the_list_item.split(" ### ");
            String url_to_play = aux[2];

            playAudio(url_to_play);

        }
    });

}

private void playAudio(String url_to_play) {

    //----- stop & reset 
    try {
        mediaPlayer.stop();
        mediaPlayer.reset();
    } catch (Exception e) {
        // TODO: handle exception
    }

    try {
        mediaPlayer.setDataSource(url_to_play);
        mediaPlayer.prepare();
        mediaPlayer.start();
    } catch (Exception e) {
        Log.e(TAG, "Could not open file " + url_to_play + " for playback.",
                e);
    }

}
@Override
public boolean onTouchEvent(MotionEvent event) {
    mediaController.show();
    return true;
}

@Override
protected void onStop() {
    super.onStop();
    mediaController.hide();
    mediaPlayer.stop();
    mediaPlayer.release();
}

@Override
public boolean canPause() {
    return true;
}

@Override
public boolean canSeekBackward() {

    return true;
}

@Override
public boolean canSeekForward() {
    return true;
}

@Override
public int getBufferPercentage() {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public int getCurrentPosition() {
    return mediaPlayer.getCurrentPosition();
}

@Override
public int getDuration() {
    return mediaPlayer.getDuration();
}

@Override
public boolean isPlaying() {
    return mediaPlayer.isPlaying();
}

@Override
public void pause() {
    mediaPlayer.pause();
}

@Override
public void seekTo(int arg0) {
    mediaPlayer.seekTo(arg0);
}

@Override
public void start() {
    mediaPlayer.start();

}
public void onPrepared(MediaPlayer mediaPlayer) {
    Log.d(TAG, "onPrepared");
    mediaController.setMediaPlayer(this);
    mediaController.setAnchorView(list);
    mediaController.show();

    handler.post(new Runnable() {
        public void run() {
            mediaController.setEnabled(true);
            mediaController.show();

        }
    });
   }
  }
4

0 回答 0