setAudioStreamType已过时,如何替换? [英] setAudioStreamType is deprecated, how can I replace it?

查看:108
本文介绍了setAudioStreamType已过时,如何替换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to make a radio streaming app in Android Studio using MediaPlayer, but when I compile it shows the next error:

使用或覆盖已弃用的API.用-Xlint:deprecation重新编译有关详细信息.

uses or overrides a deprecated API. Recompile with -Xlint:deprecation for details.

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

I searched in Android documentation and I should replace this method for setAudioAttributes, how can I change it?

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;
    }
}

推荐答案

mp.setAudioStreamType(AudioManager.STREAM_MUSIC);

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

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

setAudioStreamType was deprecated in API Level 26, you have to use new method setAudioAttributes

根据文档:您必须在prepare()或prepareAsync()之前调用此方法,以使音频属性此后生效.

这篇关于setAudioStreamType已过时,如何替换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