Android Bluetooth headset only works once - java

Good afternoon Stack Overflow. A friend of mine asked me for help with his Android application. He is trying to make a Bluetooth headset pick up his voice when a button on the headset is pressed.
The app runs fine, and when the button is pressed it properly picks up the voice. However, after one initial success when he presses the button nothing happens. His code is:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
mBluetoothAdapter.getProfileProxy(this, mHeadsetProfileListener, BluetoothProfile.HEADSET);
Intent reconocimientovoz=new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
reconocimientovoz.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
reconocimientovoz.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,1);
reconocimientovoz.putExtra(RecognizerIntent.EXTRA_PROMPT, "Escuchando");
startActivityForResult(reconocimientovoz, REQUEST_CODE);
}
He says that if he removes the following line:
mBluetoothAdapter.getProfileProxy(this, mHeadsetProfileListener, BluetoothProfile.HEADSET);
The app works fine, however it obviously uses the phone mic instead of the headset mic.
I've never worked with Bluetooth before, so honestly I don't have the slightest clue of what's wrong. Would anyone mind nudging us in the right direction?
Any and all help is greatly appreciated.

Related

Android studio - mediaPlayer

I'm doing a project on Android Studio and I've got a problem:
I have changed the volume of the media in one activity and I've tried to move the media to the second activity.
I've succeded to move the audio to the second activity but it was without any volume change...
How can I fix it?
update:
Hi, because I can't save the changes that I have made in my mediaPlayer I decided to record the song before I moving between the activitys.
I got a problem with the mediaRecorder - I can't start recording and I have no idea what is wrong in my code...
my code:
rec.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(secondRemix.this, "You are starting the recording, you need to wait a little bit until it will be ready.",
Toast.LENGTH_SHORT).show();
FILE = Environment.getExternalStorageState()+"/tempRecord.3gpp";
record.setAudioSource(MediaRecorder.AudioSource.MIC);
record.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
record.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
record.setOutputFile(FILE);
record.start();//check it!!!
song.start();
startActivity(new Intent(secondRemix.this,waitScreen.class));
record.stop();
record.release();
can anybody help me with that? –
Thank you!
Is not a properly answer but might help. Short: you can't.
Why?
The native Media Player uses canvas to render the frames and it is automatic cleaned when is not on the screen.
Workaround 1: When you start the video on the new Activity automatically start from the position that the user stopped.
Workaround 2 (Like Youtube): Use fragments to manage your view.

Android Studio startActvity requires permission

I'm attempting to make a simple click action which calls a certain number, I'm on the last stage of the code and I cannot see what I'm doing wrong. Currently its the startActivity action which seems to be presenting the error but I don't know why I have watched multiple tutorials and I can see any difference. When above startActivity it informs me that a call permission is required?
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.activity_home);
//On load the program automatically hides the taskbar.
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
Button b = (Button) this.findViewById(R.id.BTNCall);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
Intent PhoneCall = new Intent(Intent.ACTION_CALL);
PhoneCall.setData(Uri.parse("tel:123"));
startActivity(PhoneCall);
}
});
}
I have also added a permission into the android manifest
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
You should set your target SDK version to lvl 22 or under, because for lvl 23 you need to ask user at run time for permission, look there for a better explanation.
Since you are doing it from a OnClickListener, the "this" pointer references to the clickListener class you are implementing, you need to get the reference to your activity just use:
MyActivityClassName.this.startActivity() //Dont know your class name
That should get rid of the red highlight.
If you are still getting a crash we will need the logcat output to solve it.
Hope this helps.

Is that logical and a right way to add a button to close application in java?

