Android Media player not working on button click - android

As the title suggests, I'm looking to play a small sound clip when a button is click - very basic. However, when I click the button the first time I get the following error:
08-01 15:01:58.547 18991-18991/? E/MediaPlayer﹕ Should have subtitle controller already set
08-01 15:02:01.030 18991-19008/za.co.site.app E/MediaPlayer﹕ error (1, -1010)
08-01 15:02:01.054 18991-18991/za.co.site.app E/MediaPlayer﹕ Error (1,-1010)
When I click the button again I get, the following error:
08-01 15:07:09.586 18991-18991/za.co.site.app E/MediaPlayer﹕ start called in state 0
08-01 15:07:09.586 18991-18991/za.co.site.app E/MediaPlayer﹕ error (-38, 0)
08-01 15:07:09.587 18991-18991/za.co.site.app E/MediaPlayer﹕ Error (-38,0)
I'm using a fragement. Here is the code:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main, container, false);
final Button b = (Button) view.findViewById(R.id.button1);
b.setEnabled(false);
final MediaPlayer mediaPlayer = MediaPlayer.create(getActivity(), R.raw.clip);
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(final MediaPlayer mp) {
b.setEnabled(true);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mp.start();
}
});
}
});
return view;
}
After reading around the first error is to do with Android not finding the file. However, the file is in a raw folder inside the res folder and android is definitely picking up the clip with R.raw.clip. By using the OnPreparedListener, this should stop the second error from occurring because the app caters for the clip to be ready before the button is pressed.
I'm not too sure when I'm doing wrong, thoughts?

You don't need to prepare if you are creating this way. Just call start().
This is because the file is local.

When using the Media player try to use mp3 files. The file I was using was a .wav file. The moment I made the file an mp3 file it worked.

Related

E/MediaPlayerNative: error (1, -2147483648)

I have uploaded a video in my recourses file in android studio and I want to play the video offline in my app my video is of 1 MB but when I run my app it throws an error
error (1, -2147483648)
and a pop up
shows up that Video can not be played.
E/SurfaceSyncer: Failed to find sync for id=0
E/MediaPlayerNative: error (1, -2147483648)
W/Parcel: Expecting binder but got null!
E/MediaPlayer: Error (1, -2147483648)
D/VideoView: Error: 1,-2147483648
This is my Main class code
package com.example.igea;
import...
public class MathsChap1 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maths_chap1);
Button bb = findViewById(R.id.ibk);
bb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent( packageContext: MathsChap1.this, Maths.class);
startActivity(i);
}
});
VideoView v1 = findViewById(R.id.vv1);
v1.setVideoPath("android.resources://" + getPackageName() + "/" + R.raw.video1);
MediaController mc = new MediaController( context: this);
v1.setMediaController(mc);
mc.setAnchorView(v1);
v1.start();
}
}
Can someone please help me with this error?
You can use MediaPlayer
MediaPlayer mediaPlayer;
mediaPlayer = MediaPlayer.create(this, R.raw.your_video_file);
mediaPlayer.start();

"Can't play this video" Error using URL with VideoView and Mediacontroller

So I'm trying to do my homework, but the teacher's given me 0 information about this. There is an example of how to play a video from a file in the res/raw folder, but there's nothing about online URLs. Please help me, I just want a simple player. I'll attach a picture detailing exactly what is up. I'll also add the code, since it's not that much and I really have no clue what could be wrong. Error says this:
W/MediaPlayer: Couldn't open http://techslides.com/...
java.io.FileNotFoundException: No content provider: http://techslides.com/demos/sample-videos/small.mp4
And this is the code:
VideoView video;
String url = "http://techslides.com/demos/sample-videos/small.mp4";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
video = (VideoView) findViewById(R.id.videoView);
MediaController mc = new MediaController(this);
mc.setAnchorView(mc);
video.setVideoPath(url);
video.setMediaController(mc);
video.start();
}
I will finally add to this that I've tried several different URLs, including some https ones and some http.
EDIT:
So, I tried fixing it and it ended up looking like this:
video = (VideoView) findViewById(R.id.videoView);
final MediaController mc = new MediaController(this);
mc.setAnchorView(mc);
video.setVideoPath(url);
video.setMediaController(mc);
video.setOnPreparedListener(new MediaPlayer.OnPreparedListener(){
#Override
public void onPrepared(MediaPlayer mp){
video.start();
}
});
But it still gives me the same error when the emu is open. "Can't play this video". On the other hand, I got a bunch of new errors:
E/MediaPlayerNative: error (1, -2147483648)
E/MediaPlayer: Error (1,-2147483648)
D/VideoView: Error: 1,-2147483648
I'm not really familiarized with this technology, and the teacher hasn't given us any notions whatsoever as to what should or shouldn't be in the code for it to work. Just an example of a locally stored video playing in Android Studio with VideoView... that doesn't work when applied to online URLs.
So I've ended up fixing it myself. The problem wasn't in the code, for anyone wondering I've ended up using this simple format:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
video = (VideoView) findViewById(R.id.video);
Uri uri = Uri.parse("http://techslides.com/demos/sample-videos/small.mp4");
video.setMediaController(new MediaController(this));
video.setVideoURI(uri);
video.requestFocus();
video.start();
}
The problem was the AVD itself. I had a Pixel 1 running Android 9, and that for some reason didn't work. I've installed a Nexus 5 with Oreo and it works flawlessly.
This may help you
VideoView simpleVideoView = (VideoView) findViewById(R.id.simpleVideoView); // initiate a video view
// perform set on prepared listener event on video view
simpleVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
// do something when video is ready to play, you want to start playing video here
}
});
Try to start video only when its fully ready to play. Since mp4 will take some time to download.. so it might be in inconsistent state when you started video.
Hope this will help.

