Cannot able to access MediaController Field using Reflection - android

I am trying to change Visibility of FastForward Button present in MediaController Class using concept of Reflection.
Below is my code snippet.
package com.example.reflection;
import java.lang.reflect.Field;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.ImageButton;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;
public class MainActivity extends Activity {
private VideoView videoView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Class <? > aClass;
try {
MediaController controller = new MediaController(this);
aClass = Class.forName("android.widget.MediaController");
Field forwardButton = aClass.getDeclaredField("mFfwdButton");
forwardButton.setAccessible(true);
NPE--->> ImageButton button = (ImageButton) forwardButton.get(controller);
if (null == button) Toast.makeText(this, "Button is null", Toast.LENGTH_LONG)
.show();
else button.setVisibility(View.INVISIBLE);
videoView = (VideoView) findViewById(R.id.videoView);
videoView.setVideoPath(Environment.getExternalStorageDirectory() + "/Video/NeYo.flv");
videoView.setMediaController(controller);
videoView.requestFocus();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
I get NullPointerException while fetching ImageButton.
Can somebody please point out the error I am making?
Thanks
Anyways I got the answer for this question.
mFfwdButton is instantiated only when the View is inflated. Thats why I was getting a null. I was trying to get the variable even before MediaController has been attached to VideoView.
I followed following approach and it worked.It may not be best practice in production environment,but below snippet worked for me.
#Override
protected void onResume() {
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
ImageButton button;
try {
button = (ImageButton) forwardButton.get(controller);
if (null == button) Toast.makeText(MainActivity.this, "Button is null",
Toast.LENGTH_LONG).show();
else button.setVisibility(View.INVISIBLE);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}, 50);
super.onResume();
}

mFfwdButton is instantiated only when the View is inflated. So you are getting a null. So i guess you cannot use it until the activity has been shown to the user. You are trying to get the variable even before mediacontroller has been attached to VideoView
Try posting a runnable with a delay in OnResume

Related

How to play music in Thread on Android?

I am trying to write code in Android to play music. But i am starting the music in a thread so music is breaking and i need to play the music in thread because i have receive the music name through a Bluetooth socket.
So , the thread will keep listening the socket , to get the updated Music file name. But while playing the music it's breaking and not playing properly.
MyCode:
package com.example.musicexample;
import java.io.IOException;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
Button btnPlay;
MediaPlayer mPlayer;
Thread canThread;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnPlay = (Button) findViewById(R.id.btnPlay);
mPlayer = MediaPlayer.create(getApplicationContext(), R.raw.a);
btnPlay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
canThread = new Thread(new Runnable() {
#Override
public void run() {
while(!Thread.currentThread().isInterrupted()) {
MainActivity.this.runOnUiThread(new Runnable()
{
public void run()
{
if(mPlayer.isPlaying()){
mPlayer.stop();
mPlayer.reset();
try {
mPlayer.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mPlayer = MediaPlayer.create(getApplicationContext(), R.raw.a);
}
mPlayer.start();
}
});
}
}
});
canThread.start();
}
});
} }
This is the above code , please let me know , how can i play the music properly in a Thread?

Streaming Audio Android app doesn't works on Galaxy S4

