├── app
├── .gitignore
├── src
│ ├── main
│ │ ├── res
│ │ │ ├── values
│ │ │ │ ├── dimens.xml
│ │ │ │ ├── colors.xml
│ │ │ │ ├── strings.xml
│ │ │ │ └── styles.xml
│ │ │ ├── mipmap-hdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-mdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-xhdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-xxhdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-xxxhdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── anim
│ │ │ │ ├── bottom_in.xml
│ │ │ │ └── bottom_out.xml
│ │ │ ├── drawable
│ │ │ │ ├── ic_play_white.xml
│ │ │ │ ├── ic_stop_white.xml
│ │ │ │ ├── ic_play_arrow_black.xml
│ │ │ │ ├── ic_pause_white.xml
│ │ │ │ └── ic_pause_black.xml
│ │ │ ├── layout
│ │ │ │ ├── list_item.xml
│ │ │ │ ├── activity_main.xml
│ │ │ │ └── content_main.xml
│ │ │ ├── menu
│ │ │ │ └── menu_main.xml
│ │ │ └── raw
│ │ │ │ └── shoutcasts.json
│ │ ├── java
│ │ │ └── com
│ │ │ │ └── mcakir
│ │ │ │ └── radio
│ │ │ │ ├── player
│ │ │ │ ├── PlaybackStatus.java
│ │ │ │ ├── RadioManager.java
│ │ │ │ ├── MediaNotificationManager.java
│ │ │ │ └── RadioService.java
│ │ │ │ ├── util
│ │ │ │ ├── Shoutcast.java
│ │ │ │ ├── ShoutcastHelper.java
│ │ │ │ └── ShoutcastListAdapter.java
│ │ │ │ └── MainActivity.java
│ │ └── AndroidManifest.xml
│ ├── test
│ │ └── java
│ │ │ └── com
│ │ │ └── mcakir
│ │ │ └── radio
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── mcakir
│ │ └── radio
│ │ └── ExampleInstrumentedTest.java
├── proguard-rules.pro
└── build.gradle
├── settings.gradle
├── screenshot
├── home.png
├── lockscreen.png
└── notification.png
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── .gitignore
├── README.md
├── gradle.properties
├── gradlew.bat
└── gradlew
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app'
2 |
--------------------------------------------------------------------------------
/screenshot/home.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/m-cakir/radio-player/HEAD/screenshot/home.png
--------------------------------------------------------------------------------
/screenshot/lockscreen.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/m-cakir/radio-player/HEAD/screenshot/lockscreen.png
--------------------------------------------------------------------------------
/app/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 | 16dp
3 |
4 |
--------------------------------------------------------------------------------
/screenshot/notification.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/m-cakir/radio-player/HEAD/screenshot/notification.png
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/m-cakir/radio-player/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea
5 | .DS_Store
6 | /build
7 | /captures
8 | .externalNativeBuild
9 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/m-cakir/radio-player/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/m-cakir/radio-player/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/m-cakir/radio-player/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/m-cakir/radio-player/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/m-cakir/radio-player/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/anim/bottom_in.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
8 |
9 |
--------------------------------------------------------------------------------
/app/src/main/res/anim/bottom_out.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
8 |
9 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Tue Jul 10 12:36:06 EET 2018
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | RadioPlayer
3 | Settings
4 |
5 | Radio station couldn\'t stream!
6 | On Air
7 |
8 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_play_white.xml:
--------------------------------------------------------------------------------
1 |
6 |
9 |
10 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_stop_white.xml:
--------------------------------------------------------------------------------
1 |
6 |
9 |
10 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_play_arrow_black.xml:
--------------------------------------------------------------------------------
1 |
6 |
9 |
10 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_pause_white.xml:
--------------------------------------------------------------------------------
1 |
6 |
9 |
10 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/list_item.xml:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_pause_black.xml:
--------------------------------------------------------------------------------
1 |
6 |
9 |
10 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Radio Player
2 | Radio station streaming example with ExoPlayer.
3 |
4 | # Screenshots
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/app/src/main/res/menu/menu_main.xml:
--------------------------------------------------------------------------------
1 |
11 |
--------------------------------------------------------------------------------
/app/src/test/java/com/mcakir/radio/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.mcakir.radio;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() throws Exception {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/mcakir/radio/player/PlaybackStatus.java:
--------------------------------------------------------------------------------
1 | package com.mcakir.radio.player;
2 |
3 | public class PlaybackStatus {
4 |
5 | public static final String IDLE = "PlaybackStatus_IDLE";
6 |
7 | public static final String LOADING = "PlaybackStatus_LOADING";
8 |
9 | public static final String PLAYING = "PlaybackStatus_PLAYING";
10 |
11 | public static final String PAUSED = "PlaybackStatus_PAUSED";
12 |
13 | public static final String STOPPED = "PlaybackStatus_STOPPED";
14 |
15 | public static final String ERROR = "PlaybackStatus_ERROR";
16 |
17 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/mcakir/radio/util/Shoutcast.java:
--------------------------------------------------------------------------------
1 | package com.mcakir.radio.util;
2 |
3 | import com.google.gson.annotations.SerializedName;
4 |
5 | public class Shoutcast {
6 |
7 | private String name;
8 |
9 | @SerializedName("stream")
10 | private String url;
11 |
12 | public String getName() {
13 | return name;
14 | }
15 |
16 | public void setName(String name) {
17 | this.name = name;
18 | }
19 |
20 | public String getUrl() {
21 | return url;
22 | }
23 |
24 | public void setUrl(String url) {
25 | this.url = url;
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/app/src/main/java/com/mcakir/radio/util/ShoutcastHelper.java:
--------------------------------------------------------------------------------
1 | package com.mcakir.radio.util;
2 |
3 | import android.content.Context;
4 |
5 | import com.google.gson.Gson;
6 | import com.google.gson.reflect.TypeToken;
7 | import com.mcakir.radio.R;
8 |
9 | import java.io.InputStreamReader;
10 | import java.io.Reader;
11 | import java.util.ArrayList;
12 | import java.util.List;
13 |
14 | public class ShoutcastHelper {
15 |
16 | public static List retrieveShoutcasts(Context context){
17 |
18 | Reader reader = new InputStreamReader(context.getResources().openRawResource(R.raw.shoutcasts));
19 |
20 | return (new Gson()).fromJson(reader, new TypeToken>() {}.getType());
21 | }
22 |
23 | }
24 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | org.gradle.jvmargs=-Xmx1536m
13 |
14 | # When configured, Gradle will run in incubating parallel mode.
15 | # This option should only be used with decoupled projects. More details, visit
16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
17 | # org.gradle.parallel=true
18 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/mcakir/radio/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.mcakir.radio;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumentation test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() throws Exception {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("com.mcakir.radio", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in C:\Users\mcakir\AppData\Local\Android\Sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
19 | # Uncomment this to preserve the line number information for
20 | # debugging stack traces.
21 | #-keepattributes SourceFile,LineNumberTable
22 |
23 | # If you keep the line number information, uncomment this to
24 | # hide the original source file name.
25 | #-renamesourcefileattribute SourceFile
26 |
--------------------------------------------------------------------------------
/app/src/main/res/raw/shoutcasts.json:
--------------------------------------------------------------------------------
1 | [
2 | {
3 | "name" : "BBC - Radio 1",
4 | "stream" : "http://bbcmedia.ic.llnwd.net/stream/bbcmedia_radio1_mf_p"
5 | },
6 | {
7 | "name": "BBC - Radio 2",
8 | "stream": "http://bbcmedia.ic.llnwd.net/stream/bbcmedia_radio2_mf_p"
9 | },
10 | {
11 | "name": "BBC - Radio 3",
12 | "stream": "http://bbcmedia.ic.llnwd.net/stream/bbcmedia_radio3_mf_p"
13 | },
14 | {
15 | "name": "BBC - Radio 4",
16 | "stream": "http://bbcmedia.ic.llnwd.net/stream/bbcmedia_radio4fm_mf_p"
17 | },
18 | {
19 | "name": "BBC - Radio 5 live",
20 | "stream": "http://bbcmedia.ic.llnwd.net/stream/bbcmedia_radio5live_mf_p"
21 | },
22 | {
23 | "name": "BBC - Radio 6",
24 | "stream": "http://bbcmedia.ic.llnwd.net/stream/bbcmedia_6music_mf_p"
25 | },
26 | {
27 | "name": "BBC Radio Asian Network",
28 | "stream": "http://bbcmedia.ic.llnwd.net/stream/bbcmedia_asianet_mf_p"
29 | },
30 | {
31 | "name" : "BBC World Service",
32 | "stream" : "http://bbcwssc.ic.llnwd.net/stream/bbcwssc_mp1_ws-eieuk"
33 | }
34 | ]
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
13 |
14 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 27
5 | defaultConfig {
6 | applicationId "com.mcakir.radio"
7 | minSdkVersion 16
8 | targetSdkVersion 27
9 | versionCode 1
10 | versionName "1.0"
11 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
12 | }
13 | buildTypes {
14 | release {
15 | minifyEnabled false
16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
17 | }
18 | }
19 | }
20 |
21 | dependencies {
22 | implementation fileTree(dir: 'libs', include: ['*.jar'])
23 | androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
24 | exclude group: 'com.android.support', module: 'support-annotations'
25 | })
26 | implementation 'com.android.support:appcompat-v7:27.0.2'
27 | implementation 'com.android.support.constraint:constraint-layout:1.0.2'
28 | implementation 'com.android.support:design:27.0.2'
29 | implementation 'com.google.code.gson:gson:2.2.4'
30 | implementation 'org.greenrobot:eventbus:3.0.0'
31 | implementation 'com.jakewharton:butterknife:8.4.0'
32 | annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'
33 | testImplementation 'junit:junit:4.12'
34 |
35 | // full exoplayer library
36 | implementation 'com.google.android.exoplayer:exoplayer:2.8.2'
37 |
38 | // library modules
39 | // implementation 'com.google.android.exoplayer:exoplayer-core:2.X.X'
40 | // implementation 'com.google.android.exoplayer:exoplayer-dash:2.X.X'
41 | // implementation 'com.google.android.exoplayer:exoplayer-hls:2.X.X'
42 | // implementation 'com.google.android.exoplayer:exoplayer-smoothstreaming:2.X.X'
43 | // implementation 'com.google.android.exoplayer:exoplayer-ui:2.X.X'
44 | }
45 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
17 |
18 |
19 |
20 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
--------------------------------------------------------------------------------
/app/src/main/java/com/mcakir/radio/player/RadioManager.java:
--------------------------------------------------------------------------------
1 | package com.mcakir.radio.player;
2 |
3 | import android.content.ComponentName;
4 | import android.content.Context;
5 | import android.content.Intent;
6 | import android.content.ServiceConnection;
7 | import android.os.IBinder;
8 |
9 | import org.greenrobot.eventbus.EventBus;
10 |
11 | public class RadioManager {
12 |
13 | private static RadioManager instance = null;
14 |
15 | private static RadioService service;
16 |
17 | private Context context;
18 |
19 | private boolean serviceBound;
20 |
21 | private RadioManager(Context context) {
22 | this.context = context;
23 | serviceBound = false;
24 | }
25 |
26 | public static RadioManager with(Context context) {
27 |
28 | if (instance == null)
29 | instance = new RadioManager(context);
30 |
31 | return instance;
32 | }
33 |
34 | public static RadioService getService(){
35 | return service;
36 | }
37 |
38 | public void playOrPause(String streamUrl){
39 |
40 | service.playOrPause(streamUrl);
41 | }
42 |
43 | public boolean isPlaying() {
44 |
45 | return service.isPlaying();
46 | }
47 |
48 | public void bind() {
49 |
50 | Intent intent = new Intent(context, RadioService.class);
51 | context.bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
52 |
53 | if(service != null)
54 | EventBus.getDefault().post(service.getStatus());
55 | }
56 |
57 | public void unbind() {
58 |
59 | context.unbindService(serviceConnection);
60 | }
61 |
62 | private ServiceConnection serviceConnection = new ServiceConnection() {
63 |
64 | @Override
65 | public void onServiceConnected(ComponentName arg0, IBinder binder) {
66 |
67 | service = ((RadioService.LocalBinder) binder).getService();
68 | serviceBound = true;
69 | }
70 |
71 | @Override
72 | public void onServiceDisconnected(ComponentName arg0) {
73 |
74 | serviceBound = false;
75 | }
76 | };
77 |
78 | }
79 |
--------------------------------------------------------------------------------
/app/src/main/java/com/mcakir/radio/util/ShoutcastListAdapter.java:
--------------------------------------------------------------------------------
1 | package com.mcakir.radio.util;
2 |
3 | import android.app.Activity;
4 | import android.view.LayoutInflater;
5 | import android.view.View;
6 | import android.view.ViewGroup;
7 | import android.widget.BaseAdapter;
8 | import android.widget.TextView;
9 |
10 | import com.mcakir.radio.R;
11 |
12 | import java.util.ArrayList;
13 | import java.util.List;
14 |
15 | import butterknife.BindView;
16 | import butterknife.ButterKnife;
17 |
18 | public class ShoutcastListAdapter extends BaseAdapter {
19 |
20 | private Activity activity;
21 |
22 | private List shoutcasts = new ArrayList<>();
23 |
24 | public ShoutcastListAdapter(Activity activity, List shoutcasts){
25 | this.activity = activity;
26 | this.shoutcasts = shoutcasts;
27 | }
28 |
29 | @Override
30 | public int getCount() {
31 | return shoutcasts.size();
32 | }
33 |
34 | @Override
35 | public Object getItem(int position) {
36 | return shoutcasts.get(position);
37 | }
38 |
39 | @Override
40 | public long getItemId(int position) {
41 | return position;
42 | }
43 |
44 | @Override
45 | public View getView(int position, View view, ViewGroup parent) {
46 |
47 | LayoutInflater inflater = activity.getLayoutInflater();
48 |
49 | ViewHolder holder;
50 |
51 | if (view != null) {
52 |
53 | holder = (ViewHolder) view.getTag();
54 |
55 | } else {
56 |
57 | view = inflater.inflate(R.layout.list_item, parent, false);
58 |
59 | holder = new ViewHolder(view);
60 |
61 | view.setTag(holder);
62 |
63 | }
64 |
65 | Shoutcast shoutcast = (Shoutcast) getItem(position);
66 | if(shoutcast == null){
67 |
68 | return view;
69 |
70 | }
71 |
72 | holder.text.setText(shoutcast.getName());
73 |
74 | return view;
75 | }
76 |
77 | static class ViewHolder {
78 |
79 | @BindView(R.id.text)
80 | TextView text;
81 |
82 | public ViewHolder(View view) {
83 |
84 | ButterKnife.bind(this, view);
85 |
86 | }
87 | }
88 | }
89 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/content_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
10 |
11 |
15 |
16 |
21 |
22 |
29 |
30 |
39 |
40 |
49 |
50 |
51 |
52 |
53 |
54 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/app/src/main/java/com/mcakir/radio/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.mcakir.radio;
2 |
3 | import android.os.Bundle;
4 | import android.support.v7.app.AppCompatActivity;
5 | import android.support.v7.widget.Toolbar;
6 | import android.text.TextUtils;
7 | import android.view.View;
8 | import android.widget.AdapterView;
9 | import android.widget.ImageButton;
10 | import android.widget.ListView;
11 | import android.widget.TextView;
12 | import android.widget.Toast;
13 |
14 | import com.mcakir.radio.player.PlaybackStatus;
15 | import com.mcakir.radio.player.RadioManager;
16 | import com.mcakir.radio.util.Shoutcast;
17 | import com.mcakir.radio.util.ShoutcastHelper;
18 | import com.mcakir.radio.util.ShoutcastListAdapter;
19 |
20 | import org.greenrobot.eventbus.EventBus;
21 | import org.greenrobot.eventbus.Subscribe;
22 |
23 | import butterknife.BindView;
24 | import butterknife.ButterKnife;
25 | import butterknife.OnClick;
26 | import butterknife.OnItemClick;
27 |
28 | public class MainActivity extends AppCompatActivity {
29 |
30 | @BindView(R.id.toolbar)
31 | Toolbar toolbar;
32 |
33 | @BindView(R.id.playTrigger)
34 | ImageButton trigger;
35 |
36 | @BindView(R.id.listview)
37 | ListView listView;
38 |
39 | @BindView(R.id.name)
40 | TextView textView;
41 |
42 | @BindView(R.id.sub_player)
43 | View subPlayer;
44 |
45 | RadioManager radioManager;
46 |
47 | String streamURL;
48 |
49 | @Override
50 | protected void onCreate(Bundle savedInstanceState) {
51 |
52 | super.onCreate(savedInstanceState);
53 |
54 | setContentView(R.layout.activity_main);
55 |
56 | ButterKnife.bind(this);
57 |
58 | setSupportActionBar(toolbar);
59 |
60 | radioManager = RadioManager.with(this);
61 |
62 | listView.setAdapter(new ShoutcastListAdapter(this, ShoutcastHelper.retrieveShoutcasts(this)));
63 | }
64 |
65 | @Override
66 | public void onStart() {
67 |
68 | super.onStart();
69 |
70 | EventBus.getDefault().register(this);
71 | }
72 |
73 | @Override
74 | public void onStop() {
75 |
76 | EventBus.getDefault().unregister(this);
77 |
78 | super.onStop();
79 | }
80 |
81 | @Override
82 | protected void onDestroy() {
83 |
84 | radioManager.unbind();
85 |
86 | super.onDestroy();
87 | }
88 |
89 | @Override
90 | protected void onResume() {
91 | super.onResume();
92 |
93 | radioManager.bind();
94 | }
95 |
96 | @Override
97 | public void onBackPressed() {
98 |
99 | finish();
100 | }
101 |
102 | @Subscribe
103 | public void onEvent(String status){
104 |
105 | switch (status){
106 |
107 | case PlaybackStatus.LOADING:
108 |
109 | // loading
110 |
111 | break;
112 |
113 | case PlaybackStatus.ERROR:
114 |
115 | Toast.makeText(this, R.string.no_stream, Toast.LENGTH_SHORT).show();
116 |
117 | break;
118 |
119 | }
120 |
121 | trigger.setImageResource(status.equals(PlaybackStatus.PLAYING)
122 | ? R.drawable.ic_pause_black
123 | : R.drawable.ic_play_arrow_black);
124 |
125 | }
126 |
127 | @OnClick(R.id.playTrigger)
128 | public void onClicked(){
129 |
130 | if(TextUtils.isEmpty(streamURL)) return;
131 |
132 | radioManager.playOrPause(streamURL);
133 | }
134 |
135 | @OnItemClick(R.id.listview)
136 | public void onItemClick(AdapterView> parent, View view, int position, long id){
137 |
138 | Shoutcast shoutcast = (Shoutcast) parent.getItemAtPosition(position);
139 | if(shoutcast == null){
140 |
141 | return;
142 |
143 | }
144 |
145 | textView.setText(shoutcast.getName());
146 |
147 | subPlayer.setVisibility(View.VISIBLE);
148 |
149 | streamURL = shoutcast.getUrl();
150 |
151 | radioManager.playOrPause(streamURL);
152 | }
153 | }
154 |
--------------------------------------------------------------------------------
/app/src/main/java/com/mcakir/radio/player/MediaNotificationManager.java:
--------------------------------------------------------------------------------
1 | package com.mcakir.radio.player;
2 |
3 | import android.app.Notification;
4 | import android.app.NotificationChannel;
5 | import android.app.NotificationManager;
6 | import android.app.PendingIntent;
7 | import android.content.Context;
8 | import android.content.Intent;
9 | import android.content.res.Resources;
10 | import android.graphics.Bitmap;
11 | import android.graphics.BitmapFactory;
12 | import android.os.Build;
13 | import android.support.v4.app.NotificationCompat;
14 | import android.support.v4.app.NotificationManagerCompat;
15 |
16 | import com.mcakir.radio.MainActivity;
17 | import com.mcakir.radio.R;
18 |
19 | public class MediaNotificationManager {
20 |
21 | public static final int NOTIFICATION_ID = 555;
22 | private final String PRIMARY_CHANNEL = "PRIMARY_CHANNEL_ID";
23 | private final String PRIMARY_CHANNEL_NAME = "PRIMARY";
24 |
25 | private RadioService service;
26 |
27 | private String strAppName, strLiveBroadcast;
28 |
29 | private Resources resources;
30 |
31 | private NotificationManagerCompat notificationManager;
32 |
33 | public MediaNotificationManager(RadioService service) {
34 |
35 | this.service = service;
36 | this.resources = service.getResources();
37 |
38 | strAppName = resources.getString(R.string.app_name);
39 | strLiveBroadcast = resources.getString(R.string.live_broadcast);
40 |
41 | notificationManager = NotificationManagerCompat.from(service);
42 | }
43 |
44 | public void startNotify(String playbackStatus) {
45 |
46 | Bitmap largeIcon = BitmapFactory.decodeResource(resources, R.mipmap.ic_launcher);
47 |
48 | int icon = R.drawable.ic_pause_white;
49 | Intent playbackAction = new Intent(service, RadioService.class);
50 | playbackAction.setAction(RadioService.ACTION_PAUSE);
51 | PendingIntent action = PendingIntent.getService(service, 1, playbackAction, 0);
52 |
53 | if(playbackStatus.equals(PlaybackStatus.PAUSED)){
54 |
55 | icon = R.drawable.ic_play_white;
56 | playbackAction.setAction(RadioService.ACTION_PLAY);
57 | action = PendingIntent.getService(service, 2, playbackAction, 0);
58 |
59 | }
60 |
61 | Intent stopIntent = new Intent(service, RadioService.class);
62 | stopIntent.setAction(RadioService.ACTION_STOP);
63 | PendingIntent stopAction = PendingIntent.getService(service, 3, stopIntent, 0);
64 |
65 | Intent intent = new Intent(service, MainActivity.class);
66 | intent.setAction(Intent.ACTION_MAIN);
67 | intent.addCategory(Intent.CATEGORY_LAUNCHER);
68 | PendingIntent pendingIntent = PendingIntent.getActivity(service, 0, intent, 0);
69 |
70 | notificationManager.cancel(NOTIFICATION_ID);
71 |
72 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
73 | NotificationManager manager = (NotificationManager) service.getSystemService(Context.NOTIFICATION_SERVICE);
74 | NotificationChannel channel = new NotificationChannel(PRIMARY_CHANNEL, PRIMARY_CHANNEL_NAME, NotificationManager.IMPORTANCE_LOW);
75 | channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
76 | manager.createNotificationChannel(channel);
77 | }
78 |
79 | NotificationCompat.Builder builder = new NotificationCompat.Builder(service, PRIMARY_CHANNEL)
80 | .setAutoCancel(false)
81 | .setContentTitle(strLiveBroadcast)
82 | .setContentText(strAppName)
83 | .setLargeIcon(largeIcon)
84 | .setContentIntent(pendingIntent)
85 | .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
86 | .setSmallIcon(android.R.drawable.stat_sys_headset)
87 | .addAction(icon, "pause", action)
88 | .addAction(R.drawable.ic_stop_white, "stop", stopAction)
89 | .setPriority(NotificationCompat.PRIORITY_HIGH)
90 | .setWhen(System.currentTimeMillis())
91 | .setStyle(new android.support.v4.media.app.NotificationCompat.MediaStyle()
92 | .setMediaSession(service.getMediaSession().getSessionToken())
93 | .setShowActionsInCompactView(0, 1)
94 | .setShowCancelButton(true)
95 | .setCancelButtonIntent(stopAction));
96 |
97 | service.startForeground(NOTIFICATION_ID, builder.build());
98 | }
99 |
100 | public void cancelNotify() {
101 |
102 | service.stopForeground(true);
103 | }
104 |
105 | }
106 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # Attempt to set APP_HOME
46 | # Resolve links: $0 may be a link
47 | PRG="$0"
48 | # Need this for relative symlinks.
49 | while [ -h "$PRG" ] ; do
50 | ls=`ls -ld "$PRG"`
51 | link=`expr "$ls" : '.*-> \(.*\)$'`
52 | if expr "$link" : '/.*' > /dev/null; then
53 | PRG="$link"
54 | else
55 | PRG=`dirname "$PRG"`"/$link"
56 | fi
57 | done
58 | SAVED="`pwd`"
59 | cd "`dirname \"$PRG\"`/" >/dev/null
60 | APP_HOME="`pwd -P`"
61 | cd "$SAVED" >/dev/null
62 |
63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
64 |
65 | # Determine the Java command to use to start the JVM.
66 | if [ -n "$JAVA_HOME" ] ; then
67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
68 | # IBM's JDK on AIX uses strange locations for the executables
69 | JAVACMD="$JAVA_HOME/jre/sh/java"
70 | else
71 | JAVACMD="$JAVA_HOME/bin/java"
72 | fi
73 | if [ ! -x "$JAVACMD" ] ; then
74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
75 |
76 | Please set the JAVA_HOME variable in your environment to match the
77 | location of your Java installation."
78 | fi
79 | else
80 | JAVACMD="java"
81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
82 |
83 | Please set the JAVA_HOME variable in your environment to match the
84 | location of your Java installation."
85 | fi
86 |
87 | # Increase the maximum file descriptors if we can.
88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
89 | MAX_FD_LIMIT=`ulimit -H -n`
90 | if [ $? -eq 0 ] ; then
91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
92 | MAX_FD="$MAX_FD_LIMIT"
93 | fi
94 | ulimit -n $MAX_FD
95 | if [ $? -ne 0 ] ; then
96 | warn "Could not set maximum file descriptor limit: $MAX_FD"
97 | fi
98 | else
99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
100 | fi
101 | fi
102 |
103 | # For Darwin, add options to specify how the application appears in the dock
104 | if $darwin; then
105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
106 | fi
107 |
108 | # For Cygwin, switch paths to Windows format before running java
109 | if $cygwin ; then
110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112 | JAVACMD=`cygpath --unix "$JAVACMD"`
113 |
114 | # We build the pattern for arguments to be converted via cygpath
115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
116 | SEP=""
117 | for dir in $ROOTDIRSRAW ; do
118 | ROOTDIRS="$ROOTDIRS$SEP$dir"
119 | SEP="|"
120 | done
121 | OURCYGPATTERN="(^($ROOTDIRS))"
122 | # Add a user-defined pattern to the cygpath arguments
123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
125 | fi
126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
127 | i=0
128 | for arg in "$@" ; do
129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
131 |
132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
134 | else
135 | eval `echo args$i`="\"$arg\""
136 | fi
137 | i=$((i+1))
138 | done
139 | case $i in
140 | (0) set -- ;;
141 | (1) set -- "$args0" ;;
142 | (2) set -- "$args0" "$args1" ;;
143 | (3) set -- "$args0" "$args1" "$args2" ;;
144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
150 | esac
151 | fi
152 |
153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154 | function splitJvmOpts() {
155 | JVM_OPTS=("$@")
156 | }
157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
159 |
160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
161 |
--------------------------------------------------------------------------------
/app/src/main/java/com/mcakir/radio/player/RadioService.java:
--------------------------------------------------------------------------------
1 | package com.mcakir.radio.player;
2 |
3 | import android.app.Service;
4 | import android.content.BroadcastReceiver;
5 | import android.content.Context;
6 | import android.content.Intent;
7 | import android.content.IntentFilter;
8 | import android.media.AudioManager;
9 | import android.net.Uri;
10 | import android.net.wifi.WifiManager;
11 | import android.os.Binder;
12 | import android.os.Handler;
13 | import android.os.IBinder;
14 | import android.support.annotation.Nullable;
15 | import android.support.v4.media.MediaMetadataCompat;
16 | import android.support.v4.media.session.MediaControllerCompat;
17 | import android.support.v4.media.session.MediaSessionCompat;
18 | import android.telephony.PhoneStateListener;
19 | import android.telephony.TelephonyManager;
20 | import android.text.TextUtils;
21 |
22 | import com.google.android.exoplayer2.ExoPlaybackException;
23 | import com.google.android.exoplayer2.ExoPlayerFactory;
24 | import com.google.android.exoplayer2.PlaybackParameters;
25 | import com.google.android.exoplayer2.Player;
26 | import com.google.android.exoplayer2.SimpleExoPlayer;
27 | import com.google.android.exoplayer2.Timeline;
28 | import com.google.android.exoplayer2.extractor.DefaultExtractorsFactory;
29 | import com.google.android.exoplayer2.source.ExtractorMediaSource;
30 | import com.google.android.exoplayer2.source.TrackGroupArray;
31 | import com.google.android.exoplayer2.trackselection.AdaptiveTrackSelection;
32 | import com.google.android.exoplayer2.trackselection.DefaultTrackSelector;
33 | import com.google.android.exoplayer2.trackselection.TrackSelectionArray;
34 | import com.google.android.exoplayer2.upstream.DefaultBandwidthMeter;
35 | import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
36 | import com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory;
37 | import com.google.android.exoplayer2.util.Util;
38 | import com.mcakir.radio.R;
39 |
40 | import org.greenrobot.eventbus.EventBus;
41 |
42 | public class RadioService extends Service implements Player.EventListener, AudioManager.OnAudioFocusChangeListener {
43 |
44 | public static final String ACTION_PLAY = "com.mcakir.radio.player.ACTION_PLAY";
45 | public static final String ACTION_PAUSE = "com.mcakir.radio.player.ACTION_PAUSE";
46 | public static final String ACTION_STOP = "com.mcakir.radio.player.ACTION_STOP";
47 |
48 | private final IBinder iBinder = new LocalBinder();
49 |
50 | private Handler handler;
51 | private final DefaultBandwidthMeter BANDWIDTH_METER = new DefaultBandwidthMeter();
52 | private SimpleExoPlayer exoPlayer;
53 | private MediaSessionCompat mediaSession;
54 | private MediaControllerCompat.TransportControls transportControls;
55 |
56 | private boolean onGoingCall = false;
57 | private TelephonyManager telephonyManager;
58 |
59 | private WifiManager.WifiLock wifiLock;
60 |
61 | private AudioManager audioManager;
62 |
63 | private MediaNotificationManager notificationManager;
64 |
65 | private String status;
66 |
67 | private String strAppName;
68 | private String strLiveBroadcast;
69 | private String streamUrl;
70 |
71 | public class LocalBinder extends Binder {
72 | public RadioService getService() {
73 | return RadioService.this;
74 | }
75 | }
76 |
77 | private BroadcastReceiver becomingNoisyReceiver = new BroadcastReceiver() {
78 |
79 | @Override
80 | public void onReceive(Context context, Intent intent) {
81 |
82 | pause();
83 | }
84 | };
85 |
86 | private PhoneStateListener phoneStateListener = new PhoneStateListener() {
87 |
88 | @Override
89 | public void onCallStateChanged(int state, String incomingNumber) {
90 |
91 | if(state == TelephonyManager.CALL_STATE_OFFHOOK
92 | || state == TelephonyManager.CALL_STATE_RINGING){
93 |
94 | if(!isPlaying()) return;
95 |
96 | onGoingCall = true;
97 | stop();
98 |
99 | } else if (state == TelephonyManager.CALL_STATE_IDLE){
100 |
101 | if(!onGoingCall) return;
102 |
103 | onGoingCall = false;
104 | resume();
105 | }
106 | }
107 | };
108 |
109 | private MediaSessionCompat.Callback mediasSessionCallback = new MediaSessionCompat.Callback() {
110 | @Override
111 | public void onPause() {
112 | super.onPause();
113 |
114 | pause();
115 | }
116 |
117 | @Override
118 | public void onStop() {
119 | super.onStop();
120 |
121 | stop();
122 |
123 | notificationManager.cancelNotify();
124 | }
125 |
126 | @Override
127 | public void onPlay() {
128 | super.onPlay();
129 |
130 | resume();
131 | }
132 | };
133 |
134 | @Nullable
135 | @Override
136 | public IBinder onBind(Intent intent) {
137 |
138 | return iBinder;
139 | }
140 |
141 | @Override
142 | public void onCreate() {
143 | super.onCreate();
144 |
145 | strAppName = getResources().getString(R.string.app_name);
146 | strLiveBroadcast = getResources().getString(R.string.live_broadcast);
147 |
148 | onGoingCall = false;
149 |
150 | audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
151 |
152 | notificationManager = new MediaNotificationManager(this);
153 |
154 | wifiLock = ((WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE))
155 | .createWifiLock(WifiManager.WIFI_MODE_FULL, "mcScPAmpLock");
156 |
157 | mediaSession = new MediaSessionCompat(this, getClass().getSimpleName());
158 | transportControls = mediaSession.getController().getTransportControls();
159 | mediaSession.setActive(true);
160 | mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
161 | mediaSession.setMetadata(new MediaMetadataCompat.Builder()
162 | .putString(MediaMetadataCompat.METADATA_KEY_ARTIST, "...")
163 | .putString(MediaMetadataCompat.METADATA_KEY_ALBUM, strAppName)
164 | .putString(MediaMetadataCompat.METADATA_KEY_TITLE, strLiveBroadcast)
165 | .build());
166 | mediaSession.setCallback(mediasSessionCallback);
167 |
168 | telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
169 | telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
170 |
171 | handler = new Handler();
172 | DefaultBandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
173 | AdaptiveTrackSelection.Factory trackSelectionFactory = new AdaptiveTrackSelection.Factory(bandwidthMeter);
174 | DefaultTrackSelector trackSelector = new DefaultTrackSelector(trackSelectionFactory);
175 | exoPlayer = ExoPlayerFactory.newSimpleInstance(getApplicationContext(), trackSelector);
176 | exoPlayer.addListener(this);
177 |
178 | registerReceiver(becomingNoisyReceiver, new IntentFilter(AudioManager.ACTION_AUDIO_BECOMING_NOISY));
179 |
180 | status = PlaybackStatus.IDLE;
181 | }
182 |
183 | @Override
184 | public int onStartCommand(Intent intent, int flags, int startId) {
185 |
186 | String action = intent.getAction();
187 |
188 | if(TextUtils.isEmpty(action))
189 | return START_NOT_STICKY;
190 |
191 | int result = audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
192 | if(result != AudioManager.AUDIOFOCUS_REQUEST_GRANTED){
193 |
194 | stop();
195 |
196 | return START_NOT_STICKY;
197 | }
198 |
199 | if(action.equalsIgnoreCase(ACTION_PLAY)){
200 |
201 | transportControls.play();
202 |
203 | } else if(action.equalsIgnoreCase(ACTION_PAUSE)) {
204 |
205 | transportControls.pause();
206 |
207 | } else if(action.equalsIgnoreCase(ACTION_STOP)){
208 |
209 | transportControls.stop();
210 |
211 | }
212 |
213 | return START_NOT_STICKY;
214 | }
215 |
216 | @Override
217 | public boolean onUnbind(Intent intent) {
218 |
219 | if(status.equals(PlaybackStatus.IDLE))
220 | stopSelf();
221 |
222 | return super.onUnbind(intent);
223 | }
224 |
225 | @Override
226 | public void onRebind(final Intent intent) {
227 |
228 | }
229 |
230 | @Override
231 | public void onDestroy() {
232 |
233 | pause();
234 |
235 | exoPlayer.release();
236 | exoPlayer.removeListener(this);
237 |
238 | if(telephonyManager != null)
239 | telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE);
240 |
241 | notificationManager.cancelNotify();
242 |
243 | mediaSession.release();
244 |
245 | unregisterReceiver(becomingNoisyReceiver);
246 |
247 | super.onDestroy();
248 | }
249 |
250 | @Override
251 | public void onAudioFocusChange(int focusChange) {
252 |
253 | switch (focusChange) {
254 | case AudioManager.AUDIOFOCUS_GAIN:
255 |
256 | exoPlayer.setVolume(0.8f);
257 |
258 | resume();
259 |
260 | break;
261 |
262 | case AudioManager.AUDIOFOCUS_LOSS:
263 |
264 | stop();
265 |
266 | break;
267 |
268 | case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
269 |
270 | if (isPlaying()) pause();
271 |
272 | break;
273 |
274 | case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
275 |
276 | if (isPlaying())
277 | exoPlayer.setVolume(0.1f);
278 |
279 | break;
280 | }
281 |
282 | }
283 |
284 | @Override
285 | public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
286 |
287 | switch (playbackState) {
288 | case Player.STATE_BUFFERING:
289 | status = PlaybackStatus.LOADING;
290 | break;
291 | case Player.STATE_ENDED:
292 | status = PlaybackStatus.STOPPED;
293 | break;
294 | case Player.STATE_IDLE:
295 | status = PlaybackStatus.IDLE;
296 | break;
297 | case Player.STATE_READY:
298 | status = playWhenReady ? PlaybackStatus.PLAYING : PlaybackStatus.PAUSED;
299 | break;
300 | default:
301 | status = PlaybackStatus.IDLE;
302 | break;
303 | }
304 |
305 | if(!status.equals(PlaybackStatus.IDLE))
306 | notificationManager.startNotify(status);
307 |
308 | EventBus.getDefault().post(status);
309 | }
310 |
311 | @Override
312 | public void onTimelineChanged(Timeline timeline, Object manifest, int reason) {
313 |
314 | }
315 |
316 | @Override
317 | public void onTracksChanged(TrackGroupArray trackGroups, TrackSelectionArray trackSelections) {
318 |
319 | }
320 |
321 | @Override
322 | public void onLoadingChanged(boolean isLoading) {
323 |
324 | }
325 |
326 | @Override
327 | public void onPlayerError(ExoPlaybackException error) {
328 |
329 | EventBus.getDefault().post(PlaybackStatus.ERROR);
330 | }
331 |
332 | @Override
333 | public void onRepeatModeChanged(int repeatMode) {
334 |
335 | }
336 |
337 | @Override
338 | public void onShuffleModeEnabledChanged(boolean shuffleModeEnabled) {
339 |
340 | }
341 |
342 | @Override
343 | public void onPositionDiscontinuity(int reason) {
344 |
345 | }
346 |
347 | @Override
348 | public void onPlaybackParametersChanged(PlaybackParameters playbackParameters) {
349 |
350 | }
351 |
352 | @Override
353 | public void onSeekProcessed() {
354 |
355 | }
356 |
357 | public void play(String streamUrl) {
358 |
359 | this.streamUrl = streamUrl;
360 |
361 | if (wifiLock != null && !wifiLock.isHeld()) {
362 |
363 | wifiLock.acquire();
364 |
365 | }
366 |
367 | // DefaultHttpDataSourceFactory dataSourceFactory = new DefaultHttpDataSourceFactory(getUserAgent());
368 |
369 | DefaultDataSourceFactory dataSourceFactory = new DefaultDataSourceFactory(this, getUserAgent(), BANDWIDTH_METER);
370 |
371 | ExtractorMediaSource mediaSource = new ExtractorMediaSource.Factory(dataSourceFactory)
372 | .setExtractorsFactory(new DefaultExtractorsFactory())
373 | .createMediaSource(Uri.parse(streamUrl));
374 |
375 | exoPlayer.prepare(mediaSource);
376 | exoPlayer.setPlayWhenReady(true);
377 | }
378 |
379 | public void resume() {
380 |
381 | if(streamUrl != null)
382 | play(streamUrl);
383 | }
384 |
385 | public void pause() {
386 |
387 | exoPlayer.setPlayWhenReady(false);
388 |
389 | audioManager.abandonAudioFocus(this);
390 | wifiLockRelease();
391 | }
392 |
393 | public void stop() {
394 |
395 | exoPlayer.stop();
396 |
397 | audioManager.abandonAudioFocus(this);
398 | wifiLockRelease();
399 | }
400 |
401 | public void playOrPause(String url){
402 |
403 | if(streamUrl != null && streamUrl.equals(url)){
404 |
405 | if(!isPlaying()){
406 |
407 | play(streamUrl);
408 |
409 | } else {
410 |
411 | pause();
412 | }
413 |
414 | } else {
415 |
416 | if(isPlaying()){
417 |
418 | pause();
419 |
420 | }
421 |
422 | play(url);
423 | }
424 | }
425 |
426 | public String getStatus(){
427 |
428 | return status;
429 | }
430 |
431 | public MediaSessionCompat getMediaSession(){
432 |
433 | return mediaSession;
434 | }
435 |
436 | public boolean isPlaying(){
437 |
438 | return this.status.equals(PlaybackStatus.PLAYING);
439 | }
440 |
441 | private void wifiLockRelease(){
442 |
443 | if (wifiLock != null && wifiLock.isHeld()) {
444 |
445 | wifiLock.release();
446 | }
447 | }
448 |
449 | private String getUserAgent(){
450 |
451 | return Util.getUserAgent(this, getClass().getSimpleName());
452 | }
453 | }
454 |
--------------------------------------------------------------------------------