Why MediaPlayer throws NOT present error when creating instance of it?
It means your platform does not support QCMediaPlayer. QCMediaPlayer provides extended APIs and interfaces to get and set MPD attributes for DASH protocol in compatible Snapdragon builds. So, this error should not affect on media playing in normal case
Check this sources to find out more details:
QCMediaPlayer.java
Error in MediaPlayer : E/MediaPlayer: Error (1,-19) - Android
I solved the problem!
I changed the way I call the sound file, here's how I did it.
I put the file inside the assets in order to make it works.
if(mp.isPlaying())
{
mp.stop();
}
try {
mp.reset();
AssetFileDescriptor afd;
afd = getAssets().openFd("AudioFile.mp3");
mp.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
mp.prepare();
mp.start();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
I got this from here: android - how to make a button click play a sound file every time it been pressed?
Why do I get NullPointerException when trying to instantiate Mediaplayer? Android Kotlin
Often it is best to wait to use the activity as a context until later in the lifecycle when things have been set up (e.g. in onCreate
) to avoid initialization order issues. For exampe:
class MainActivity : AppCompatActivity() {
private lateinit var soundGenerator : SoundGenerator
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
//...
soundGenerator = SoundGenerator(this)
}
}
Alternately, sometimes you can use applicationContext
instead of the current activity as an appropriate context.
Related Topics
How to Have a Transparent Imagebutton: Android
Why Was "Avoid Enums Where You Only Need Ints" Removed from Android's Performance Tips
Start an Activity from a Fragment
Android Recyclerview:Notifydatasetchanged() Illegalstateexception
How to Resolve "Waiting for Debugger" Message
How to Access Fragment's Child Views Inside Fragment's Parent Activity
Proguard: Can't Find Referenced Class Com.Google.Android.Gms.R
How to Sign an APK with More Than One Certificate
Accessing Contents of R.String Using a Variable to Represent the Resource Name
Progressbar Togther with Asynctask
How to Simulate Android Killing My Process
How to Set Android_Home Path in Ubuntu
Handle Button Click Inside a Row in Recyclerview
Controlling the Camera to Take Pictures in Portrait Doesn't Rotate the Final Images
How to Start an Intent by Passing Some Parameters to It
Show and Hide a View With a Slide Up/Down Animation
How to Evenly Distribute Buttons Across the Width of a Linearlayout