How to set the Audio Mode in Android to improve call Quality? - java

Problem:
I have a that i regularly talk to via Discord but the Audio Quality is quite bad except when she is being called (ringtone) or sometimes when playing back videos on Instagram. My theory is that the Audio Mode android uses internally affects sound quality (MODE_IN_CALL -> bad, MODE_RINGTONE -> good).
Attempted solution:
Write an app in android studio that emulates all the possible audio modes using AudioManager.
Problem 2:
I know very little about java and android and stuff (i mostly program c++ for arduinos) and i can't get it to work.
I hope im not asking too much here. This is my **horrible **attempt:
package cf.hacker3000.audiomodeswitcher;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
Button ring, alarm;
private AudioManager am;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ring = (Button) findViewById(R.id.btn_ring);
alarm = (Button) findViewById(R.id.btn_alarm);
am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
ring.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MediaPlayer player = MediaPlayer.create(MainActivity.this, R.raw.silence);
final AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
if (audioManager.getStreamVolume(AudioManager.STREAM_RING) != 0) {
player.setAudioStreamType(AudioManager.STREAM_RING);
player.setLooping(true);
try {
player.prepare();
} catch (IOException e) {
e.printStackTrace();
}
player.start();
toastCurrentMode();
}
}
});
alarm.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MediaPlayer player = MediaPlayer.create(MainActivity.this, R.raw.silence);
final AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
player.setAudioStreamType(AudioManager.STREAM_ALARM);
player.setLooping(true);
try {
player.prepare();
} catch (IOException e) {
e.printStackTrace();
}
player.start();
toastCurrentMode();
}
}
});
}
private void toastCurrentMode() {
switch (am.getMode()) {
case AudioManager.MODE_RINGTONE:
Toast.makeText(MainActivity.this,"Now RINGTONE", Toast.LENGTH_LONG).show();
break;
case AudioManager.MODE_IN_COMMUNICATION:
Toast.makeText(MainActivity.this,"Now IN_COMMUNICATION", Toast.LENGTH_LONG).show();
break;
case AudioManager.MODE_IN_CALL:
Toast.makeText(MainActivity.this,"Now IN_CALL", Toast.LENGTH_LONG).show();
break;
case AudioManager.MODE_NORMAL:
Toast.makeText(MainActivity.this,"Now NORMAL", Toast.LENGTH_LONG).show();
break;
default:
Toast.makeText(MainActivity.this,"Unknown mode...", Toast.LENGTH_LONG).show();
break;
}
}
}

Related

How to add user agent to radio players APKs

I have made 2 radio player apps but I want to create a new update with a custom user agent instead of stagefright. My own user-agent will be Shoutcast Streaming Player/1.0 (information of a android device (Version 1.0)) Here is the code of my first Android app:
package com.musicasporlukas.player1;
// Primera actualización de Musicas Por Lukas
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
Button playBtn, pauseBtn;
MediaPlayer mediaPlayer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
playBtn = findViewById(R.id.idBtnPlay);
pauseBtn = findViewById(R.id.idBtnPause);
playBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
playAudio();
}
});
pauseBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
mediaPlayer.reset();
mediaPlayer.release();
Toast.makeText(MainActivity.this, "Musicas Por Lukas se ha pausado en línea.", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(MainActivity.this, "Musicas Por Lukas no se tocó en línea.", Toast.LENGTH_SHORT).show();
}
}
});
}
private void playAudio() {
String audioUrl = "https://eu8.fastcast4u.com/proxy/lukasito?mp=/1";
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(audioUrl);
mediaPlayer.prepare();
mediaPlayer.start();
}
catch (IOException e) {
e.printStackTrace();
}
Toast.makeText(this, "Reproducción de Musicas Por Lukas en línea.", Toast.LENGTH_SHORT).show();
}
}
When a device connects to my URL Media Audio Source, the user-agent will automatically be stagefright.
I only get stagefright / Dalvik users.

Issues with Android's Mediaplayer lib

