Hi I have this simple code on Android Wear I need a bit of help everything works well if I'm using the phone alone the problem I have is that I taught Android wear will use automatically the Phone hardware for sound and all that good stuff.
Here is my test code
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
#Override
public void onLayoutInflated(WatchViewStub stub) {
imageButtonOne = (ImageButton) findViewById(R.id.imageButtonOne);
textviewOne = (TextView) findViewById(R.id.textviewOne);
imageButtonOne.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textviewOne.setText("Hello World!");
soundOne() ;
}
});
}
public void soundOne() {
try {
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone one = RingtoneManager.getRingtone(getApplicationContext(), notification);
one.play();
} catch (Exception e) {
e.printStackTrace();
}
// MediaPlayer media = MediaPlayer.create(this, R.raw.sound_file_1);
// media.start() ;
// Toolkit.getDefaultToolkit().beep() ;
}
});
It call the sound but because watches doesn't have speakers won't sound. I want the phone to make the sound. Please if someone can help me will be appreciated.
While Android Wear devices will play sound if a bluetooth headset is attached, if you need sound to play on the phone you need to use the Wearable Data Layer and probably more specifically the Message API to send a message to your phone app. Your phone app would implement a WearableListenerService which would be triggered by the incoming message and would play the sound on the phone.
Note: it is much more common for Wear apps to vibrate (via the Vibrator to draw attention rather than play sounds on the phone. Many Wear users use the 'Mute connected phone' option to effectively silence their phone so a sound coming from it may be quite unexpected.
Related
I'm creating a live streaming (that uses exomedia), with admin panel but i dont know in some devices the live stream does not open its stays black no mediaplayer controls nothing shows up just a black page, in other devices it works fine, live stream starts as it should, the mediaplayer control shows up, (I tried this in two Samsung s8 phones, in one phone it worked just fine, in a other phone it stays black.
i have done some test in in android api24(Android 7.0) is working just fine in api28(Android 9) the channel does not play it stays black and nothing shows up
JAVA
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_streaming);
url = getIntent().getStringExtra("url");
videoView = (VideoView) findViewById(R.id.video_view);
videoView.setOnPreparedListener(this);
Uri videoUri = Uri.parse(url);
videoView.setVideoURI(videoUri);
}
#Override
public void onPrepared() {
//Starts the video playback as soon as it is ready
videoView.start();
}
public void onBackPressed() {
super.onBackPressed();
}
public void closeStreaming() {
if ((System.currentTimeMillis() - exitTime) > 2000) {
Toast.makeText(this, getString(R.string.press_again_to_close_streaming), Toast.LENGTH_SHORT).show();
exitTime = System.currentTimeMillis();
} else {
finish();
}
}
}
I want to pause media player in case the user use another program to watch video
or the user receive a phone call like any multimedia app (YouTube ,...)
MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.song);
mediaPlayer.start();
thanks
You need to override android methods which relate to the lifecycle of an android application
#Override
protected void onStop() {
super.onStop();
mediaPlayer.stop(); //put your stop criteria here
}
#Override
public void onPause() {
super.onPause();
mediaPlayer.pause(); //put your pause criteria here
}
#Override
public void onResume() {
super.onResume();
mediaPlayer.play(); //put your resume criteria here
}
you can simply override these methods to do your job.
MediaPlayer can be implemented as a service. Pause the service when the event happens using the BroadcastReceiver and you can again resume it using that. It would work in this manner.
You have to use AudioFocus (https://developer.android.com/guide/topics/media-apps/audio-focus). In this way you will receive an Event when another App is taking "the ownership" of Audio Device to play its content/audio and then another Event will be raised when that App stops to play it and the "ownership" return to your App.
Trying something pretty out of the box... I have a simple app with a button that when pushed, plays music out of the audio jack of my android tablet.
public void btn1 (View view) {
MediaPlayer mp = MediaPlayer.create(this, R.raw.xxx);
mp.start();
}
I've now added a usb audio interface (through a micro usb adapter) and I can hear audio out of it.
I'm able to list the sound cards with this
AudioDeviceInfo[] devices = audioManager.getDevices(AudioManager.GET_DEVICES_OUTPUTS);
for (AudioDeviceInfo device : devices) {
int b = device.getId();
int d = device.getType();
CharSequence productName = device.getProductName();
}
How do I route music so that I can play 2 different music at once, one through usb and the other through the headphone jack?
According to the MediaPlayer documentation, you can set the audio device using setPreferredDevice which receive an AudioDeviceInfo as a parameter, see https://developer.android.com/reference/android/media/MediaPlayer.html#setPreferredDevice(android.media.AudioDeviceInfo).
You will then have to create one MediaPlayer to play on each device.
it works about like this:
protected void playAudio() {
this.playByDeviceIdx(0, R.raw.xxx);
this.playByDeviceIdx(1, R.raw.yyy);
}
protected void playByDeviceIdx(int deviceIndex, #IdRes int resId) {
/* obtain audio-output device-infos */
deviceInfos[] devices = audioManager.getDevices(AudioManager.GET_DEVICES_OUTPUTS);
/* check, if the desired index is even within bounds */
if(deviceInfos.length < deviceIndex) {
/* create an instance of MediaPlayer */
MediaPlayer mp = MediaPlayer.create(this, resId);
/* assign a preferred device to the MediaPlayer instance */
mp.setPreferredDevice(deviceInfos[deviceIndex]);
/* start the playback (only if a device exists at the index) */
mp.start();
}
}
you could also filter for the headset jack plug/unplug event:
IntentFilter intentFilter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
Intent intent = context.registerReceiver(null, intentFilter);
boolean isConnected = intent.getIntExtra("state", 0) == 1;
sources: me, based upon the SDK documentation for the MediaPlayer.
I am using Java and xml to create an application for the summer. This app has music in the background, that I would like to pause and then play depending on the state it is in. When i lock my screen the music does not pause it keeps playing. How would i fix this, I have tried to use the method on this site:
https://thinkandroid.wordpress.com/2010/01/24/handling-screen-off-and-screen-on-intents
I have not been able to successfully get a working prototype, and I am working in android studio.
myPlayer = MediaPlayer.create(c(this is the context), myField(This is the raw file));
myPlayer.start();
myPlayer.setLooping(true);
myPlayer.setVolume(0.7f,0.7f);
What could I add to pause the mediaplayer when the lock button is pressed or the phone goes to sleep and then play it when the phone is unlocked?
#Override
protected void onPause() {
super.onPause();
if (myPlayer != null) {
myPlayer.pause();
}
}
#Override
protected void onResume() {
super.onResume();
if (myPlayer != null) {
myPlayer.start();
}
}
I'm playing video using VideoView in portrait mode (not on full screen). When I change to landscape mode, the video not resume (like youtube). Any Suggestion? Here is my code.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Checks the orientation of the screen
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_box_office_detail);
final ProgressDialog ringProgressDialog = ProgressDialog.show(BoxOfficeDetailActivity.this, "Please wait ...", "Video now loading ...", true);
ringProgressDialog.setCancelable(true);
new Thread(new Runnable() {
#Override
public void run() {
try {
Thread.sleep(15000);
} catch (Exception e) {
}
ringProgressDialog.dismiss();
}
}).start();
videoView =(VideoView) findViewById(R.id.tvVideo);
tvTitle = (TextView) findViewById(R.id.tvTitle);
tvSynopsis = (TextView) findViewById(R.id.tvSynopsis);
tvCast = (TextView) findViewById(R.id.tvCast);
BoxOfficeMovie movie = (BoxOfficeMovie) getIntent().getSerializableExtra(BoxOfficeActivity.MOVIE_DETAIL_KEY);
loadMovie(movie);
}
public void loadMovie(BoxOfficeMovie movie) {
videoView.setVideoPath(movie.getVideo());
MediaController mediaController = new
MediaController(this);
mediaController.setAnchorView(videoView);
videoView.setMediaController(mediaController);
videoView.start();
videoView.requestFocus();
tvTitle.setText(movie.getTitle());
tvCast.setText(movie.getCastList() + " views");
tvSynopsis.setText(movie.getSynopsis());
}
}
Avoid activity destruction, by adding this for the activity in the manifest android:configChanges="orientation|uiMode|screenSize"
Well odds are that on Orientation change you will need to destroy your video view and then reinstate it after the orientation change and resume playback from where it was left off at.
A little side note, I have had nothing but problems working with VideoView and MediaPlayer in Android development. Especially getting those 2 things to work correctly across multiple devices. If I were you, I would look into the ExoPlayer library to support video playback. But I guess that depends on how big of a part of your app video playback will be. ExoPlayer takes a significant more amount of time to get setup and working correctly however it still isnt hard and the performance and stability improvement make it work it.