setAudioStreamType is deprecated, how can I replace it?

Viewed 13541

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

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

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;
    }
}
3 Answers
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);

to

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

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

According to document: You must call this method before prepare() or prepareAsync() in order for the audio attributes to become effective thereafter.

Use setAudioAttributes(AudioAttributes) instead of setAudioStreamType()

The Google Documentation says:

Sets the audio stream type for this MediaPlayer. See AudioManager for a list of stream types. Must call this method before prepare() or prepareAsync() in order for the target stream type to become effective thereafter.

setAudioStreamType has been replaced by setAudioAttributes. Below is the sample implementation for the same. For more understanding you can refer to the following doc :

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()
}
Related