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.");
}
}
}
Related
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;
}
}
}
I am really new to Android Programming. I am working on a simple app: FlashLight for Android.
I need the app to continue run in background if the user presses the menubutton or locks the screen. The light should not stop until the user presses the button the stop the light.
Below is my code:
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
public class MainActivity
extends Activity {
ImageButton btnSwitch;
private Camera camera;
private boolean isFlashOn;
private boolean hasFlash;
Parameters params;
MediaPlayer mp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// flash switch button
btnSwitch = (ImageButton) findViewById(R.id.btnSwitch);
// First check if device is supporting flashlight or not
hasFlash = getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
if(!hasFlash) {
// device doesn't support flash
// Show alert message and close the application
AlertDialog alert = new AlertDialog.Builder(MainActivity.this).create();
alert.setTitle("Error");
alert.setMessage("Sorry, your device doesn't support flash light!");
alert.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// closing the application
finish();
}
});
alert.show();
return;
}
// get the camera
getCamera();
// displaying button image
toggleButtonImage();
// Switch button click event to toggle flash on/off
btnSwitch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(isFlashOn) {
// turn off flash
turnOffFlash();
} else {
// turn on flash
turnOnFlash();
}
}
});
}
// Get the camera
private void getCamera() {
if(camera == null) {
try {
camera = Camera.open();
params = camera.getParameters();
} catch(RuntimeException e) {
Log.e("Camera Error. Failed to Open. Error: ", e.getMessage());
}
}
}
// Turning On flash
private void turnOnFlash() {
if(!isFlashOn) {
if(camera == null || params == null) {
return;
}
params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(params);
camera.startPreview();
isFlashOn = true;
// changing button/switch image
toggleButtonImage();
}
}
// Turning Off flash
private void turnOffFlash() {
if(isFlashOn) {
if(camera == null || params == null) {
return;
}
params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(params);
camera.stopPreview();
isFlashOn = false;
// changing button/switch image
toggleButtonImage();
}
}
/*
* Toggle switch button images
* changing image states to on / off
* */
private void toggleButtonImage() {
if(isFlashOn) {
btnSwitch.setImageResource(R.drawable.btn_switch_on);
} else {
btnSwitch.setImageResource(R.drawable.btn_switch_off);
}
}
/*
* Playing sound
* will play button toggle sound on flash on / off
* */
private void playSound() {
if(isFlashOn) {
mp = MediaPlayer.create(MainActivity.this, R.raw.light_switch_off);
} else {
mp = MediaPlayer.create(MainActivity.this, R.raw.light_switch_on);
}
mp.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.release();
}
});
mp.start();
}
#Override
protected void onDestroy() {
super.onDestroy();
}
#Override
protected void onPause() {
super.onPause();
// on pause turn off the flash
turnOffFlash();
}
#Override
protected void onRestart() {
super.onRestart();
}
#Override
protected void onResume() {
super.onResume();
// on resume turn on the flash
if(hasFlash) {
turnOnFlash();
}
}
#Override
protected void onStart() {
super.onStart();
// on starting the app get the camera params
getCamera();
}
#Override
protected void onStop() {
super.onStop();
// on stop release the camera
if(camera != null) {
camera.release();
camera = null;
}
}
}
You;re turning off the light in onPause. When the screen is locked or the app is minimized, onPause is called. If you don't want that to happen, don't call it there.
I think you are searching for Services: http://developer.android.com/guide/components/services.html
They are very similar to an Activity but do dot provide a user interface and can run in the background.
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?
I have the following code:
package com.example.top_tech_deals;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.VideoView;
public class Splash extends Activity{
#Override
protected void onCreate(Bundle TravisLoveBacon) {
// TODO Auto-generated method stub
super.onCreate(TravisLoveBacon);
setContentView(R.layout.splash);
VideoView vv = (VideoView)this.findViewById(R.id.videoView);
String fileName = "android.resource://" + getPackageName() + "/" + R.raw.splashvid2;
vv.setVideoURI(Uri.parse(fileName));
vv.start();
Thread timer = new Thread(){
public void run(){
try{
sleep(12000);
} catch(InterruptedException e){
e.printStackTrace();
} finally{
Intent openStartingPoint = new Intent ("android.intent.action.MENU");
startActivity(openStartingPoint);
}
}
};
timer.start();
}
//Function that will handle the touch
#Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
synchronized(timer){
splashTread.notifyAll();
}
}
return true;
}
}
By using one of Bucky's tutorials I managed to create the above code which is used to create a splash screen for 12 seconds. I also modified it so that a Video will play. The main problem I'm having is with the last bit of the code which is the OnTouchEvent I found online. What it should do is allow the user to skip the splash screen simply by tapping the screen, which should take the user to the MENU file.
The error seems to be in this line:
synchronized(timer){
Which says "error timer cannot be resolved into a variable"
Why is this happening and how can I fix it? Thanks for the help.
See code:
package com.example.top_tech_deals;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.VideoView;
public class Splash extends Activity{
Thread timer;
#Override
protected void onCreate(Bundle TravisLoveBacon) {
// TODO Auto-generated method stub
super.onCreate(TravisLoveBacon);
setContentView(R.layout.splash);
VideoView vv = (VideoView)this.findViewById(R.id.videoView);
String fileName = "android.resource://" + getPackageName() + "/" + R.raw.splashvid2;
vv.setVideoURI(Uri.parse(fileName));
vv.start();
timer = new Thread(){
public void run(){
try{
synchronized (this) {
wait(12000);
}
} catch(InterruptedException e){
e.printStackTrace();
} finally{
Intent openStartingPoint = new Intent ("android.intent.action.MENU");
startActivity(openStartingPoint);
}
}
};
timer.start();
}
//Function that will handle the touch
#Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
synchronized(timer){
timer.notify();
}
}
return true;
}
}
Your timer variable is local to your onCreate() method, but you're trying to access it (via synchronized) in a different method -- so it is unresolved. You need to either move timer to being a class data member, or use some other object whose scope is available in your onTouchEvent() method.
User a count downTimer instead of using a thread and in the override method onFinish()
CountDownTimer countDownTimer = new CountDownTimer(12000, 1000) {
public void onTick(long millisUntilFinished) {
//ToDO
}
public void onFinish() {
Intent openStartingPoint = new Intent ("android.intent.action.MENU");
startActivity(openStartingPoint);
}
}.start();
and in onTouch()
#Override
public boolean onTouchEvent(MotionEvent event) {
startActivity(openStartingPoint);
countDownTimer.cancle();
return true;
}
It seems your timer variable is not declare at class scope,it is declared in onCreate() function thats why other methods are not able to get it by reference. I suggest declare this as class variable like this private Thread timer = null; and initialize it in oncreate() method
#Override
protected void onCreate(Bundle TravisLoveBacon) {
// TODO Auto-generated method stub
super.onCreate(TravisLoveBacon);
setContentView(R.layout.splash);
VideoView vv = (VideoView)this.findViewById(R.id.videoView);
String fileName = "android.resource://" + getPackageName() + "/" + R.raw.splashvid2;
vv.setVideoURI(Uri.parse(fileName));
vv.start();
this.timer = new Thread(){
public void run(){
try{
sleep(12000);
} catch(InterruptedException e){
e.printStackTrace();
} finally{
Intent openStartingPoint = new Intent ("android.intent.action.MENU");
startActivity(openStartingPoint);
}
}
};
timer.start();
}
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.