setAudioStreamType不推荐使用的方法,我该如何替换呢? [英] setAudioStreamType deprecated method, how i can replace it?

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

问题描述

我正在尝试使用Mediaplayer在android studio中制作一个广播流应用程序,但是我在编译时显示了下一个错误:

I am trying make a radio streaming app in android studio using mediaplayer, but i when compile shows the next error:

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

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

我在android文档中进行搜索,我应该将该方法替换为setAudioAttributes,我该如何更改?我是使用Android Studio的新手.

I was search in android documentation and i should reemplace this method for setAudioAttributes, i how can change it? i am new using android studio.

谢谢.

公共类电台扩展了片段{

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不推荐使用的方法,我该如何替换呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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