如何在 Android 中播放音频时显示 MediaController?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3747139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:38:00  来源:igfitidea点击:

How can I show a MediaController while playing audio in Android?

android

提问by Android_programmer_camera

How can I show a MediaControllerwhile playing audio file? I am creating an instance of MediaControllerand calling its show()method, but it is not showing. Can any help me in sorting out this issue?

如何MediaController在播放音频文件时显示?我正在创建一个实例MediaController并调用它的show()方法,但它没有显示。任何人都可以帮助我解决这个问题吗?

回答by user229487

Here is an example of an Activity that uses MediaPlayer and MediaController to play audio.

下面是一个使用 MediaPlayer 和 MediaController 播放音频的 Activity 示例。

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;

import android.media.MediaPlayer.OnPreparedListener;
import android.view.MotionEvent;
import android.widget.MediaController;
import android.widget.TextView;

import java.io.IOException;

public class AudioPlayer extends Activity implements OnPreparedListener, MediaController.MediaPlayerControl{
  private static final String TAG = "AudioPlayer";

  public static final String AUDIO_FILE_NAME = "audioFileName";

  private MediaPlayer mediaPlayer;
  private MediaController mediaController;
  private String audioFile;

  private Handler handler = new Handler();

  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.audio_player);

    audioFile = this.getIntent().getStringExtra(AUDIO_FILE_NAME);
    ((TextView)findViewById(R.id.now_playing_text)).setText(audioFile);

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

    mediaController = new MediaController(this);

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

  }

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

  @Override
  public boolean onTouchEvent(MotionEvent event) {
    //the MediaController will hide after 3 seconds - tap the screen to make it appear again
    mediaController.show();
    return false;
  }

  //--MediaPlayerControl methods----------------------------------------------------
  public void start() {
    mediaPlayer.start();
  }

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

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

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

  public void seekTo(int i) {
    mediaPlayer.seekTo(i);
  }

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

  public int getBufferPercentage() {
    return 0;
  }

  public boolean canPause() {
    return true;
  }

  public boolean canSeekBackward() {
    return true;
  }

  public boolean canSeekForward() {
    return true;
  }
  //--------------------------------------------------------------------------------

  public void onPrepared(MediaPlayer mediaPlayer) {
    Log.d(TAG, "onPrepared");
    mediaController.setMediaPlayer(this);
    mediaController.setAnchorView(findViewById(R.id.main_audio_view));

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

Here is a layout that can be used with the code above:

这是一个可以与上面的代码一起使用的布局:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/main_audio_view"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:layout_gravity="center"
              android:orientation="vertical">
  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="Now playing:"
    android:textSize="25sp"
    android:textStyle="bold"
    />
  <TextView
    android:id="@+id/now_playing_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dip"
    android:layout_marginLeft="10dip"
    android:layout_marginRight="10dip"
    android:layout_gravity="center"
    android:text="Now playing.."
    android:textSize="16sp"
    android:textStyle="italic"
    />
</LinearLayout>

回答by jp36

I still don't have enough rep to Comment so i have to just post this as an answer.

我仍然没有足够的代表来发表评论,所以我必须将此作为答案发布。

@user229487's answer

@ user229487 的回答

you need to add mediaController.hide();to the onStop()method. If you don't do this and you happen to rotate the window or close the window while it is showing you will get a Activity x has leaked windowerror

您需要添加mediaController.hide();onStop()方法中。如果您不这样做并且您碰巧在显示时旋转窗口或关闭窗口,则会出现Activity x has leaked window错误

New method should look like this

新方法应该是这样的

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

回答by Kevin

I copied the code above from user229487 verbatim but couldn't get it to work. Given I'm new to Android dev, I found out the hard way that you need to put an intent as well as a few other things. I also don't exactly understand how the intent will work with a user interface (again I'm new), but will work on it.

我从 user229487 逐字复制了上面的代码,但无法让它工作。鉴于我是 Android 开发人员的新手,我发现您需要将意图和其他一些东西放在一起很难。我也不完全理解意图将如何与用户界面一起工作(我还是新手),但会处理它。

Few things for other noobs like me to get the code above to work (I at least got it to not crash now and play a song :) ):

对于像我这样的其他菜鸟来说,很少有事情可以让上面的代码工作(我至少让它现在不会崩溃并播放一首歌:)):

  • Won't compile without this:

    @Override
    public int getAudioSessionId() {
    return 0;
    }

  • To set audio manually: (change line to:)

    public static final String AUDIO_FILE_NAME = Environment.getExternalStorageDirectory()+"/MusicFolder/Song.mp3";

  • Setup intent
    Add this line: this.getIntent().putExtra(AUDIO_FILE_NAME,AUDIO_FILE_NAME);
    Before this line: audioFile = this.getIntent().getStringExtra(AUDIO_FILE_NAME);

  • 没有这个就不会编译:

    @Override
    public int getAudioSessionId() {
    return 0;
    }

  • 手动设置音频:(将行更改为:)

    public static final String AUDIO_FILE_NAME = Environment.getExternalStorageDirectory()+"/MusicFolder/Song.mp3";

  • 设置意图
    添加此行: this.getIntent().putExtra(AUDIO_FILE_NAME,AUDIO_FILE_NAME);
    在此行之前: audioFile = this.getIntent().getStringExtra(AUDIO_FILE_NAME);

Should compile and play song now.

现在应该编译和播放歌曲。

回答by Wil-It

I am not yet allowed to comment ... therefore I write here. The above code is excellent. I only needed to take out the

我还不能发表评论……所以我写在这里。上面的代码很棒。我只需要取出

mediaPlayer.prepare();

mediaPlayer.prepare();

bit in the try catch, otherwise the activity would crash. Not sure what this bit should be doing, it is working very well without it. Thanks!!

在 try catch 中位,否则活动将崩溃。不确定这个位应该做什么,没有它它工作得很好。谢谢!!