Online radio streaming app for Android
So I found this sample and it works for me, here it is if you have the same issue:
in myMain.java
import android.app.Activity;
import android.os.Bundle;
import java.io.IOException;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
public class myMain extends Activity implements OnClickListener {
private ProgressBar playSeekBar;
private Button buttonPlay;
private Button buttonStopPlay;
private MediaPlayer player;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initializeUIElements();
initializeMediaPlayer();
}
private void initializeUIElements() {
playSeekBar = (ProgressBar) findViewById(R.id.progressBar1);
playSeekBar.setMax(100);
playSeekBar.setVisibility(View.INVISIBLE);
buttonPlay = (Button) findViewById(R.id.buttonPlay);
buttonPlay.setOnClickListener(this);
buttonStopPlay = (Button) findViewById(R.id.buttonStopPlay);
buttonStopPlay.setEnabled(false);
buttonStopPlay.setOnClickListener(this);
}
public void onClick(View v) {
if (v == buttonPlay) {
startPlaying();
} else if (v == buttonStopPlay) {
stopPlaying();
}
}
private void startPlaying() {
buttonStopPlay.setEnabled(true);
buttonPlay.setEnabled(false);
playSeekBar.setVisibility(View.VISIBLE);
player.prepareAsync();
player.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
player.start();
}
});
}
private void stopPlaying() {
if (player.isPlaying()) {
player.stop();
player.release();
initializeMediaPlayer();
}
buttonPlay.setEnabled(true);
buttonStopPlay.setEnabled(false);
playSeekBar.setVisibility(View.INVISIBLE);
}
private void initializeMediaPlayer() {
player = new MediaPlayer();
try {
player.setDataSource("http://usa8-vn.mixstream.net:8138");
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
player.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {
public void onBufferingUpdate(MediaPlayer mp, int percent) {
playSeekBar.setSecondaryProgress(percent);
Log.i("Buffering", "" + percent);
}
});
}
@Override
protected void onPause() {
super.onPause();
if (player.isPlaying()) {
player.stop();
}
}
}
in the XML (main.xml) code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="Source: (Radio La Chevere)"
android:layout_marginTop="10dip" android:gravity="center" />
<ProgressBar android:id="@+id/progressBar1"
android:indeterminateOnly="false" android:progressDrawable="@android:drawable/progress_horizontal"
android:indeterminateDrawable="@android:drawable/progress_indeterminate_horizontal"
android:minHeight="20dip" android:maxHeight="20dip"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_marginLeft="20dip" android:layout_marginRight="20dip"
android:layout_marginTop="10dip"></ProgressBar>
<LinearLayout android:id="@+id/linearLayout1"
android:layout_height="wrap_content" android:layout_width="match_parent"
android:layout_marginTop="20dip" android:gravity="center">
<Button android:text="Play" android:id="@+id/buttonPlay"
android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="Stop" android:id="@+id/buttonStopPlay"
android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
</LinearLayout>
and the android manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="package.your.RadioStream"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".myMain"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Android Studio Online Radio app(java) from API
Please see onPlayClick
which is called on the play button, right now I always change the URL to the radio station which is clicked on just to show you how can you change the URL for media to be played in mediaplayer.
YOu can yourself think of logic to show the `pause' button and updating them according to the song is played.
Right now this snippet is to show you how you can changes the ongoing radio to different when you clicked upon that.
getAdapterPosition()
: I am taking leverage of this to know which row's Play
button is clicked so that accordingly I can switch to that radio station.
mediaPlayer.prepareAsync(): so that I don't block the main thread.
setOnPreparedListener because we want to start
our mediaPlayer when it is done prepared and as we have done it asynchronously we have to listen for the callback to know when to start our media player.
setOnErrorListener to know if there is some error while playing some media, this was helpful as in your case out of 10 stations only 5 or 6 are working and others are throwing error and this callback actually helped me to know why exactly media was not played.
stopPlaying There is mediaPlayer.stop();
and mediaPlayer.reset();
this are to make sure that before new media data is set using mediaPlayer.setDataSource already playing media is stopped and the data is reset.
public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {
// creating a variable for array list and context.
private ArrayList<RadioStation> radioStationArrayList;
private MediaPlayer mediaPlayer;
// creating a constructor for our variables.
public Adapter(ArrayList<RadioStation> radioStationArrayList, Context context) {
this.radioStationArrayList = radioStationArrayList;
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
}
@NonNull
@Override
public Adapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
// below line is to inflate our layout.
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.course_rv_item, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull Adapter.ViewHolder holder, int position) {
// setting data to our views of recycler view.
RadioStation modal = radioStationArrayList.get(position);
holder.name.setText(modal.getName());
Picasso.get().load(modal.getFavicon()).into(holder.favicon);
}
@Override
public int getItemCount() {
// returning the size of array list.
return radioStationArrayList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
// creating variables for our views.
private TextView name;
private ImageView favicon;
private Button play;
public ViewHolder(@NonNull View itemView) {
super(itemView);
name = itemView.findViewById(R.id.idName);
favicon = itemView.findViewById(R.id.idImg);
play = itemView.findViewById(R.id.play);
play.setOnClickListener(view -> onPlayClick());
}
public void onPlayClick() {
stopPlaying();
startPlaying();
}
private void startPlaying() {
try {
mediaPlayer.setDataSource(radioStationArrayList.get(getAdapterPosition()).getUrl_resolved());
mediaPlayer.setOnPreparedListener(mp -> {
mediaPlayer.start();
Log.v("DINKAR", "Started");
});
mediaPlayer.setOnErrorListener((mp, what, extra) -> {
Log.e("DINKAR", "Error");
return false;
});
mediaPlayer.prepareAsync();
} catch (IOException e) {
Log.e("DINKAR", "failed");
}
}
private void stopPlaying() {
mediaPlayer.stop();
mediaPlayer.reset();
}
}
}
Note: many of the radio stations are having issues MediaPlayer were not able to play those.
I used https://textuploader.com/tsu8j to get your adapter class code and using your screenshot of the app created layout XML file on my own so that I can run the app to test it out before giving you any snippet
Online radio streaming app for Android: where to place Wakelock and Wifilock
These attributes go in the AndroidManifest.xml file. You may need the following for your app to work correctly:
<android:name="android.permission.ACCESS_WIFI_STATE" uses-permission />
<android:name="android.permission.CHANGE_WIFI_STATE" uses-permission />
<android:name="android.permission.CHANGE_NETWORK_STATE" uses-permission />
<android:name="android.permission.INTERNET" uses-permission />
<android:name="android.permission.ACCESS_NETWORK_STATE" uses-permission />
Code in java file for wifi lock.
// obtain wifi lock
wifiLock = ((WifiManager)
THIS_CONTEXT.getSystemService(THIS_CONTEXT.WIFI_SERVICE)).createWifiLock("clientWifiLock");
wifiLock.acquire();
Do similarly for wake lock.
Making an Android radio station streaming app
The question you asked leads to a broad answer. However here goes some tips for you.
For a streaming radio station you can create a steaming server or buy streaming hosting. There are some free software's available that can be installed in your server and use to produce audio streaming from tracks/audio input.
For free streaming software you can try,
http://www.shoutcast.com/BroadcastNow
http://icecast.org/
If you want to buy a streaming hosting service with pre-installed software like SHOUTcast try this,
http://www.shoutcheap.com/
Once you buy or host a radio station, you can connect to that station using a http://ip_address:port_number
using Android MediaPlayer
.
Details needed for making an online streaming android app for an internet radio
You should look at this answer There are some code for how to stream a radio link.
I think you want to know which information of your station you need, right?
If you check the answer, there's a streaming link of the internet radio station. Every station has a streaming link like this with a port number (the four number at the end of the link).
This is the basic and simplest internet radio streaming way.
Related Topics
Efficiency of Searching Using Wherearraycontains
Android Gives Error "Cannot Fit Requested Classes in a Single Dex File"
PDF Library to Rendering the PDF Files in Android
When Onbindviewholder Is Called and How It Works
Does Proguard Work to Obfuscate Static String Constants
Converting Epoch Time to Date String
How to Load a Jni .Dylib File with a Dependency Without Getting an Unsatifiedlinkerror
Why Would One Declare an Immutable Class Final in Java
Wrong Version of Keystore on Android Call
Polyline Is Not on the Roads: It Goes Straight from One Point to Other
Preload Multiple Images with Glide
Google Cloud Messaging - Messages Either Received Instantly or with Long Delay
Android Drawable Speech Bubble
Sqliteopenhelper Failing to Call Oncreate