how to play audio file in android
Simply you can use MediaPlayer
and play the audio file. Check out this nice example for playing Audio:
public void audioPlayer(String path, String fileName){
//set up MediaPlayer
MediaPlayer mp = new MediaPlayer();
try {
mp.setDataSource(path + File.separator + fileName);
mp.prepare();
mp.start();
} catch (Exception e) {
e.printStackTrace();
}
}
how to play an audio file based on a string variable on Android
After long struggle I finally found the answer.
If myInput is the variable, then it is possible to use a String of the path , parse it to Uri and finally pass it into create(). I also used try-catch because I noticed that if the file is not found in the folder, then the app crashes. Here is the solution that worked for me:
String uriPath = "android.resource://" + getPackageName() + "/raw/" + myInput;
Uri uri = Uri.parse(uriPath);
mp = MediaPlayer.create(this, uri);
try {
mp.start();
}
catch (Exception e) {}
Related Topics
How to Use the Android Accelerometer
Calculate the Size of a List View or How to Tell It to Fully Expand
Create Custom Big Notifications
Android - Print Full Exception Backtrace to Log
Cordova Plugin Development - Adding Aar
Displaying Yuv Image in Android
Live Audio Recording and Playing in Android and Thread & Callback Handling
Error:(9, 5) Error: Resource Android:Attr/Dialogcornerradius Not Found
Failure [Install_Failed_Invalid_Apk]
Build Errors After Android Studio 3.2.1 Upgrade
How to Add Action Bar Options Menu in Android Fragments
Androidstudio - Module Dependencies in Gradle
Offline Mode for Android App Using the Google Maps API
Where Does Android View.Scrollto(X, Y) Scroll To
Android Taking Screenshot of Offscreen Page