I've created an app (radio streaming) but doesn't works on S4.
I've tested this app on my Galaxy Nexus, Xperia Arc s, Htc Desire and it works properly.
I think there is an error in my code.
My code:
package com.dieesoft.radiolluvia;
import java.io.IOException;
import android.R.array;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.media.MediaPlayer.TrackInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
private MediaPlayer mp;
private ProgressDialog pb;
private Button bplay;
private Button bstop;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bplay = (Button) findViewById(R.id.button1);
bstop = (Button) findViewById(R.id.button2);
mp = new MediaPlayer();
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
bplay.setOnClickListener(this);
bstop.setOnClickListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.getId() == R.id.button1 )
{
//button play
new iniciarStreaming().execute();
Toast.makeText(this, "Play", Toast.LENGTH_SHORT).show();
}
else if(v.getId() == R.id.button2)
{
//button stop
Toast.makeText(this, "Stop", Toast.LENGTH_SHORT).show();
mp.stop();
}
}
private class iniciarStreaming extends AsyncTask<Void, Void, Boolean> implements OnPreparedListener
{
#Override
protected Boolean doInBackground(Void... params) {
// TODO Auto-generated method stub
try {
mp.setDataSource("http://makrodigital.com:8134/radiolluvia");
} 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();
}
mp.prepareAsync();
mp.setOnPreparedListener(this);
return null;
}
protected void onPreExecute() {
// TODO Auto-generated method stub
pb = new ProgressDialog(MainActivity.this);
pb.setMessage("Buffering...");
pb.show();
}
#Override
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
if (pb.isShowing()) {
pb.cancel();
}
mp.start();
}
}
Your streaming url (http://makrodigital.com:8134/radiolluvia) has AAC format with content-type: audio/aacp.
But audio/aacp streaming is not supported directly. Maybe previously was another link? MP3 or something else?
For playing this url you can use this library:
http://code.google.com/p/aacplayer-android/

Android radio streaming, the steam link doesnt play

I've followed up zvzej's post on Online radio streaming app for Android
I am encountering 2 problems:
1st.) The stream link don't play, though buttonPlay.enabled(false) does work, I also don't see the progressbar, eventhough onClick of Play it should be visible. Does this mean the script cant go beyond
playSeekBar.setVisibility(View.VISIBLE);
in startPlaying() ?
2nd.) After clicking Play, then stop, then back to play, the application force crashes, sometimes it requires more clicks, sometimes fewer.
Code:
package com.afghan.radios;
import android.app.Activity;
import android.os.Bundle;
import java.io.IOException;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
public class ArmanFMRadio extends Activity implements OnClickListener {
private ProgressBar playSeekBar;
private Button buttonPlay;
private Button buttonStopPlay;
private MediaPlayer player;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.armanfm);
initializeUIElements();
initializeMediaPlayer();
}
private void initializeUIElements() {
playSeekBar = (ProgressBar) findViewById(R.id.progressBar1);
playSeekBar.setMax(100);
playSeekBar.setVisibility(View.INVISIBLE);
buttonPlay = (Button) findViewById(R.id.buttonPlay);
buttonPlay.setOnClickListener(this);
buttonStopPlay = (Button) findViewById(R.id.buttonStopPlay);
buttonStopPlay.setEnabled(false);
buttonStopPlay.setOnClickListener(this);
}
private void initializeMediaPlayer() {
player = new MediaPlayer();
try {
player.setDataSource("http://usa8-vn.mixstream.net:8138");
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
player.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {
public void onBufferingUpdate(MediaPlayer mp, int percent) {
playSeekBar.setVisibility(View.VISIBLE);
playSeekBar.setSecondaryProgress(percent);
Log.i("Buffering", "" + percent);
}
});
}
public void onClick(View v) {
if (v == buttonPlay) {
startPlaying();
} else if (v == buttonStopPlay) {
stopPlaying();
}
}
private void startPlaying() {
buttonStopPlay.setEnabled(true);
buttonPlay.setEnabled(false);
playSeekBar.setVisibility(View.VISIBLE);
player.prepareAsync();
player.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
try {
player.prepare();
player.start();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
private void stopPlaying() {
if (player.isPlaying()) {
player.stop();
player.release();
initializeMediaPlayer();
}
buttonPlay.setEnabled(true);
buttonStopPlay.setEnabled(false);
playSeekBar.setVisibility(View.INVISIBLE);
}
#Override
protected void onPause() {
super.onPause();
if (player.isPlaying()) {
player.stop();
}
}
}

Android App Play sound when holding down a button

new android dev hopeful here. I've been attempting to implement an app that, upon holding down the "ok" button, it will play the default ringtone in a loop.
What I have so far
package com.tick.helloAndroid;
import net.technologichron.android.control.NumberPicker;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
//import android.widget.TextView;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.Toast;
public class main extends Activity implements OnTouchListener {
private int REQUEST_ENABLE_BT = 1;
private MediaPlayer mp;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set up the window layout
setContentView(R.layout.main);
final Button zero = (Button) this.findViewById(R.id.ok);
zero.setOnTouchListener(this);
Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
mp.setDataSource(this, alert);
}
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mp.setLooping(true);
mp.start();
case MotionEvent.ACTION_UP:
mp.pause();
}
return true;
}
}
From doing some searching on SO, this seems to work for some people, however, the line
mp.setDataSource(this, alert);
seems to not work. By not work, I mean that it forces an "Unhandled Exception Error", which upon doing a try-catch statement,
try {
mp.setDataSource(this, alert);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
Toast.makeText(this, "args", Toast.LENGTH_LONG).show();
finish();
return;
} catch (SecurityException e) {
// TODO Auto-generated catch block
Toast.makeText(this, "sec", Toast.LENGTH_LONG).show();
finish();
return;
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
Toast.makeText(this, "illega", Toast.LENGTH_LONG).show();
finish();
return;
} catch (IOException e) {
// TODO Auto-generated catch block
Toast.makeText(this, "io", Toast.LENGTH_LONG).show();
finish();
return;
}
compiles without any error but crashes when I try to run the app on the android emulator, it does not print any of the above catches' strings.
Any thoughts and ideas about what I have done incorrectly will be most appreciated.
edit: could it be from the fact that the emu does not have a ringtone?
forgot to instantiate mp....hahahahha