Android video stream from url over the videoview or surfaceview

There are so many posts on the same topic but no one is giving proper reason for this error, Could some one help me to solving this error.
My code is working fine for SDCARD videos. whenever I try to access it from url it throws this error.
W/MediaPlayer: Couldn't open file on client side; trying server side: java.io.FileNotFoundException: No content provider: http://download.itcuties.com/teaser/itcuties-teaser-480.mp4
E/MediaPlayer: error (1, -2147483648)
I tried several formats and several urls for everything i am getting the same error.
Internet permissions given.
Code:
public class MainActivity extends AppCompatActivity {
VideoView video1;
String url ="http://download.itcuties.com/teaser/itcuties-teaser-480.mp4";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
video1 = (VideoView) findViewById(R.id.video1);
video1.setVideoURI(Uri.parse(url));
video1.setMediaController(new MediaController(this));
video1.requestFocus();
Thread view1=new Thread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_DISPLAY);
video1.start();
}
});
}
}
You just need to start your view1 thread. Your code loads the video but doesn't play it. Adding this will solve the issue.
view1.start();

playing a MP3 from raw folder?

im trying to play a MP3 from raw folder .i have different buttons which playing different MP3's so i must change the mediaplayer input every time here is my code on button click:
public void onClick(View v) {
t=R.raw.virtualbarber
playsound(t);
}
and here is my mediaplayer code which im taking error in .create ! :
private void playSound(string t ){
mp = MediaPlayer.create(getActivity(), t);
mp.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
mp.setLooping(true);
mp.start();
}}
im also declared my string and mediaplayer on top of my project like this :
MediaPlayer mp;
int t;
nothing works ! my other program worked correctly but this doesn't !
logcat
01-17 22:01:35.016: E/AndroidRuntime(30521): FATAL EXCEPTION: main
01-17 22:01:35.016: E/AndroidRuntime(30521): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.safshari.board3d/com.safshari.board3d.MainActivity}: java.lang.NullPointerException
01-17 22:01:35.016: E/AndroidRuntime(30521): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2247)
01-17 22:01:35.016: E/AndroidRuntime(30521): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2297)
Use
mp = MediaPlayer.create(getActivity(), R.raw.virtualbarber);
If it is fragment instead of this use getActivity().
mediaplayer fixed tnx to you but now im getting nullpointer exception on start of program ?
If its still crashing post the updated relevant code along with stacktrace for further help
Use the uri something like:
Uri myUri = Uri.parse("android.resource://com.package.sample/raw/filename");
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.setDataSource(getApplicationContext(), myUri);
mp.prepare();

android video from local server error

Hi there I'm working in my android app to run video from my local server XAMPP and I have placed my video in htdocs folder under my XAMPP server folder and I got this piece of code :
this is the .java file code
public class video extends Activity {
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.video1);
final VideoView vv = (VideoView) findViewById(R.id.vview);
MediaController mc = new MediaController(this);
vv.setMediaController(new MediaController(this));
vv.setVideoURI(Uri.parse("http://10.0.2.2/abc.MP4"));
vv.requestFocus();
vv.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
vv.start();
}
});
}
}
and here's my logcat errors
03-02 02:12:29.222: E/MediaPlayer(464): error (1, -2147483648)
03-02 02:12:29.242: E/MediaPlayer(464): start called in state 0
03-02 02:12:29.242: E/MediaPlayer(464): error (-38, 0)
03-02 02:12:29.252: E/MediaPlayer(464): Attempt to call getDuration without a valid mediaplayer
03-02 02:12:29.252: E/MediaPlayer(464): error (-38, 0)
03-02 02:12:29.322: E/MediaPlayer(464): Error (1,-2147483648)
03-02 02:12:29.742: E/MediaPlayer(464): Error (-38,0)
03-02 02:12:30.222: E/MediaPlayer(464): Error (-38,0)
When I click the button to direct me to video view layout where I should see my video playing I got a "black" blank screen for a moment then the screen changes to a white color for the lower have and black for the upper after that I receive the "this video can't be played" error msg three of them together

Categories

Resources