I have some pretty standard code to run an MP3, but it takes like 2 seconds to play it. This app is very simple. This app should play a 1-2 second sound. Also, is there a way to buffer the mp3 at app's load into the RAM or something so it can play very quickly(w/o delay)? Please help!
import android.content.Context;
import android.media.MediaPlayer;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
public class MainActivity1 extends ActionBarActivity {
MediaPlayer mediaPlayer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity1);
}
public void playSound(View v)throws Exception{
mediaPlayer = MediaPlayer.create(this, R.raw.john_cena);
mediaPlayer.setLooping(false);
mediaPlayer.start();
Thread.sleep(1500);
//mediaPlayer.stop();
mediaPlayer.reset();
mediaPlayer.release();
Log.d("1234FIND", "STOP RELEASE RESET");
}
}
If you want to avoid the slight delay as the media file is loaded, you should prepare your media player instance ahead of time. There's a delay while your media file is loaded and prepared for playback. You have a few options:
Continue to use MediaPlayer.create(), but do so sooner, before you're ready to call playSound(). In the simple example you provided, try calling MediaPlayer.create() in the onCreate() method.
You could also use the prepareAsync() and setOnPreparedListener() methods to prepare the media file in the background and set a notification when it is ready. This is more complicated but provides you with more control and a better user experience. If you need to load a large file, this is the best way to make it work. Small audio files probably aren't as big of a deal.
If you're going to play the same media file over and over again, you don't need to release the MediaPlayer right away. If you don't reset or release, the media will still be in memory and can play again immediately. But be sure to call release() when you're definitely done, and in the lifecycle events such as onPause() or onStop().
See the documentation for details.
To get it to play for a few seconds and then stop, use Android's CountDownTimer:
// Will call onFinish() after 2 seconds. Second parameter is
// for onTick() which we don't need and can ignore.
new CountDownTimer(2000, 2000) {
public void onTick(long millisUntilFinished) {
// Do nothing...
}
public void onFinish() {
mediaPlayer.stop();
// Decide here if you need to prepare a new clip
// or release the media player
}
}.start();
Here is a complete example:
package com.example.matthew.somediaplayer;
import android.content.res.AssetFileDescriptor;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "TEST";
private MediaPlayer mediaPlayer;
private boolean isMediaPlayerPrepared = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
button.setEnabled(false);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mediaPlayer.isPlaying() == false) {
playSound();
}
}
});
}
#Override
public void onResume(){
super.onResume();
Log.d(TAG, "In onResume()");
createAndPrepareMediaPlayer();
}
#Override
public void onStop() {
super.onStop();
Log.d(TAG, "In onStop()");
mediaPlayer.release();
isMediaPlayerPrepared = false;
}
#Override
public void onPause(){
Log.d(TAG, "In onPause()");
super.onPause();
}
public void createAndPrepareMediaPlayer() {
Log.d(TAG, "In createAndPrepareMediaPlayer()");
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
if(isMediaPlayerPrepared == false) {
try {
AssetFileDescriptor afd = getResources().openRawResourceFd(R.raw.test);
mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
Log.d(TAG, "Media player is prepared.");
isMediaPlayerPrepared = true;
Button button = (Button) findViewById(R.id.button);
button.setEnabled(true);
}
});
Log.d(TAG, "Beginning to prepare media player.");
mediaPlayer.prepareAsync();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void playSound() {
Log.d(TAG, "In playSound()");
if( isMediaPlayerPrepared ) {
// Will call onFinish() after 2 seconds. Second parameter is
// for onTick() which we don't need and can ignore.
new CountDownTimer(2000, 2000) {
public void onTick(long millisUntilFinished) {
// Do nothing...
}
public void onFinish() {
Log.d(TAG, "In onFinish()");
// We can't call a method like pause on an unprepared MediaPlayer instance.
if( isMediaPlayerPrepared ) {
mediaPlayer.pause();
} else {
Log.d(TAG, "Media player isn't prepared, and isn't allowed to pause.");
}
}
}.start();
mediaPlayer.setLooping(false);
mediaPlayer.seekTo(0); // set to beginning of track (if not already)
mediaPlayer.start();
Log.d(TAG, "Starting to play a sound.");
}
}
}

Using Media Player inside my app