I'm using Android Studio.
In the MainActivity inside the onCreate I did:
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startTime = SystemClock.uptimeMillis();
serverChecksThread.start();
status1 = (TextView) findViewById(R.id.textView3);
timerValue = (TextView) findViewById(R.id.timerValue);
uploadedfilescount = (TextView) findViewById(R.id.numberofuploadedFiles);
uploadedfilescount.setText("Uploaded Files: 0");
addListenerOnButton();
initTTS();
Button btn1 = (Button) findViewById(R.id.btn1);
btn1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
System.exit(0);
}
});
}
And in the activity_main.xml I added:
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="#+id/btn1"
android:text="Close App" />
I have two questions.
The main is that if it's logical and good thing to do to add a button that will close the application so it will not leave anything behind? This way when i'm running the application over again on my android device it's starting clean reseted.
The sub question is when I click the button and exit the app then when I'm running it again for a millisecond the app blink. And it happen after I added this button code it wasn't before. I'm not getting error or exception but it's blinking for a very short time.
"Running" and "closed" are fuzzy concepts in Android. When an app is in the background, it may or may not actually be running. When an activity is in the backstack, an in-memory instance of it may or may not exist. When your last activity finishes, the framework may or may not kill the process. And when you start the app again, the framework may or may not create a new instance of your Application class.
Calling System.exit(0) is a bad idea because it short-circuits the Android framework. It may result in unspecified behavior (read: really strange bugs.) Better to just finish your last activity and let the framework do as it likes.
Whether it's good UX to show a close button is a matter of opinion. Google recommends against it. The preferred way to "close" an activity is by pressing the back button.
It is not necessary that you add a close button to an application on android because, there is usually a, either software button, or b, a hardware button on the device to close (minimize) applications. so it wouldn't be a good thing to add a button, and it would also be illogical
And for your second question, I did not quite understand your point.
Even if you close your app with a button, it won't be closed permanently, it still will be shown on the users device as a 'running on background app'. Because android is not working like windows, so its not so useful to add such a button.
As far as i know the only apps that using this button is using it to be sure in 100% that the user left the app and that the connection that he had will be closed, so no one else will be able to use his login or password...

MediaPlayer / VideoView not playing video?

I'm new to android development, self taught so can expect a few errors here and there but none so irritating as this. I've looked over my code a thousand times, searched high and low across multiple websites, books and forums for an answer but I still get the same error so this is a last resort.
I just want to play a hardcoded path to a video in an activity, which is part of my video portfolio app. (The hardcoded path is just for testing, later I will call each video from the related button press, but only after I sort the player out!).
Here is my code:
String path = "android.resource://mysite/res/raw/video1";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_videoplayer);
VideoView view = (VideoView) this.findViewById(R.id.vv);
MediaController controller = (MediaController) new MediaController(this);
controller.setMediaPlayer(view);
view.setVideoPath(path);
view.requestFocus();
view.start();
}
This snippet is inside my VideoPlayer class, where vv is the VideoView in the XML and video1 is the video to be played. The video is h.264 mp4, 1 minute long and 3mb in size and can be played normally through the default player.
XML:
<VideoView
android:id="#+id/vv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
The activity loads but returns the error "Can't play this video".
Any help I greatly appreciate.
VideoView view = (VideoView) this.findViewById(R.id.vv);
view.setVideoPath(path);
view.setMediaController(new MediaController(this));
view.requestFocus();
view.start();
Use like this. hope this will give you some solution.
According Android's official documentation Video decoding support for MP4(h.264) added from android 3.0+, so I think you are playing this video on version below 3.0. Try it on the device which has android os version 3.0+.
Try this..
video0=(VideoView)findViewById(R.id.vv);
video0.setMediaController(new MediaController(this));
video0.setVideoURI(Uri.parse("android.resource://" +getPackageName()+ "/" +R.raw.video1));
video0.requestFocus();
video0.start();
Did you try
MediaController mc = new MediaController(this);
mc.setAnchorView(vv);

Android MediaPlayer will not play sound on newer APIs

I've been using this code and it plays sound just fine on Froyo and Gingerbread (and I assume Honeycomb as well as my friends have used it):
MediaPlayer mp = MediaPlayer.create(this, R.raw.click);
Button clicker = (Button) findViewById(R.id.clicker);
clicker.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
mp.start();
}
});
The audio I'm using is in WAV format. I've checked it to make sure it's not corrupted and it's fine. This code and sound file still run correctly on Gingerbread.
On Ice Cream Sandwich and JellyBean devices (a galaxy nexus and a nexus 7 respectively) this code does not work. No sound is played. There's nothing put in logcat. I've searched through the Internet and asked my friends for ideas and I can't come up with anything.
Thank you in advance for your time!
Wrap the call in an IllegalStateException, run it thru the debugger and see what you are getting. Also set a boolean isPlaying=mp.isPlaying(); and check its value. Also try a mp.reset() before starting and see it it works.
Also see http://developer.android.com/reference/android/media/MediaPlayer.html#setOnErrorListener%28android.media.MediaPlayer.OnErrorListener%29
Go ahead and inplement MediaPlayer.OnErrorListener and register the method with the media player. See what error you get.

Categories