I'm trying to create a mediacontroller to control my audio file - android

Alright so i'm trying to create a mediacontroller to control my audio file that plays whenever i click on an imageview.
i tried this but it wouldn't recognized the audio file (mysound)
MediaController mc = new MediaController(this);
mc.setMediaPlayer(mysound);
mysound.setMediaController(mc);

You should be able to call start per the Android documentation once you have provided a valid audio file.
MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.sound_file_1);
mediaPlayer.start(); // no need to call prepare(); create() does that for you

Related

One media controller for two video views android

I am trying to play two video views using the same media controller but it seems impossible to play them and stop them and seek through both at the same time
can someone suggest a solution here is what I am trying :
//2 video views
videoview=(VideoView)findviewbyid(R.id.videowview);
videoviewtwo=(VideoView)findviewbyid(R.id.videowview2);
//the media controller
MediaController mc =new MediaController(this);
//setting uri for both:
videoview.setVideoURI(uri);
videoviewtwo.setVideoURI(uri1);
//setting same media controller object for both
videoview.setMediaController(mc);
videoviewtwo.setMediaController(mc);
//finally start both
videoview.Start();
videoviewtwo.Start();
after this both play well at the first time but when I try to repeat only one starts the other doesnt, thank you for your help.
Edit: I want them both to play at the same time each time I repeat the process .
You need to release and set the mediaplayer to null before you can start the new video. Try to call this when the video has stopped and before you start a new video so you know that the mediaplayer is not busy.
private void releaseMediaPlayer() {
mMediaPlayer.release();
mMediaPlayer = null;
}
So next is to find out when the mediaplayer have stopped playing your video. A hint: MediaPlayer.OnCompletionListener

Media Player set path of another folder

I am making a simple app that plays an mp3 file. I use the code
mp = MediaPlayer.create(MainActivity.this, R.raw.mysong);
But this works only if my song is saved in the res\raw folder of my PC. Sooner or later I will run the app of my iphone (as soon as I buy one!). What happens if I must set the path to the one that the mobile is saved? Let's say my Downloads folder from my mobile phone.
You can do it by giving it it's absolute path from the ExternalStorage as follwoing
mp = MediaPlayer.create(MainActivity.this, Uri.parse(Environment.getExternalStorageDirectory().getPath()+ "/Music/mysong.mp3"));
OR Use setDataSource() method:
String PATH_TO_FILE = "/sdcard/music.mp3";
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(PATH_TO_FILE);
mediaPlayer.prepare();
mediaPlayer.start()
String filePath = "somepath/somefile.mp3";
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(filePath);
mediaPlayer.prepare();

Android : how to play multiple audios using single media player object

I need to create only one media player object Mediaplayer mp = new Mediaplayer();
Using this I need to play multiple audio files one after the other for that i am using handler
and getting the duration.
If i create multiple media player objects it shows error(1, -17)
I also need to display images related to audio files.
Did you rule out SoundPool?
http://developer.android.com/reference/android/media/SoundPool.html
It typically good for short audio clips.
Implement the OnCompletionListener and register it with your MediaPlayer instance.
After the media has been played out it will call this callback method onCompletion
void onCompletion(MediaPlayer mp){
//Here you stop it.
mp.stop();
//Reset the data source path to the new file
mp.setDataSource(<uri>);
mp.prepare(); // or mp.prepareAsync();
// start the mediaplayer after the prepare has completed.
}
Release the mediaplayer instance once you are done playing all the files.

Android play a video with controller

I'm using the following code to play a .mp4 video on my android device. I have 3 problems,
using this code when I press the back button on the device the sound would still continoue to play
It doesn't play the video, only plays the sound!
I don't know how to get controller for the video so the user can stop the video, go back or forth on the video.
VideoView videoHolder = new VideoView(this);
videoHolder.setMediaController(new MediaController(this));
getWindow().setFormat(PixelFormat.TRANSLUCENT);
MediaPlayer mediaPlayer = new MediaPlayer();
Uri myUri = Uri.parse("android.resource://" + getPackageName() + "/" + R.drawable.video);
videoHolder.setVideoURI(myUri);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(getApplicationContext(), myUri);
mediaPlayer.prepare();
mediaPlayer.start();
videoHolder.requestFocus();
videoHolder.start();
1) Override onBackPressed in your activity:
#Override
public void onBackPressed() {
mediaPlayer.stopPlayback();
super.onBackPressed();
}
2) Is it a valid video format? Are you using emulator or a real device? If you use an emulator, try a real device instead. I recall similar issues before.
But I also notice: did you put your video in the drawable folder?
R.drawable.video
I don't think that will work, you cannot play videos from there. Use the resources/raw folder instead. btw: Audio can be played from assets folder directly, while video cannot; video can only played from the raw folder or if you copy it to the apps files folder (getFilesDir()) - or sdcard of course.
3) Why are you using a VideoView AND a MediaPlayer, instead of just a VideoView? You should get rid of the mediaPlayer and just use the VideoView. This line
videoHolder.setMediaController(new MediaController(this));
looks correct and sets the media controller, with which the user can seek in the video.
Programatically you can also use seekTo() to go back and forth in a video.
VideoView videoView =(VideoView)findViewById(R.id.vv01);
//specify the location of media file
Uri myUri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.login_footage);
//Setting MediaController and URI, then starting the videoView
videoView.setVideoURI(myUri);
videoView.requestFocus();
videoView.start();
This worked for me

android MediaPlayer for video. problems starting

how do i reference a video and play it using mediaPlayer and VideoViewer, when the file is in my raw folder
iv tried
videoview.setVideoPath("android.raw://com.example.movievp8");
as well as
MediaPlayer video1 = MediaPlayer.create(this, R.raw.movievp8);
once I have referenced it should i just hit video1.start();
also does anyone know where i could get like a full sample code on how to use media player for videos, just the basics.
Your code
MediaPlayer video1 = MediaPlayer.create(this, R.raw.movievp8);
video1.start();
should works (but you will only have audio).
One way to do it is using a VideoView (where your video will be displayed) and a MediaController (to have some predefined buttons such as Play, pause, stop, etc.)
VideoView vv = (VideoView) findViewById(R.id.videoview);
MediaController controller = new MediaController(vv.getContext());
vv.setMediaController(controller);
vv.setVideoURI(Uri.parse("android.resource://" + context.getPackageName() + "/" + videoID));
vv.start();
Remember that the simulator doesn't support video. You'll need to use a real device for testing video.

Categories

Resources