Streaming Audio from A URL in Android using MediaPlayer?

simple Media Player with streaming example.For xml part you need one button with id button1 and two images in your drawable folder with name button_pause and button_play and please don’t forget to add the internet permission in your manifest. public class MainActivity extends Activity { private Button btn; /** * help to toggle between play … Read more

What is the difference between MediaPlayer and VideoView in Android

Was asking the same question and as I understood from what Mark (CommonsWare) advised on numerous threads here, VideoView is a wrapper (200 hundred lines of code) for MediaPlayer and SurfaceView to provide embedded controls. He also kindly shared some examples: https://github.com/commonsguy/cw-advandroid/blob/master/Media/Video/src/com/commonsware/android/video/VideoDemo.java https://github.com/commonsguy/vidtry/blob/master/src/com/commonsware/android/vidtry/Player.java and example from android sdk http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/media/MediaPlayerDemo.html Also some people had issues playing … Read more

Media Player called in state 0, error (-38,0)

You need to call mediaPlayer.start() in the onPrepared method by using a listener. You are getting this error because you are calling mediaPlayer.start() before it has reached the prepared state. Here is how you can do it : mp.setDataSource(url); mp.setOnPreparedListener(this); mp.prepareAsync(); public void onPrepared(MediaPlayer player) { player.start(); }

MediaSessionCompat:Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent

If you are NOT USING PendingIntent anywhere. The issue might be resolved by adding or updating this dependency // required to avoid crash on Android 12 API 31 implementation ‘androidx.work:work-runtime-ktx:2.7.0’ This fixed my problem.

Play audio file from the assets directory

player.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength()); Your version would work if you had only one file in the assets directory. The asset directory contents are not actually ‘real files’ on disk. All of them are put together one after another. So, if you do not specify where to start and how many bytes to read, the player will read up … Read more

Play sound on button click android

This is the most important part in the code provided in the original post. Button one = (Button) this.findViewById(R.id.button1); final MediaPlayer mp = MediaPlayer.create(this, R.raw.soho); one.setOnClickListener(new OnClickListener(){ public void onClick(View v) { mp.start(); } }); To explain it step by step: Button one = (Button) this.findViewById(R.id.button1); First is the initialization of the button to be … Read more

tech