VideoView (building my own video view) - android

private void openVideo() {
if (mUri == null || mSurfaceHolder == null) {
// not ready for playback just yet, will try again later
return;
}
// Tell the music playback service to pause
// TODO: these constants need to be published somewhere in the framework.
Intent i = new Intent("com.android.music.musicservicecommand");
i.putExtra("command", "pause");
mContext.sendBroadcast(i);
if (mMediaPlayer != null) {
mMediaPlayer.reset();
mMediaPlayer.release();
mMediaPlayer = null;
}
try {
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setOnPreparedListener(mPreparedListener);
mMediaPlayer.setOnVideoSizeChangedListener(mSizeChangedListener);
mIsPrepared = false;
Log.v(TAG, "reset duration to -1 in openVideo");
mDuration = -1;
mMediaPlayer.setOnCompletionListener(mCompletionListener);
mMediaPlayer.setOnErrorListener(mErrorListener);
mMediaPlayer.setOnBufferingUpdateListener(mBufferingUpdateListener);
mCurrentBufferPercentage = 0;
mMediaPlayer.setDataSource(mContext, mUri);
mMediaPlayer.setDisplay(mSurfaceHolder);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setScreenOnWhilePlaying(true);
mMediaPlayer.prepareAsync();
attachMediaController();
} catch (IOException ex) {
Log.w(TAG, "Unable to open content: " + mUri, ex);
return;
} catch (IllegalArgumentException ex) {
Log.w(TAG, "Unable to open content: " + mUri, ex);
return;
}
}
I got this code from GrepCode (VideoView).I would like to add Mute and unmute functions on my videoview hence am trying to create a new videoview.But due to this mContext variable i am not able to set the path for my mediaplayer.
Purpose of Mute and Unmute: To play other audios with the same audio..
I want to access the Mediaplayer for mute and unmute function and hence am not overriding my videoview class.
Am a newbie to android so suggest me some methods to achieve this. Thanks in advance.

Related

Mediaplayer is playing random songs

Im using the following code to play music.
if (mMediaPlayer.isPlaying()) {
mMediaPlayer.reset();
mMediaPlayer = new MediaPlayer();
try {
mMediaPlayer.setDataSource(MainActivity.localTrackList.get(MainActivity.currentOffset).getPath());
} catch (IOException e) {
e.printStackTrace();
}
mMediaPlayer.prepareAsync();
} else {
mMediaPlayer = new MediaPlayer();
try {
mMediaPlayer.setDataSource(MainActivity.localTrackList.get(MainActivity.currentOffset).getPath());
} catch (IOException e) {
e.printStackTrace();
}
mMediaPlayer.prepareAsync();
}
Explanation:
when the user clicks a song for the first time, else loop will be
executed.
If the user clicks a song when the mediaplayer is playing a song already, if loop will
be executed
In my if loop, Im resetting the mediaplayer before setting the data source so it calls the onCompletion listener.
Follwing is my onCompletionListener.
mMediaPlayer.setOnCompletionListener(mp -> {
MainActivity.nextTrackController.performClick();
});
Following is my nextTrackController onClickListener.
MainActivity.nextTrackController.setOnClickListener(v -> {
try {
if (MainActivity.currentOffset < (MainActivity.localTrackList.size() - 1)) {
if (mMediaPlayer.isPlaying()) {
mMediaPlayer.stop();
mMediaPlayer.reset();
} else {
mMediaPlayer.reset();
}
MainActivity.currentOffset = MainActivity.currentOffset + 1;
mMediaPlayer.setDataSource(MainActivity.localTrackList.get(MainActivity.currentOffset).getPath());
mMediaPlayer.prepareAsync();
} else {
if (mMediaPlayer.isPlaying()) {
mMediaPlayer.stop();
mMediaPlayer.reset();
}
MainActivity.currentOffset = 0;
mMediaPlayer.setDataSource(MainActivity.localTrackList.get(MainActivity.currentOffset).getPath());
mMediaPlayer.prepareAsync();
}
} catch (Exception ex) {
}
});
the problem is that,
If the media player is playing and if the user is clicking a song, it is not playing the specific song that had been clicked but it plays some random song.
If the song finished playing, it plays a random song instead of the next song. How can i be able to sort this out?

How to play Notification sound even in silent mode and return back to its stat

