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
Related
I'm trying to play a sound when a button is clicked or when a new view is showing up. I wrote this method to play a sound:
void play_sound(String musicFile) {
AudioClip sound = new AudioClip(new File(musicFile).toURI().toString());
sound.play();
}
and this is how I call it:
String musicFile = "src/resources/sounds/applause.mp3";
play_sound(musicFile);
I have to mention that applause.mp3 is downloaded from the web and when I play a sound from web it works. But when I want to play a sound that is recorded (for example app.mp3), it doesn't work anymore. All these files are in the same folder(sounds).
Your file path should definitely start with a / to mark it as absolute (inside of the jar file).
Then, you should'nt use the File class to load resources from inside the jar file. Use
String path = getClass().getResource("/resources/sounds/applause.mp3")
AudioClip sound = new AudioClip(path);
instead.
Cheers
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.
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'm new to android development, self taught so can expect a few errors here and there but none so irritating as this. I've looked over my code a thousand times, searched high and low across multiple websites, books and forums for an answer but I still get the same error so this is a last resort.
I just want to play a hardcoded path to a video in an activity, which is part of my video portfolio app. (The hardcoded path is just for testing, later I will call each video from the related button press, but only after I sort the player out!).
Here is my code:
String path = "android.resource://mysite/res/raw/video1";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_videoplayer);
VideoView view = (VideoView) this.findViewById(R.id.vv);
MediaController controller = (MediaController) new MediaController(this);
controller.setMediaPlayer(view);
view.setVideoPath(path);
view.requestFocus();
view.start();
}
This snippet is inside my VideoPlayer class, where vv is the VideoView in the XML and video1 is the video to be played. The video is h.264 mp4, 1 minute long and 3mb in size and can be played normally through the default player.
XML:
<VideoView
android:id="#+id/vv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
The activity loads but returns the error "Can't play this video".
Any help I greatly appreciate.
VideoView view = (VideoView) this.findViewById(R.id.vv);
view.setVideoPath(path);
view.setMediaController(new MediaController(this));
view.requestFocus();
view.start();
Use like this. hope this will give you some solution.
According Android's official documentation Video decoding support for MP4(h.264) added from android 3.0+, so I think you are playing this video on version below 3.0. Try it on the device which has android os version 3.0+.
Try this..
video0=(VideoView)findViewById(R.id.vv);
video0.setMediaController(new MediaController(this));
video0.setVideoURI(Uri.parse("android.resource://" +getPackageName()+ "/" +R.raw.video1));
video0.requestFocus();
video0.start();
Did you try
MediaController mc = new MediaController(this);
mc.setAnchorView(vv);
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.