I've been working on an MP3 player in Android Studio 2.3.3, but I haven't been able to make the MediaPlayer class work. I've uploaded some MP3 files on my virtual Android device (Android 7.0), but I cannot set a correct path to any of them. What I do is pretty simple:
try{
MediaPlayer mediaPlayer = MediaPlayer.create(MainActivity.this, Uri.parse("/storage/emulated/0/03. Bloom.mp3"));
mediaPlayer.prepare();
mediaPlayer.start();
} catch (Exception ex){}
But it doesn't want to work. I've tried also
MediaPlayer mediaPlayer = MediaPlayer.create(MainActivity.this, Uri.parse("file:///storage/emulated/0/03. Bloom.mp3"));
Neither of those options work. I'm sure the song is in this folder (proof pic).
I would really appreciate any help, thank you.
Related
I can play a sound using Mediaplayer class in java android.
I make a Mediaplayer object like this.
Mediaplayer mp=Mediaplayer.create(this,R.raw.mysong);
And then start it but what if i have more than one song and have to play it. Do I need to make objects for everyone of them. What i am trying to say is does Mediaplayer class have any kind of method or thing that i can clear the old song and put new song for playing?
Hope you get what i say
I could really appreciate it with a simple example.
Create only one object as you've done ,but like this
Mediaplayer mp = new MediaPLayer();
Change the data source:
If your source it’s a uri just pass it to setDataSource , but if is a resource in raw folder use this to create an uri from the resource
int resourceId = R.raw.other_song
uri uriSound = Uri.Builder()
.scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)
.authority(resources.getResourcePackageName(resourceId))
.appendPath(resources.getResourceTypeName(resourceId))
.appendPath(resources.getResourceEntryName(resourceId))
.build()
mp.setDataSource(this , uriSound)
Prepare and start the media player when you want
mp.prepare()
mp.start()
You create your mediaplayer object without ".create" like this
Mediaplayer mp=new Mediaplayer();
Then you just set the source of it note the source must be in uri format
mp.setDataSource(this,uri);
But before starting it you have to prepare first like this:
mp.prepare();
Then start.
And when you wanted to change the source again first stop the mp then set another source
Hope you get it.
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'am Unable to put Reverb effect on mp3 audio files for my android application and I've tried the following codes:
mp = MediaPlayer.create(this, R.raw.audio1);
PresetReverb pr=new PresetReverb(0,mp.getAudioSessionId());
pr.setPreset(PresetReverb.PRESET_LARGEHALL);
pr.setEnabled(true);
mp.start()
mp = MediaPlayer.create(this, R.raw.audio1);
PresetReverb pr=new PresetReverb(1,0);
pr.setPreset(PresetReverb.PRESET_LARGEHALL);
mp.attachAuxEffect(pr.getId());
mp.setAuxEffectSendLevel(1.0f);
pr.setEnabled(true);
mp.start();
I've also added required permissions.
Anybody please help. I'm not getting any help from android Document as well.
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.
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