I'm trying to play notification sound even if silent mode is on
Uri uri = Uri.parse(alarmSound);
notification.setSound(uri);
AudioManager mobileMode = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
int previousNotificationVolume = mobileMode.getStreamVolume(AudioManager.STREAM_NOTIFICATION);
if (ignoreSilent) {
mobileMode.setStreamVolume(AudioManager.STREAM_NOTIFICATION, mobileMode.getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION), 0);
}
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(UNIQUE_ID, n);
try {
// to delay make a space to finish play sound before return back to original stat.
Thread.sleep(2000);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
mobileMode.setStreamVolume(AudioManager.STREAM_NOTIFICATION, previousNotificationVolume, 0);
This should be option that user enable or disable, I'm trying the code above but the things not going well, the sound heard "sometimes" and the phone return to the Vibration mode instead of silent, I want to handle lollipop case:
In Silent mode , timed or indefinitely.
In priority mode, timed or indefinitely.
In another world, I want something to save full stat and return it as it was.
Or, since I know how to play sound with media player, way to get the stat of phone, and if it is silent, make sound playing as a media, with max sound of media, the following how I can play sound with MediaPlayer :
public static void playSound(Context context, Uri alert) {
MediaPlayer mMediaPlayer = new MediaPlayer();
try {
mMediaPlayer.setDataSource(context, alert);
final AudioManager audioManager = (AudioManager) context
.getSystemService(Context.AUDIO_SERVICE);
if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
mMediaPlayer.prepare();
mMediaPlayer.start();
}
} catch (IOException e) {
Log.e(TAG, e.getMessage());
}
}
And then like this (but I want to know if phone in silent or vibration or priority mode) :
if (ignoreSilent) {
CoreServices.playSound(context, uri);
} else {
// else just follow normal behavior
notification.setSound(uri);
}
Finally, I prefer to solve by switching status, at least I will know how android deal things in this part, and working with API 14+.
public void playSound(){
final MediaPlayer mMediaPlayer;
Uri notification = null;
try {
notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setDataSource(getApplicationContext(), notification);
// mMediaPlayer = MediaPlayer.create(ctx, notification);
final AudioManager audioManager = (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
try {
mMediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
// mMediaPlayer.start();
mMediaPlayer.setLooping(false);
mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
public void onPrepared(MediaPlayer arg0) {
/* if (mMediaPlayer != null) {
mMediaPlayer.stop();
}*/
mMediaPlayer.seekTo(0);
mMediaPlayer.start();
}
});
mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
}
});
}catch (Exception e){
e.printStackTrace();
}
}

android MediaPlayer cannot play mp3 with 16kbps bit rate on Kitkat

i am creating an application that in it i need to play some sounds with low bit rates such as 16kbps.
I use MediaPlayer with below code. but it cannot play and i get Error(100, 0) in logcat. when i replace audio with 48kbps OGG file or 128kbps MP3 file all thing is ok. How i can play audio with 16kbps bit rate?
I have this problem only on Kitkat
player = new MediaPlayer();
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
try {
Class<?> cMediaTimeProvider = Class.forName("android.media.MediaTimeProvider");
Class<?> cSubtitleController = Class.forName("android.media.SubtitleController");
Class<?> iSubtitleControllerAnchor = Class.forName("android.media.SubtitleController$Anchor");
Class<?> iSubtitleControllerListener = Class.forName("android.media.SubtitleController$Listener");
Constructor constructor = cSubtitleController.getConstructor(new Class[]{Context.class, cMediaTimeProvider, iSubtitleControllerListener});
Object subtitleInstance = constructor.newInstance(activity, null, null);
Field f = cSubtitleController.getDeclaredField("mHandler");
f.setAccessible(true);
try {
f.set(subtitleInstance, new Handler());
} catch (IllegalAccessException ignored) {
} finally {
f.setAccessible(false);
}
Method setsubtitleanchor = player.getClass().getMethod("setSubtitleAnchor", cSubtitleController, iSubtitleControllerAnchor);
setsubtitleanchor.invoke(player, subtitleInstance, null);
//Log.e("", "subtitle is setted :p");
} catch (Exception e) {
}
}
player.setWakeMode(activity,
PowerManager.PARTIAL_WAKE_LOCK);
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
this.seekbar = seekbar;
player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
finalTime = player.getDuration();
timeElapsed = mediaPlayer.getCurrentPosition();
seekbar.setMax(finalTime);
init();
remaining.setText(miliToTime(finalTime));
elapsed.setText("0:00:00");
durationHandler.postDelayed(updateSeekBarTime, 100);
}
});
player.setOnErrorListener(this);
player.setDataSource(musicPath);
player.prepareAsync();

mediaplayer does not work android

This is the function that suppose to play some sound when a notification is set
private void playRingtone() {
AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
if (am.getRingerMode() == AudioManager.RINGER_MODE_NORMAL) {
try {
MediaPlayer mp = MediaPlayer.create(this, R.raw.notification_ringtone);
mp.start();
} catch (IllegalStateException e) {
Log.d(TAG, "MediaPlayer problem");
e.printStackTrace();
}
}
}
using the debugger everything goes smoothly but no sound is played. WHY???
MediaPlayer mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource("android.resource://YOURPACKAGE_NAME/raw/"+file anme);
mediaPlayer.prepare();
mediaPlayer.start();
}catch(Exceptions e){
}

Vitamio bug play local audio

i'm using the Vitamio bundle with success for streaming audio, but in some cases i have to play local audio. Inside MediaPlayerDemo_Audio activity the code says that in that case exists a bug if you want to play audio:
case RESOURCES_AUDIO:
/**
* TODO: Upload a audio file to res/raw folder and provide its resid in
* MediaPlayer.create() method.
*/
//Bug need fixed
mMediaPlayer = createMediaPlayer(this, R.raw.test_cbr);
mMediaPlayer.start();
}
public MediaPlayer createMediaPlayer(Context context, int resid) {
try {
AssetFileDescriptor afd = context.getResources().openRawResourceFd(resid);
MediaPlayer mp = new MediaPlayer(context);
mp.setDataSource(afd.getFileDescriptor());
afd.close();
mp.prepare();
return mp;
} catch (IOException ex) {
Log.d(TAG, "create failed:", ex);
// fall through
} catch (IllegalArgumentException ex) {
Log.d(TAG, "create failed:", ex);
// fall through
} catch (SecurityException ex) {
Log.d(TAG, "create failed:", ex);
// fall through
}
return null;
}
The createMediaPlayer method crash when execute mMediaplayer.prepare() method and neither exists the Mediaplayer.create() method, so... What should i do? Thx in advance
Try adding this permission in the manifest
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Categories

Resources