I am running a Text To Speech code, but now my new requirement is to provide "Pause" facility. I read number of SO questions and found I have to write data into a file and have to use media player class to play the file. Below is my code.
public void speak(String text)
{
HashMap<String,String> map = new HashMap<String,String>();
map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, text);
tts.synthesizeToFile(text, map,"ttsFile.wav");
//tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
Here I have 2 questions.
I do not want to open media player to play the file, file should be played inside the app with the media player controls (just like how you can set videoView.setMediaController(); to VideoView). How can I do this? any example please?
Is there any "Cache" place to save the above file so it will be deleted once the app is closed?
You can do something like this:
Create a video player in your application :
VideoPlayerActivity.java:
package com.camera.manual;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;
public class VideoPlayerActivity extends Activity {
private boolean mResumed = false;
private boolean mFocused = false;
private boolean mControlResumed = false;
private VideoView videoView = null;
private int stopPosition = 0;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(R.style.Theme_TransparentVideo);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
setContentView(R.layout.video_view);
videoView =(VideoView)findViewById(R.id.myvideoview);
MediaController mediaController= new MediaController(this);
mediaController.setAnchorView(videoView);
Uri uri=Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.slow);
videoView.setMediaController(mediaController);
videoView.setVideoURI(uri);
videoView.requestFocus();
videoView.start();
}
#Override
public void onPause() {
super.onPause();
mResumed = false;
if (mControlResumed) {
if (null != videoView)
videoView.pause();
stopPosition = videoView.getCurrentPosition();
mControlResumed = false;
}
}
#Override
public void onResume() {
super.onResume();
mResumed = true;
if (mFocused && mResumed && !mControlResumed) {
if(null != videoView) {
//videoView.resume();
videoView.seekTo(stopPosition);
videoView.start();
}
mControlResumed = true;
}
}
#Override
public void onWindowFocusChanged(boolean hasFocus) {
mFocused = hasFocus;
if (mFocused && mResumed && !mControlResumed) {
if (null != videoView) {
//videoView.resume();
videoView.seekTo(stopPosition);
videoView.start();
}
mControlResumed = true;
}
}
}
You can call it like this:
Intent intent = new Intent();
intent.setClass(mContext, VideoPlayerActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
mContext.startActivity(intent);

Android: playing large video MP4 files through http

I have 300 MB videos on my http server...I'm trying to make good simple code to play (in 3G mode) these videos remotely in my application - sd storing or local storing is not a option. Of course I started from demo source in SDK android-17 (MediaPlayerDemo_Video, VideoViewDemo...) and in 3G mode my video doesn't play (I'm testing on Samsung Galaxy Nexus). After that I made new try in next code:
/**
* Listing 15-4: Initializing and assigning a Surface View to a Media Player
*/
import java.io.IOException;
import mobile.dariknews.R;
import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.MediaController.MediaPlayerControl;
public class SurfaceViewVideoViewActivity extends Activity
implements SurfaceHolder.Callback {
static final String TAG = "SurfaceViewVideoViewActivity";
private MediaPlayer mediaPlayer;
public void surfaceCreated(SurfaceHolder holder) {
try {
// When the surface is created, assign it as the
// display surface and assign and prepare a data
// source.
mediaPlayer.setDisplay(holder);
mediaPlayer.setDataSource("http://snimkitevi-bg.com/darik/1.MP4");
//mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.prepareAsync();
} catch (IllegalArgumentException e) {
Log.e(TAG, "Illegal Argument Exception", e);
} catch (IllegalStateException e) {
Log.e(TAG, "Illegal State Exception", e);
} catch (SecurityException e) {
Log.e(TAG, "Security Exception", e);
} catch (Exception e) {
Log.e(TAG, "IO Exception", e);
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
mediaPlayer.release();
}
public void surfaceChanged(SurfaceHolder holder,
int format, int width, int height) { }
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.surfaceviewvideoviewer);
// Create a new Media Player.
mediaPlayer = new MediaPlayer();
// Get a reference to the Surface View.
final SurfaceView surfaceView =
(SurfaceView)findViewById(R.id.surfaceView);
// Configure the Surface View.
surfaceView.setKeepScreenOn(true);
// Configure the Surface Holder and register the callback.
SurfaceHolder holder = surfaceView.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
holder.setFixedSize(400, 300);
// Connect a play button.
Button playButton = (Button)findViewById(R.id.buttonPlay);
playButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mediaPlayer.start();
}
});
// Connect a pause button.
Button pauseButton = (Button)findViewById(R.id.buttonPause);
pauseButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mediaPlayer.pause();
}
});
// Add a skip button.
Button skipButton = (Button)findViewById(R.id.buttonSkip);
skipButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mediaPlayer.seekTo(mediaPlayer.getDuration()/2);
}
});
/**
* Listing 15-5: Controlling playback using the Media Controller
*/
MediaController mediaController = new MediaController(this);
mediaController.setMediaPlayer(new MediaPlayerControl() {
public boolean canPause() {
return true;
}
public boolean canSeekBackward() {
return true;
}
public boolean canSeekForward() {
return true;
}
public int getBufferPercentage() {
return 0;
}
public int getCurrentPosition() {
return mediaPlayer.getCurrentPosition();
}
public int getDuration() {
return mediaPlayer.getDuration();
}
public boolean isPlaying() {
return mediaPlayer.isPlaying();
}
public void pause() {
mediaPlayer.pause();
}
public void seekTo(int pos) {
mediaPlayer.seekTo(pos);
}
public void start() {
mediaPlayer.start();
}
});
}
}
after that new try in
import android.app.Activity;
import android.app.Application;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.widget.MediaController;
import android.widget.VideoView;
public class PlayerActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.videoplayer);
new LoadOutbox().execute();
}
/**
* Background Async Task to Load all OUTBOX messages by making HTTP Request
* */
class LoadOutbox extends AsyncTask<String, String, String> {
int progressBarStatus = 0;
private Handler progressBarHandler = new Handler();
/**
* Before starting background thread Show Progress Dialog
* */
#Override
protected void onPreExecute() {
super.onPreExecute();
}
/**
* getting Outbox JSON
* */
protected String doInBackground(String... args) {
// Building Parameters
VideoView videoView = (VideoView) findViewById(R.id.videoView1);
videoView.setKeepScreenOn(true);
// Attach a Media Controller MediaController mediaController = new
// MediaController( this); videoView.setMediaController(
// mediaController);
MediaController mediaController = new MediaController(
PlayerActivity.this);
videoView.setMediaController(mediaController);
Uri uri = Uri.parse("http://snimkitevi-bg.com/darik/1.MP4");
videoView.setVideoURI(uri);
videoView.start();
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
}
});
}
}
public static Bitmap getLocalImageBitmap(String url) {
return MainNewsActivities.getLocalImageBitmap(url);
}
}
After that I played with 50 other tests with MediaPlayer and VIewVideo... All results are same - slow and freezed video playing. My need is pretty simple - I have big video files (mp4) on my website and I want to play them on my app. So I'm here - what is the right formula for playing of large remote files in android application?
PS: Of course I tried to use GStreamer but this framework is not compatible with last android environment - I played with in 2 days...My video is OK - in wifi mode I play http://snimkitevi-bg.com/darik/1.MP4
So could you give me some good advases, examples, tutorials how MediaPlayer could play normally, buffered and fastly big video files in 3G internet connection?

