Keep trying to buffer/reconnect - android

I have a small radio app, I'm trying to get it to keep trying to reconnect/buffer my stream forever without quiting... not sure how I can loop this, but this is my buffering code. Any kind of help I can get on this is greatly appreciated!
public void prepare() {
/* ------Station- buffering-------- */
isPreparingStarted = true;
setStatus(STATUS_BUFFERING);
sendBroadcast(new Intent(MODE_THREE));
showNotification();
try {
if (getCurrentStationType().equals(SIGNAL_AAC))
multiPlayer.playAsync(StationURL());
else {
mediaPlayer.setDataSource(StationURL());
mediaPlayer.prepareAsync();
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
stop();
} catch (NullPointerException e) {
e.printStackTrace();
stop();
} catch (IllegalStateException e) {
e.printStackTrace();
stop();
} catch (IOException e) {
e.printStackTrace();
stop();
} catch (Exception e) {
e.printStackTrace();
stop();
}
}

Related

XMPP error reply received from myroom#conference.****/myroom#conference.****: XMPPError: item-not-found - cancel

I am not able to join the room. I have gone through the documentation XMPP Instant Room Error
Here is how i am creating instant room:
try {
UserSearchManager usm = new UserSearchManager(Utils.connection);
List<DomainBareJid> services = usm.getSearchServices();
String roomjid = "myroom#" + services.get(0);
mucJid = JidCreate.entityBareFrom(roomjid);
Log.d(TAG, mucJid.toString());
// Create the nickname.
nickname = Resourcepart.from(roomjid);
Log.d(TAG, nickname.toString());
} catch (XmppStringprepException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (SmackException.NoResponseException e) {
e.printStackTrace();
} catch (SmackException.NotConnectedException e) {
e.printStackTrace();
} catch (XMPPException.XMPPErrorException e) {
e.printStackTrace();
}
// Get the MultiUserChatManager
manager = MultiUserChatManager.getInstanceFor(Utils.connection);
// Get a MultiUserChat using MultiUserChatManager
MultiUserChat muc = manager.getMultiUserChat(mucJid);
// Create the room and send an empty configuration form to make this an instant room
try {
// Prepare a list of owners of the new room
Set<Jid> owners = JidUtil.jidSetFrom(new String[] { "54321#rahul", "12345#rahul" });
muc.create(nickname)
.getConfigFormManager()
.setRoomOwners(owners)
.submitConfigurationForm();
muc.sendConfigurationForm(new Form(DataForm.Type.submit));
muc.join(nickname);
EntityBareJid invitemucJid = JidCreate.entityBareFrom("12345#rahul");
muc.invite(invitemucJid, "testing");
} catch (SmackException.NoResponseException e) {
e.printStackTrace();
} catch (XMPPException.XMPPErrorException e) {
e.printStackTrace();
} catch (SmackException.NotConnectedException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (MultiUserChatException.MucAlreadyJoinedException e) {
e.printStackTrace();
} catch (MultiUserChatException.MissingMucCreationAcknowledgeException e) {
e.printStackTrace();
} catch (MultiUserChatException.NotAMucServiceException e) {
e.printStackTrace();
} catch (XmppStringprepException e) {
e.printStackTrace();
} catch (MultiUserChatException.MucConfigurationNotSupportedException e) {
e.printStackTrace();
}
The MUC room is getting created successfully. But when i am checking through Invitation Listener and trying to join the room. It is giving error.
The Invitation listener code is as follow:
try {
Resourcepart nickname = Resourcepart.from(room.getRoom().toString());
room.join(nickname);
Log.d(TAG, "room status---> " + room.isJoined());
runOnUiThread(new Runnable() {
#Override
public void run() {
if (room.isJoined()) {
Toast.makeText(FriendListActivity.this, "Joined", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(FriendListActivity.this, "Not joined", Toast.LENGTH_SHORT).show();
}
}
});
} catch (XmppStringprepException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (XMPPException.XMPPErrorException e) {
e.printStackTrace();
} catch (SmackException.NotConnectedException e) {
e.printStackTrace();
} catch (SmackException.NoResponseException e) {
e.printStackTrace();
} catch (MultiUserChatException.NotAMucServiceException e) {
e.printStackTrace();
}
Below is screen shot of the stack Trace
As per the documentation it says that the MUC room is locked. But I have created instant room along with default configuration.
Maybe it's a network error, I do somethings wrong if I use Wifi but get corrcet result with cellular network.

Handle Next And Previous Button in MediaPlayer?

This is how I am doing this.
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.next: {
currentFile = input.get(currentPosition + 1);
if (mediaPlayer != null) {
mediaPlayer.release();
}
try{
mediaPlayer.setDataSource(currentFile);
mediaPlayer.reset();
mediaPlayer.prepare();
mediaPlayer.start();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
break;
}
case R.id.prev: {
currentFile = input.get(currentPosition - 1);
if (mediaPlayer != null) {
mediaPlayer.release();
}
try {
mediaPlayer.setDataSource(currentFile);
mediaPlayer.prepare();
mediaPlayer.start();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
}
Can't play the next and previous buttons. I want to play next and previous track from raw folder. I tried to do so but I am unable to do this. Give some suggestions. Thank You...

Android: Activity freezes while running code that takes a while to complete

The app I am working on takes JSONs from a server and has to interpret them. I have around 6-7 JSONs that are requested.
As such, this is bound to take some time.
This code runs whenever the user types in information to an EditText and clicks a button. After the search button is clicked, the app freezes and will not do anything until all the data is loaded.
I tried having the code like this:
Button button = (Button) findViewById(R.id.search_button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
InputMethodManager imm = (InputMethodManager) getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(summonerNameET.getWindowToken(), 0);
final CollectUserData c = new CollectUserData();
c.setRegion(region);
c.setSummonerName(summonerNameET.getText().toString());
Runnable r = new Runnable() {
#Override
public void run() {
try {
c.setUpTheJSONs();
updateUI(c);
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
};
r.run();
}
});
But it still freezes. Here is the code for setUpTheJSON();
public void setUpTheJSONs() throws IOException, JSONException, URISyntaxException {
jsonSummonerInfo = getJsonSummonerInfo();
if(canIContinue()) {
jsonSummonerStats = getJsonSummaryStats();
//jsonSummonerRankedStats = getJsonRankedStats();
jsonSummonerMatchHistory = getJsonMatchHistory();
jsonSummonerLeagueInfo = getJsonSummonerLeagueInfo();
jsonSummonerRecentGames = getJsonSummonerRecentGames();
}
}
And here is the code for unpdateUI(c);
public void updateUI(CollectUserData c) {
if (c.canIContinue()) {
String league = "";
try {
league = c.getMundaneCurrentLeague();
} catch (JSONException e) {
e.printStackTrace();
}
setLeague(tierIV, league);
try {
summonerNameTV.setText(c.getSummonerName());
} catch (JSONException e) {
e.printStackTrace();
}
try {
tierTV.setText(c.getTier());
} catch (JSONException e) {
e.printStackTrace();
}
try {
leaguePointsTV.setText(c.getLeaguePoints() + "");
} catch (JSONException e) {
e.printStackTrace();
}
try {
winsTV.setText("W: " + c.getWins());
} catch (JSONException e) {
e.printStackTrace();
}
slashTV.setText("/");
try {
lossesTV.setText("L: " + c.getLosses());
} catch (JSONException e) {
e.printStackTrace();
}
try {
lastSeasonRankTV.setText("Season 4: " + c.getRankOfLastSeason());
} catch (JSONException e) {
e.printStackTrace();
}
try {
gameModeGameOneTV.setText(c.getGameType(1));
} catch (JSONException e) {
e.printStackTrace();
}
try {
gameOneChampion.setBackground(c.getChampionPicturePlayed(1));
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
gameOneKillsTV.setText(c.getKills(1) + "");
} catch (JSONException e) {
e.printStackTrace();
}
gameOneSlashOneTV.setText(" / ");
try {
gameOneDeathsTV.setText(c.getDeaths(1) + "");
} catch (JSONException e) {
e.printStackTrace();
}
gameOneSlashTwoTV.setText(" / ");
try {
gameOneAssistsTV.setText(c.getAssists(1) + "");
} catch (JSONException e) {
e.printStackTrace();
}
try {
gameOneGoldTV.setText(c.getGold(1) + " K");
} catch (JSONException e) {
e.printStackTrace();
}
try {
gameOneCSTV.setText(" " + c.getMinionsKilled(1) + " CS");
} catch (JSONException e) {
e.printStackTrace();
}
boolean gameOneWon = true;
try {
gameOneWon = c.isGameWon(1);
} catch (JSONException e) {
e.printStackTrace();
}
if (gameOneWon) {
gameOneTitleBar.setBackgroundColor(Color.parseColor("#d604c429"));
gameOne.setBackgroundColor(Color.parseColor("#4600FF06"));
}
if (!gameOneWon) {
gameOneTitleBar.setBackgroundColor(Color.parseColor("#d6ff0100"));
gameOne.setBackgroundColor(Color.parseColor("#4fff0100"));
}
try {
gameOneItemOneIV.setBackground(c.getItemFromMatchHistory(1, 0, getResources()));
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
gameOneItemTwoIV.setBackground(c.getItemFromMatchHistory(1, 1, getResources()));
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
gameOneItemThreeIV.setBackground(c.getItemFromMatchHistory(1, 2, getResources()));
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
gameOneItemFourIV.setBackground(c.getItemFromMatchHistory(1, 3, getResources()));
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
gameOneItemFiveIV.setBackground(c.getItemFromMatchHistory(1, 4, getResources()));
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
gameOneItemSixIV.setBackground(c.getItemFromMatchHistory(1, 5, getResources()));
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Drawable[] tempDrawable = null;
try {
tempDrawable = c.getTeamPlayerChampionIcon(1, 100);
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (tempDrawable != null) {
gameOneTeamOnePlayerOne.setBackground(tempDrawable[0]);
gameOneTeamOnePlayerTwo.setBackground(tempDrawable[1]);
gameOneTeamOnePlayerThree.setBackground(tempDrawable[2]);
gameOneTeamOnePlayerFour.setBackground(tempDrawable[3]);
gameOneTeamOnePlayerFive.setBackground(tempDrawable[4]);
}
Drawable[] tempDrawable2 = null;
try {
tempDrawable2 = c.getTeamPlayerChampionIcon(1, 200);
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (tempDrawable != null) {
gameOneTeamTwoPlayerOne.setBackground(tempDrawable2[0]);
gameOneTeamTwoPlayerTwo.setBackground(tempDrawable2[1]);
gameOneTeamTwoPlayerThree.setBackground(tempDrawable2[2]);
gameOneTeamTwoPlayerFour.setBackground(tempDrawable2[3]);
gameOneTeamTwoPlayerFive.setBackground(tempDrawable2[4]);
}
String[] tempString = null;
try {
tempString = c.getTeamPlayerName(1, 100);
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (tempString != null) {
gameOneTeamOnePlayerOneName.setText(tempString[0]);
gameOneTeamOnePlayerTwoName.setText(tempString[1]);
gameOneTeamOnePlayerThreeName.setText(tempString[2]);
gameOneTeamOnePlayerFourName.setText(tempString[3]);
gameOneTeamOnePlayerFiveName.setText(tempString[4]);
}
String[] tempString2 = null;
try {
tempString2 = c.getTeamPlayerName(1, 200);
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (tempString2 != null) {
gameOneTeamTwoPlayerOneName.setText(tempString2[0]);
gameOneTeamTwoPlayerTwoName.setText(tempString2[1]);
gameOneTeamTwoPlayerThreeName.setText(tempString2[2]);
gameOneTeamTwoPlayerFourName.setText(tempString2[3]);
gameOneTeamTwoPlayerFiveName.setText(tempString2[4]);
}
}
}
Anyone know a fix for this?
Thanks ☺
You need to introduce multi threading. The r.run() command needs to be started by another thread.
http://developer.android.com/training/multiple-threads/index.html
You are running the Runnable in the UI Thread and this is why you are getting your UI blocked until it finished.
Try something like this:
Button button = (Button) findViewById(R.id.search_button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
InputMethodManager imm = (InputMethodManager) getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(summonerNameET.getWindowToken(), 0);
final CollectUserData c = new CollectUserData();
c.setRegion(region);
c.setSummonerName(summonerNameET.getText().toString());
new Thread(new Runnable() {
#Override
public void run() {
try {
c.setUpTheJSONs();
updateUI(c);
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
}).start();
}
});
Put your downloading files in a separate thread. Use AsyncTask or Java Thread. Just simply.
class MyTask extends AsyncTask<Void, Void, Void> {
#Override
protected void onPreExecute() {
super.onPreExecute();
//Show progress dialog, if u want
}
#Override
protected Void doInBackground(Void... params) {
//Do all your processing here!
return null;
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
//Return some result to UI
}
}

I wanna play recording again in existing file

I am a newbie.
I'm developing an android (4.2) app to record some audio.
The code seems to run fine the first time, and it even saves the files on the SD Card.
But, if I try to record again, my app crashes.
This is my code:
StringVoiceFile = PathImagePage + "/"+book_id+"-"+pagenumber+".mp3";
myAudioRecorder = new MediaRecorder();
myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
myAudioRecorder.setOutputFile(StringVoiceFile);
this is my function
public void start(){
String PathImagePage = AppDirectory+"/content/"+userid+"/books/"+book_id+"/";
StringVoiceFile = PathImagePage + "/"+book_id+"-"+pagenumber+".mp3";
File VoiceFile = new File(PathImagePage, +"-"+pagenumber+".mp3");
MediaPlayer m = new MediaPlayer();
if (VoiceFile.exists()){
try {
myAudioRecorder.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
m.setDataSource(StringVoiceFile);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
myAudioRecorder.start();
} catch (IllegalStateException e) {
Log.e(TAG, "start() failed = " + e );
}
stop.setEnabled(true);
stop.setVisibility(View.VISIBLE);
time.setEnabled(true);
time.setVisibility(View.VISIBLE);
time.setText(formatTime(m.getDuration()));
play.setEnabled(false);
play.setVisibility(View.INVISIBLE);
long now = System.currentTimeMillis();
startRecordingTimer(now);
Alert.ToastAlert(FlipActivity.this, getResources().getString(R.string.record_start_again));
} else {
try {
myAudioRecorder.prepare();
myAudioRecorder.start();
} catch (IllegalStateException e) {
Alert.ToastAlert(FlipActivity.this, getResources().getString(R.string.record_nostart));
} catch (IOException e) {
Alert.ToastAlert(FlipActivity.this, getResources().getString(R.string.record_nostart));
}
stop.setEnabled(true);
stop.setVisibility(View.VISIBLE);
time.setVisibility(View.VISIBLE);
time.setText(formatTime(m.getDuration()));
play.setEnabled(false);
play.setVisibility(View.INVISIBLE);
long now = System.currentTimeMillis();
startRecordingTimer(now);
Alert.ToastAlert(FlipActivity.this, getResources().getString(R.string.record_start));
}
}
Please help me to solve this issue

runtimeException at mediaRecorder.start() on android at version 4.0.3

I have this code for capturing video on android. But this code throws a RuntimeException. I searched for that but I couldnt get a response. Log is:
errorrrrrrrrr runtime44444444444!!!
what line has error???
private void startRecording()
{
mCamera.unlock();
mrec = new MediaRecorder();
mrec.setCamera(mCamera);
mrec.setAudioSource(MediaRecorder.AudioSource.MIC);
mrec.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mrec.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mrec.setVideoFrameRate(24);
mrec.setVideoSize(720, 480);
Method[] methods = mrec.getClass().getMethods();
for (Method method: methods){
try
{
if(method.getName().equals("setAudioEncodingBitRate"))
{
method.invoke(mrec,12200);
}
else if(method.getName().equals("setVideoEncodingBitRate"))
{
method.invoke(mrec, 3000000);
}
else if(method.getName().equals("setAudioSamplingRate"))
{
method.invoke(mrec,8000);
}
else if(method.getName().equals("setVideoFrameRate"))
{
method.invoke(mrec,24);
}
}
catch (IllegalArgumentException e)
{
e.printStackTrace();
}
catch (IllegalAccessException e)
{
e.printStackTrace();
}
catch (InvocationTargetException e)
{
e.printStackTrace();
}
catch (RuntimeException e)
{
Log.d("errorrrrrrrrr runtime111111111", "okkkkkkkkkk");
e.printStackTrace();
}
}
mrec.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mrec.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mrec.setPreviewDisplay(surfaceHolder.getSurface());
mrec.setOrientationHint(90);
mrec.setOutputFile(Videopath);
try
{
mrec.prepare();
}
catch (IllegalStateException e)
{
// releaseRecorder();
// return false;
}
catch (IOException e)
{
// releaseRecorder();
// return false;
}
catch (RuntimeException e)
{
e.printStackTrace();
}
catch(Exception e)
{
// return false;
}
try
{
mrec.start();
}
catch (RuntimeException e)
{
Log.d("errorrrrrrrrr runtime44444444444", "okkkkkkkkkk");
e.printStackTrace();
}
catch(Exception e)
{
// return false;
Log.d("errorrrrrrrrr 33333333333", "okkkkkkkkkk");
}
isRecording=true;
// return true;
}

Categories

Resources