I am using JMF for playing mp3 Audio file.It is playing but I have no idea how to control its volume? I am using
Player audioPlayer = Manager.createRealizedPlayer(url);
audioPlayer.start();
See Player.getGainControl() which:
Gets the object for controlling this Player's audio gain. If this player does not have a GainControl, getGainControl returns null. For example, getGainControl might return null if the Player does not play audio data.
On GainControl:
GainControl is an interface for manipulating audio signal gain.
Gain and Gain Measures
Gain is a multiplicative value applied to an audio signal that modifies the amplitude of the signal. This interface allows the gain to be specified in either decibels or using a floating point value that varies between 0.0 and 1.0.
I did it on Android in this fashion.
audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
final int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
int curVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
SeekBar volumecontrol = (SeekBar) findViewById(R.id.seekBar);
volumecontrol.setMax(maxVolume);
volumecontrol.setProgress(curVolume);
volumecontrol.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromuser) {
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,progress, 0);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
Related
I have the following code to get the x and y angle of my device and it is working fine on my phone, but not my tablet (Samsung galaxy tab e). I was wondering if anyone had any idea as to what could be causing it to work on one device but not another.
I did also ensure that screen rotation was enabled on both. My assumption is that the tablet is lacking a sensor, and what I'm looking for most is a workaround. Any help would be much appreciated. Thanks in advance!
Source code:
double yAngle;
double xAngle;
#Override
protected void onResume() {
super.onResume();
sensorManager.unregisterListener(this);
sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR),RATE);
}
#Override
public void onSensorChanged(SensorEvent event) {
float[] rotationMatrix;
rotationMatrix = new float[16];
SensorManager.getRotationMatrixFromVector(rotationMatrix, event.values);
determineOrientation(rotationMatrix);
lblY.setText(String.format("%.1f", yAngle));
lblX.setText(String.format("%.1f", xAngle));
}
private void determineOrientation(float[] rotationMatrix){
//CREATING FLOAT ARRAY OF ORIENTATION VALUES
float [] orientationValues = new float[3];
SensorManager.getOrientation(rotationMatrix, orientationValues);
yAngle = Math.toDegrees(orientationValues[2]);
xAngle = Math.toDegrees(orientationValues[1]);
}
You can use adb shell pm list features to check all sensors and other features supported.
I have problems about how to implement PlaybackParams to set video speed:
public PlaybackParams getPlaybackParams ()
Added in API level 23
Gets the playback rate using PlaybackParams.
PlaybackParams setSpeed (float speed) //Sets the speed factor.
Returns:
the playback rate being used.
Throws IllegalStateException:
if the internal sync engine or the audio track has not been initialized.
This is my code:
mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener()
{
#Override
public void onPrepared(MediaPlayer mp)
{
mp.setPlaybackParams(new PlaybackParams().setSpeed(1.f));
if (mPlaybackState == PlaybackState.PLAYING) { mVideoView.start();}
}
});
You are getting an IllegalStateException while calling the 'setPlayParams' method, because you're not doing PlaybackParams params = mp.getPlaybackParams(), set the speed and then pass it to mp.setPlaybackParams()! Set the speed DIRECTLY while calling the mp.getPlayParams()!
MediaPlayer mp = ...;
float speed = 0.55f;
mp.setPlaybackParams(mp.getPlaybackParams().setSpeed(speed));
After many try i find a solution.
Example how use VideoView
final VideoView mVideoView = findViewById(R.id.videoView);
mVideoView.setVideoPath(Environment.getExternalStorageDirectory() + "/bluetooth/test.webm"); //Path of your file video
mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener()
{
#Override
public void onPrepared(MediaPlayer mp)
{
mp.setPlaybackParams(mp.getPlaybackParams().setSpeed(0.55f));
mVideoView.start();
}
});
MediaController media = new MediaController(this); //this is for play and restart play manually
media.setAnchorView(mVideoView);
mVideoView.setMediaController(media);
//mVideoView.start();
You have not started the Media Playe..
Try This Code:
#Override
public void onPrepared(MediaPlayer mp)
{
mp.start()
mp.setPlaybackParams(new PlaybackParams().setSpeed(1.f));
}
I am trying to set the max for my seekbar but it still defaults to 100.
XML
<SeekBar
android:id="#+id/seekBarRow1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="5"
android:progress="0"
/>
Defining and setting max
final SeekBar seekBarRow1 = (SeekBar)findViewById(R.id.seekBarRow1);
seekBarRow1.setMax(50);
Don't know if it is effecting it but here is my listener
seekBarRow1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
{
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
progress = progress * 2;
numWeightsRow1 = progress;
numWeightsRow1 = numWeightsRow1 / 2;
numRow1.setText("" + progress);
updateWeight();
}
});
I have tried adding to my seekbar in xml but it still defaults to 100.
android:max="50"
Can you please tell, For What purpose you want to use SeekBar?
I mean, do you want to use SeekBar with MediaPlayer or with any other widget?
Suppose if you want to use SeekBar with MediaPlayer, then below is the best example for SeekBar with SeekBarChangeListener
Seekbar: Android Online/Local Audio Media Player with Play/Pause, Rewind, Forward, Previous and Next functionality with Seekbar
I have a service that uses MediaPlayer to stream internet radio:
public class MediaPlayerService extends Service implements
MediaPlayer.OnPreparedListener,
MediaPlayer.OnBufferingUpdateListener {
private MediaPlayer mMediaPlayer;
private WifiLock mWifiLock;
public int onStartCommand(Intent intent, int flags, int startId) {
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setOnBufferingUpdateListener(this);
mMediaPlayer.setOnPreparedListener(this);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setDataSource(MY_URL);
// Acquire CPU lock and wi-fi lock
mMediaPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
mWifiLock = ((WifiManager) getSystemService(Context.WIFI_SERVICE))
.createWifiLock(WifiManager.WIFI_MODE_FULL, "Media Player Wi-Fi Lock");
mWifiLock.acquire();
mMediaPlayer.prepareAsync();
return START_STICKY;
}
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
mMediaPlayer.start();
}
#Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
Log.d("Buffered " + percent);
}
}
Everything works great except onBufferingUpdate(...) method. It prints this in the log:
Buffered -819200000
Buffered -819200000
Buffered -1228800000
Buffered -1228800000
Buffered -1228800000
Buffered -1228800000
Buffered -1638400000
Buffered -1638400000
Buffered -1638400000
Buffered -1638400000
Buffered -2048000000
Buffered -2048000000
Buffered -2048000000
Buffered -2048000000
Buffered -2147483648 (repeated further on)
Note: -2147483648 is Integer MIN_VALUE
While percent is supposed to be:
The percentage (0-100) of the content that has been buffered or played
thus far
Questions:
Why incorrect values are passed in onBufferingUpdate() percent parameter?
How to fix that?
Is there any other way to get how much data has been buffered? My goal is to show a progress bar to give user an idea of when enough data will be buffered and radio playback will start.
Thank you!
I am not sure why is this happening, I had the same problem, but this code may help you:
#Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
// TODO Auto-generated method stub
if (percent < 0 || percent > 100) {
//System.out.println("Doing math: (" + (Math.abs(percent)-1)*100.0 + " / " +Integer.MAX_VALUE+ ")" );
percent = (int) Math.round((((Math.abs(percent)-1)*100.0/Integer.MAX_VALUE)));
}
progressBar.setProgress(percent);
Log.i("Buffering! " , " " +percent );
System.out.println("Buffering: " +percent);
}
public void onBufferingUpdate(MediaPlayer mp, int percent)
{
double ratio = percent / 100.0;
bufferingLevel = (int)(mp.getDuration() * ratio);
seekBar.setSecondaryProgress(bufferingLevel);
}
And somewhere upper in OnCreateView OR OnCreate
seekBar.setMax(GET_TOTAL_DURATION_OF_SONG);
The problem is in Android Player Driver, point is in signed/unsigned values. Here is related issue.
To solve it on JVM level you can use this code:
percent &= 0xFF;
// media broadcasts may return values more than 100
if (percent > 100)
percent = 0;
I'm playing a video (from youtube) using a VideoView, like this:
VideoView video = (ViewView) this.findViewById(R.id.youtube_video);
MediaController controllers = new MediaController(this);
controllers.setAnchorView(video);
video.setMediaController(controllers);
video.setVideoURI(videoUri);
video.start()
I would like to be able to mute the video, but so far the only thing I found is:
AudioManager audioManager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamMute(AudioManager.STREAM_MUSIC, true);
Which works fine in that the video is muted, but the problem is that all of the streamed music is muted, and if there's another video (or audio) which I want to play it's also muted.
Is there a way to just target the specific stream to be muted?
Thanks.
After digging into every possible source of information I managed to find, I came up with the following solution and thought it might benefit others in the future:
public class Player extends VideoView implements OnPreparedListener, OnCompletionListener, OnErrorListener {
private MediaPlayer mediaPlayer;
public Player(Context context, AttributeSet attributes) {
super(context, attributes);
this.setOnPreparedListener(this);
this.setOnCompletionListener(this);
this.setOnErrorListener(this);
}
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
this.mediaPlayer = mediaPlayer;
}
#Override
public boolean onError(MediaPlayer mediaPlayer, int what, int extra) { ... }
#Override
public void onCompletion(MediaPlayer mediaPlayer) { ... }
public void mute() {
this.setVolume(0);
}
public void unmute() {
this.setVolume(100);
}
private void setVolume(int amount) {
final int max = 100;
final double numerator = max - amount > 0 ? Math.log(max - amount) : 0;
final float volume = (float) (1 - (numerator / Math.log(max)));
this.mediaPlayer.setVolume(volume, volume);
}
}
It seems to working well for me, acting just like a VideoView with mute/unmute functionality.
It's possible to make the setVolume method public so that volume can be controlled outside of the scope of the class, but I just needed mute/unmute to be exposed.
put this code in oncreate() and also in onresume() for handle video view in better way...
VideoView videoview = (VideoView) findViewById(R.id.videoview);
videoview.setVideoPath(videopath);
videoview.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.setVolume(0f, 0f);
mp.setLooping(true);
}
});
videoview.start();
If you are coding in kotlin, Just use this line of code
videoView.setOnPreparedListener { video->
video.setVolume(0f,0f)
}
I have a simpler method, by doing this you will have a mute/unmute button (this can be used for audio and videos);
//set global variable
//private Button mMuteButton;
//private static int aux = 0;
//private AudioManager mAudioManager;
//set the Mute button on clickListener
mMuteButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
mAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
if(aux % 2 == 0){
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 100, 0);
aux++;
} else {
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, 0);
aux++;
}
}
});
}