thread, MediaPlayer, ProgressDialog and context issues

I've tried and done everything that I read on internet and here on stackoverflow to solve my problem, but no success till now. What I'm trying to do is basically click on a button and show a ProgressDialog while the MediaPlayer is buffering a stream from internet. I got many types of erros with context stuff (NullPointerException) and thread problems too. Here are some details about the code:
the button is a toggle button with background image sat based on events (on, off, not connected show different image buttons);
The function prepareStream() is the one that should be ran in a thread and dismiss the ProgressDialog after load the stream. it's calling an http stream of BBC radio;
I guess that the problem is about the context... I've put some Log.d TAGs on the code to check where was happening the problem and O figured out that was on mediaPlayer.start() method.
He goes the little boy:
package com.android.iFocus;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ToggleButton;
import com.insightoverflow.iFocus.R;
public class iFocusActivity extends Activity implements OnClickListener {
//Declare Controls
public int count = 0;
public int x = 1;
public MediaPlayer mediaPlayer = null;
ToggleButton toggleRain = null;
Button buttonAbout = null;
Button buttonMethod = null;
Button buttonLink = null;
public ProgressDialog progressDialog;
public static final String TAG = "getFocused";
public boolean isOnline() {
//Check if internet is connected
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null;
}
public void prepareStream(final Context context){
if(isOnline()){
// init player
new Thread()
{
public void run()
{
try {
sleep(1500);
//progressDialog.show();
mediaPlayer = MediaPlayer.create(context, Uri.parse("http://vprbbc.streamguys.net:80/vprbbc24.mp3"), null);
x=2;
} catch (Exception e){
x=3;
}
//dismiss the progressdialog
progressDialog.dismiss();
}
}.start();
} else {
x=3;
}
}
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
// load layout
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// load controls
toggleRain = (ToggleButton)findViewById(R.id.toggleRain);
buttonAbout = (Button)findViewById(R.id.buttonAbout);
buttonMethod = (Button)findViewById(R.id.buttonMethod);
buttonLink = (Button)findViewById(R.id.buttonLink);
//Define Listeners (click event handler)
toggleRain.setOnClickListener(this);
buttonAbout.setOnClickListener(this);
buttonMethod.setOnClickListener(this);
buttonLink.setOnClickListener(this);
// init state for player
count = 0;
//Context APP
//Context appContext = this.getApplicationContext();
if (!isOnline()){
toggleRain.setBackgroundDrawable(getResources().getDrawable(R.drawable.notconnectedbutton));
x=3;
}
}
public void onClick(View v) {
if( toggleRain.getId() == ((Button)v).getId() ){
//meanwhile device is offline, do this
do {
toggleRain.setBackgroundDrawable(getResources().getDrawable(R.drawable.notconnectedbutton));
try{
Thread.currentThread();
//do what you want to do before sleeping
Thread.sleep(1000);//sleep for 1000 ms
//do what you want to do after sleeptig
} catch(Exception ie){}
continue;
}while (!isOnline());
//If device is online, go for this
if (((CompoundButton) toggleRain).isChecked()) {
toggleRain.setBackgroundDrawable(getResources().getDrawable(R.drawable.stopbutton));
} else {
toggleRain.setBackgroundDrawable(getResources().getDrawable(R.drawable.playbutton));
}
//----> HERE GOES WHERE I THINK IS THE PROBLEM <-----
//---------------------------------------------------
if (isOnline()){
//If music is not playing, start music
if(count==0){
Log.d(TAG, "START PROGRESS DIALOG");
progressDialog = ProgressDialog.show(v.getContext(), "Load", "Loading");
//progressDialog = ProgressDialog.show(, "Load", "Loading...", true, false);
Log.d(TAG, "END PROGRESS DIALOG");
Log.d(TAG, "START PREPARE STREAM");
Context context = v.getContext();
prepareStream(context);
Log.d(TAG, "END PREPARE STREAM");
Log.d(TAG, "START MEDIA PLAYER START");
//LOG CAT START AND END ALL OF THE OTHER LOG TAGS, EXCEPT THIS mediaplayer.start()
mediaPlayer.start();
Log.d(TAG, "END MEDIAPLAYER START");
count = 1;
} else {
mediaPlayer.pause();
count = 0;
}
}
} else if( buttonAbout.getId() == ((Button)v).getId() ){
Intent i = new Intent(iFocusActivity.this, AboutActivity.class);
startActivity(i);
}
else if ( buttonMethod.getId() == ((Button)v).getId() ){
Intent o = new Intent(iFocusActivity.this, MethodActivity.class);
startActivity(o);
}
else if ( buttonLink.getId() == ((Button)v).getId() ){
Uri uri = Uri.parse( "http://getFocused.in" );
startActivity( new Intent( Intent.ACTION_VIEW, uri ) );
}
}
#Override
protected void onDestroy() {
super.onDestroy();
if(mediaPlayer != null) {
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = null;
}
}
}
So logcat tell me on the time mediaPlayer.start() is called the NullPointerException
You must run your ProgressDialog.show() in the runOnUiThread() method(Not in the main UI thread). See the Android docs here
Create a progress dialog object and then, write this code.
runOnUiThread(new Runnable(){
#Override
public void run() {
dialog.show();
}
});
Also the calls prepareStream() and mediaplayer.start() should go into a separate thread and not the main UI thread.
Sort out the thread related issues and you should be done.

Categories