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.
Related
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.
I've simple Android app that record a call and it works fine on Android 6 + Samsung devices (both with MIC source and VOICE_COMMUNICATION). But once Samsung devices updated to Nougat (for ex. S7 / S7 Edge) these methods to record a call failed :( MIC record only my voice but not opponent voice and VOICE_COMMUNICATION does not works at all.
Could anybody advice what can be done here?
same problem.
still looking for the answer.
I've got a samsung s7. Firstly, I'v unlocked native recorder (in dialer) and it's works.
So, android 7 (and samsung) can record calls without inner actions inside of Android.
But, in the same time I've read that developers can't get the access to recording calls from higher level.
It seems like there's only one way to achieve our goals: modification of stock Android's version or catch sound stream before it gets to speaker.
Fix me if I wrong.
Normally for recording phone call, we can use 4 types: DEFAULT, MIC, VOICE_CALL, VOICE_COMMUNICATION. But with 3 types below is enough for all
MIC, VOICE_CALL, VOICE_COMMUNICATION
base on the android version of your phone that will support or don't support some of them.
So to make your app work perfectly with all android versions we should change the AudioSource type following the android version with this rule:
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);
callType = "VOICE_CALL";
} else if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
callType = "MIC";
} else {
recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_COMMUNICATION);
callType = "VOICE_COMMUNICATION";
}
I am using this on my application and it worked on most devices. It's no need to add any C library.
Check my example application to see how they react with a dedicated Android version.
Github: https://github.com/tntkhang/call-recording-master
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.
I've tried overriding onPause and everything but the mp3 file continues to play even after the home button is pressed.
I have logged the onPause method and it does seem to be working, however it is not stopping the media player.
I believe that when the home button is pressed the object in which my mediaplayer is held in is set to NULL. Is there anyway to stop the music when the home button is pressed?
User870380 is correct, the home key cannot be overridden, and for good reason. The MediaPlayer is quite erratic and buggy, so seeing your exact code will be very useful. Your code will probably look something like this:
#Override
protected void onPause() {
if (mediaPlayer != null) {
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = null;
}
}
You may also need to check to see if it throws an IllegalStateException. The MediaPlayer does that. A lot.
Are you getting any errors in your logcat when you try to stop the MediaPlayer?
Are you running the MediaPlayer in a service?
Is this even possible? I have tried using the MediaPlayer but it throws a NullPointerException on the MediaPlayer object. I can get audio to work but video wont.
mp=MediaPlayer.create(getApplicationContext(), R.raw.sample);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener()
{
public void onCompletion(MediaPlayer mp) {
mp.release();
playing = false;
}
});
the sample is of .mp4 type.
Anyone have an idea of why this is happening or have a suggestion for another method of getting videos to be played?
You can use the following code
VideoView videoView;
VideoView = (VideoView) findViewById (R.id.txt1);
videoView.setVideoPath(path);
videoView.setVisibility(VideoView.VISIBLE);
videoView.start();
i have tried to play mp4 on my emulator but it was not showing video but when i tried on device it work fine.
Haven't tried that before but I think you can use vlcj framework that's totally free and can play effectively almost any type of video (and of course plays .mp4 video files) .I can't give you any code in android because have never worked with android but I know java and and it just works.So here what i use in Java:
NativeLibrary.addSearchPath("libvlc",path); //To set path of libvlc
Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class);//To import libvlc
//The path can be a folder in your android project.All the files needed are in vlc player installation folder.so yes you have to install vlc in your computer to get those files but just once.
canvas = new WindowsCanvas();
panel.add(canvas);//panel is like your VideoView
canvas.setVisible(true);
canvas.setBackground(Color.black);
mediaPlayerFactory = new MediaPlayerFactory();
player12 = mediaPlayerFactory.newEmbeddedMediaPlayer();
CanvasVideoSurface videoSurface = mediaPlayerFactory.newVideoSurface(canvas);
player12.setVideoSurface(videoSurface);
player12.setPlaySubItems(true);
player12.startMedia(yourVideoPath);
player12.setAspectRatio(""+panel.getWidth()+":"+panel.getHeight()); //Those two lines are for your video to be adusted in your panel or better to your VideoView
player12.setCropGeometry(""+panel.getWidth()+":"+panel.getHeight());
The jar files you have to include in your classpath are jna-3.4.0.jar,platform-3.4.0.jar,vlcj-2.1.0.jar