Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setAudioStreamType is deprecated, how can I replace it?

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;
    }
}
like image 523
Jeison Melo Avatar asked Jun 24 '19 22:06

Jeison Melo


People also ask

Which method is used to bring the media player to prepared state?

So instead of calling MediaPlayer directly, MediaPlayerWrapper is used, which internally updates StateMachine and exposes getState() method.

What is media player in Android Studio?

android.media.MediaPlayer. MediaPlayer class can be used to control playback of audio/video files and streams. MediaPlayer is not thread-safe. Creation of and all access to player instances should be on the same thread. If registering callbacks, the thread must have a Looper.

How do I reset my media player on Android?

Just use pause to stop, followed by seekTo(0) before restarting: mediaPlayer. seekTo(0); mediaPlayer.


Video Answer


2 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.

like image 78
fangzhzh Avatar answered Oct 12 '22 22:10

fangzhzh


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.

like image 1
Edgar Khimich Avatar answered Oct 12 '22 22:10

Edgar Khimich