I am trying to play an audio file using the code below. I have the audio fileset p and everything is goo except for where it says context.context comes up in red so how do i `fix that or what am I suppoesed to put in there to make the snippet of code work. Any and all help is appreciated and thanks in advance. BTW i am using android studio if that helps.
MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.sound_file_1);
mediaPlayer.start();
I figured it out all i have to replace context with is "this" so the code is like below. Leaving this up in case someone in the future needs it.
MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.sound_file_1);
mediaPlayer.start();
Related
I have an old Android app, targeted for SDK16, which played my own MP3 sounds loud and clear:
MediaPlayer mp;
setVolumeControlStream(AudioManager.STREAM_MUSIC);
if (mp != null) {
mp.reset();
mp.release();
}
mp = MediaPlayer.create(this, R.raw.red_click);
mp.setVolume(1.0f, 1.0f);
mp.start();
I haven't done any Android coding for a few years, and now I started to work on my old project again, but now targeted to SDK23. I didn't change my code from the one above. Now my sounds are still playing in the app, but really really quiet.
I tried to search for an answer but couldn't find any: what has changed in MP implementation from SDL16, and how should I play my files now such that I get the full volume for them?
I'm trying to get some sound in my app, but I can't get the mediaplayer working! I got some errors like:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.start()' on a null object reference
with the following code:
private MediaPlayer mPlayer;
#Override
protected void onCreate(Bundle savedInstanceState) {
mPlayer = MediaPlayer.create(this, R.raw.NAMEOFSOUND);
}
protected void playBtnClicked(){
mPlayer.start();
}
It just doesn't work, whatever I do ... Does someone got some tips/Can someone help me out? The ''NAMEOFSOUND'' is a .mp3 file, I don't know if it even matters? Thank you!
SOLVED: I just solved it by coverting the file with a converter. Just changing the name+.mp3 didn't solve it; NullPointerException in Java Android App MediaPlayer , comment of Berty did the trick!
As you can see in the supported Media formats .mav is not supported by android. you should convert it to mp3 or any of the supported file formats
I am struggling with loading a sound in my application. I'm receiving a null point exception when trying to create the same "sound" in two activities. I have a variable clickSound that i declare in both activities which accesses the same Raw file.
I have added a folder named 'raw' in the 'res' folder and the file clicksound.mp3 in it.
The thing is, for MainActivity is working perfect. for Second activity i just receive the exception.
Main Activity:
// Declare clickSound onCreate
final MediaPlayer clickSound = MediaPlayer.create(this, R.raw.click_button);
Second Activity:
private final MediaPlayer mClickSound = MediaPlayer.create(this, R.raw.click_button);
The strange thing is that it worked some time ago but now i don't understand why not anymore. Any suggestions what is happening?
This line:
private final MediaPlayer mClickSound = MediaPlayer.create(this, R.raw.click_button);`
for that moment the instace in not valid initialized, so this is not givin all that you need to create a media player.... move that to the onCreate method
I think this error is related when you don't initialize your class.
Try before something like this:
MediaPlayer mClickSound = new MediaPlayer();
Or full code:
private final MediaPlayer mClickSound;
//other things
MediaPlayer mClickSound = new MediaPlayer();
mClickSound = MediaPlayer.create(this, R.raw.click_button);
It could be also that you're using two different names "clickSound" and "mClickSound"
The problem was not with the MediaPlayer file. I had an error with my Json Reader that was not handling well an exception.
So I hope it's not a repeated question but, from the following code
File f = new File(Environment.getExternalStorageDirectory(), TRYVID);
Uri uri = Uri.fromFile(f);
mc = new MediaController(this);
mp.setMediaController(mc);
mp.setVideoPath("/sdcard/try2.mp4");
this is part of a function that's called when a button is pressed, what i'm hoping to achieve is that when the user presses a key, the video plays but i've learned that the videoview does not play anything from the raw folder so i copied the video into the sdcard, but then after i press on the button on the emulator, it just crashes says it has to be close unexpectedly. I tried both the .setVideoPath as well as the .setUri but both does not work hmm anyone can point to my problem here?
Ok so first off you need to make sure that you use the .setAnchorView(View v) on your mediaController or else it wont correctly control the videoView. Also your missing your .start() to actually start the video. Having recently done something similar with streaming from an rstp video file i can tell you there there is a chance its not working because your running it on an emulator, the video playback on AVD's often doesn't work. Try running it on a physical device if you have access to one, also read the logcat to get a better idea of where the errors are happening.
I hope this helps.
For playing video files an from SD card you can try this:
String filepath = Environment.getExternalStorageDirectory()+"/a.mp4";
VideoView vv = new VideoView(getApplicationContext());
setContentView(vv);
vv.setVideoPath(filepath);
vv.setMediaController(new MediaController(this));
vv.requestFocus();
vv.start();
Try this below code this wii surely solve your problem,
Make videoView,
VideoView videoView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
videoView = (VideoView)findViewById(R.id.VideoView);
videoView.setVideoPath("/sdcard/blonde_secretary.3gp");
videoView.start();
}
I wish it will help you.
I had the same question and found the solution. My code in the link works fine.
Check this question of mine
I have a soundboard uploaded on the android market. The app is doing pretty well in the market(50,000+ downloads), but the developer console reports that I have an error, and this is bothering me.
All crash reports come from only one device - Motorola Droid. I've looked at what the error actually is, and it happens when I call the start() method for the MediaPlayer class. I get the following:
java.lang.NullPointerException:
at com.meeg.soundit.Soundboard.playAudio(Soundboard.java:2517)
the code for the method playAudio is as follows and line 2517 is mp.start():
public void playAudio(int resid){
final MediaPlayer mp = MediaPlayer.create(this, resid);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer arg0) {
mp.release();
}
});
}
Like I said, my soundboard has over 50,000 downloads, and it has 80 reports, all from the Motorola Droid. Is this something that I should ignore because 80 reports isn't much compared to how many people have used this, is there a problem with Moto Droid's and MediaPlayer, or is it just my code thats faulty?
This was a problem earlier on some builds that causes playback from resources to not work quite right. But you should fix your code to check for null and display appropriate message to the user.