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;
}
}
So instead of calling MediaPlayer directly, MediaPlayerWrapper is used, which internally updates StateMachine and exposes getState() method.
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.
Just use pause to stop, followed by seekTo(0) before restarting: mediaPlayer. seekTo(0); mediaPlayer.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With