Play mp3 file from raw resource on click of a TextView

I want to play a certain mp3 file when a text is clicked. For example, I clicked the word "Nicholas", the app have to play nicholas.mp3.
Sorry for my messy code, I'm new to android dev:
package com.example.playword;
import java.io.IOException;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
//import android.os.Handler;
import android.view.View;
//import android.view.View.OnClickListener;
//import android.widget.Button;
import android.widget.TextView;
public class PlayWord extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//final Handler mHandler = new Handler();
final TextView nicholas = (TextView) findViewById(R.id.nicholas);
final TextView was = (TextView) findViewById(R.id.was);
nicholas.setText("Nicholas ");
was.setText("was ");
/*
Button btn = (Button) (findViewById(R.id.nicholasBtn));
btn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
nicholas.setText("Nicholas (Clicked!) ");
}
});
*/
View.OnClickListener handler = new View.OnClickListener(){
public void onClick(View v) {
switch (v.getId()) {
case R.id.nicholas: // doStuff
MediaPlayer mPlayer = MediaPlayer.create(null, R.raw.aaanicholas);
try {
mPlayer.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mPlayer.start();
nicholas.setText("Nicholas (Clicked!) ");
break;
case R.id.was: // doStuff
MediaPlayer mPlayer1 = MediaPlayer.create(null, R.raw.aaawas);
try {
mPlayer1.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mPlayer1.start();
was.setText("was (Clicked!) ");
break;
}
}
};
findViewById(R.id.nicholas).setOnClickListener(handler);
findViewById(R.id.was).setOnClickListener(handler);
}
}
When I run this, I'm getting a force close error. Do you have a much better idea on this?
You need to pass in a context instance into MediaPlayer.create method:
MediaPlayer mPlayer = MediaPlayer.create(PlayWorld.this, R.raw.aaanicholas);
Also, after the create() call, prepare is already executed, so you don't need to execute it explicitly, just invoke start() right after create().
When you create the mPlayer object you should pass it the Context, which is in your case PlayWord.this.
MediaPlayer mPlayer = MediaPlayer.create(FakeCallScreen.this, R.raw.mysoundfile);
mPlayer.start();
val mediaPlayer = MediaPlayer.create(this,R.raw.music)
mediaPlayer.isLooping=true
mediaPlayer.start()
music_on_off_button.setOnClickListener {
if (mediaPlayer.isPlaying){
mediaPlayer.pause()
}else{
mediaPlayer.start()
}
}
You can detect weather the music is playing or not by using isPlaying() method

Categories

Resources