【问题标题】:setAudioStreamType is deprecated, how can I replace it?setAudioStreamType 已弃用,我该如何替换它?
【发布时间】:2019-06-24 22:57:16
【问题描述】:

我正在尝试使用 MediaPlayer 在 Android Studio 中制作一个广播流媒体应用,但是当我编译它时显示下一个错误:

使用或覆盖已弃用的 API。使用 -Xlint:deprecation 重新编译 了解详情。

我在Android文档中搜索,我应该将此方法替换为setAudioAttributes,我该如何更改它?

public class Radio extends Fragment {

    Button play_pause;
    MediaPlayer mp;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.radio, container, false);
        play_pause = (Button) view.findViewById(R.id.btnplay);
        try {
               mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
                mp.setDataSource("http://198.27.83.65:9962/;stream.mp3");
                mp.prepareAsync();
         }
         catch (Exception e){
             Toast.makeText(getContext(),"Error" + e,Toast.LENGTH_SHORT).show();
         }
         //mp = MediaPlayer.create(this.getContext(), R.raw.radio);
            play_pause.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                         if(mp.isPlaying()) {
                            mp.pause();
                            Toast.makeText(getContext(),"Stop",Toast.LENGTH_SHORT).show();
                        }
                        else {
                            mp.start();
                            Toast.makeText(getContext(),"Start",Toast.LENGTH_SHORT).show();
                        }
                }
            });
        return view;
    }
}

【问题讨论】:

    标签: android android-mediaplayer


    【解决方案1】:
    mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
    

    mp.setAudioAttributes(
                new AudioAttributes
                   .Builder()
                   .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
                   .build());
    

    setAudioStreamType 在 API 级别 26 中已弃用,您必须使用新方法 setAudioAttributes

    根据文件: You must call this method before prepare() or prepareAsync() in order for the audio attributes to become effective thereafter.

    【讨论】:

    • 在我的情况下,这个替换没有按预期工作。在强制通过耳机播放音频时,我需要不推荐使用的方法来更改音量。使用 AudioAttibutes.Builder()... 时,无法通过硬件按钮更改音量。
    • AudioManager.STREAM_VOICE_CALL 怎么样?可以换成 AudioAttributes.CONTENT_TYPE_SPEECH 吗?
    • 我们可以使用API 26下面的相同方法还是可以对所有版本使用相同的方法??
    • @VivekThummar 你不能使用 API 26 下的新方法。但这仍然是 2021 年的问题,因为谷歌要求“新应用和应用更新必须针对 Android 10(API 级别 29)或更高”
    【解决方案2】:

    使用 setAudioAttributes(AudioAttributes) 代替 setAudioStreamType()

    谷歌文档说:

    设置此 MediaPlayer 的音频流类型。有关流类型的列表,请参阅 AudioManager。必须在 prepare() 或 prepareAsync() 之前调用此方法,以便目标流类型在之后生效。

    【讨论】:

      【解决方案3】:

      setAudioStreamType 已被 setAudioAttributes 取代。 下面是相同的示例实现。更多理解可以参考以下文档:

      https://developer.android.com/guide/topics/media/mediaplayer.html#kotlin

      val mediaPlayer = MediaPlayer().apply {
      setAudioAttributes(
          AudioAttributes.Builder()
              .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
              .setUsage(AudioAttributes.USAGE_MEDIA)
              .build()
      )
      setDataSource(applicationContext, myUri)
      prepare()
      start()
      }
      

      【讨论】:

        猜你喜欢
        • 2021-11-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-12
        • 1970-01-01
        • 2017-02-13
        相关资源
        最近更新 更多