I implemented a file chooser on a media player which returns the file paths for an .mp3 and an .srt on the externalSD. The audio plays fine. But when I call addTimedTextSource with the path to the .srt, it throws a null pointer exception. So, I put in an If(file.exists). It also returns a null. I tried moving the file to the internal SD with the same result. Any ideas?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_player);
txtDisplay = (TextView) findViewById(R.id.txtDisplay);
buttonPause = (Button) findViewById(R.id.buttonPause);
buttonPlay = (Button) findViewById(R.id.buttonPlay);
Bundle bundle = getIntent().getExtras();
if(bundle!=null) {
String removeString = "file:";
soundPath = bundle.getString("soundFile");
subPath = bundle.getString("subFile");
subPath = removeString(subPath,removeString);
soundFile = new File(soundPath);
subFile = new File(subPath);
}
player = new MediaPlayer();
try {
player.setDataSource(soundPath);
player.setOnErrorListener(this);
player.setOnPreparedListener(this);
player.prepareAsync();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void onPrepared(MediaPlayer mp){
mp.setOnTimedTextListener(this);
if(subFile.exists() {
try {
player.addTimedTextSource(subFile.getAbsolutePath(), MediaPlayer.MEDIA_MIMETYPE_TEXT_SUBRIP);
} catch (IOException e) {
Log.v("Hey Here is a Problem: ", e.getMessage());
}
TrackInfo[] ti = player.getTrackInfo();
for (int i = 0; i < ti.length; i++) {
if (ti[i].getTrackType() == TrackInfo.MEDIA_TRACK_TYPE_TIMEDTEXT) {
player.selectTrack(i);
break;
}
}
}else{
onBackPressed();
}
mp.start();
}
Edit:
Rereading your question, it appears that subfile.exists() returns true, but you get a null pointer exception when you try to call player.addTimedTextSource().
Assuming player is not null, I'd like you to try this:
string fileStr = subfile.getAbsolutePath();
player.addTimedTextSource(fileStr, MediaPlayer.MEDIA_MIMETYPE_TEXT_SUBRIP);
I suspect that you may be running into an issue with the overloads of addTimedTextSource().
Old answer:
So, I put in an If(file.exists). It also returns a null. I tried
moving the file to the internal SD with the same result. Any ideas?
There is no file at the provided path. Since you have moved the file, seems like you're mistyping the file name.
Related
I have problem with playing mp3 from asset folder. When i try to change image on button, mp3 stops playing(not always...)
Error that im getting
W/MediaPlayer-JNI: MediaPlayer finalized without being released
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(enableMusic){
playMusic();
}
...
private void playMusic() {
boolean noAudioFile = false;
Context context = this; // or ActivityNotification.this
MediaPlayer mpMusic = new MediaPlayer();
AssetManager mg2 = getResources().getAssets();
String musicPath = langFolder+audioFolder+"system/Bubble_Bath.mp3";
try {
mg2.open(musicPath);
} catch (IOException ex) {
noAudioFile = true;
}
if(!noAudioFile) {
AssetFileDescriptor descriptor = null;
try {
descriptor = context.getAssets().openFd(musicPath);
} catch (IOException e) {
e.printStackTrace();
}
long start = descriptor.getStartOffset();
long end = descriptor.getLength();
try {
mpMusic.setDataSource(descriptor.getFileDescriptor(), start, end);
} catch (IOException e) {
e.printStackTrace();
}
}
else{
}
mpMusic.setVolume(5,5);
try {
mpMusic.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mpMusic.start();
mpMusic.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.start();
}
});
}
public void nextCard(View view) throws IOException {
i++;
if (i==files.length) i=0;
if(i>-1){
Button btnNext = (Button)findViewById(R.id.btnNext);
btnNext.setText("Next");
}
Drawable d = Drawable.createFromStream(getAssets().open(categoryPath+"/"+files[i]+".png"), null);
findViewById(R.id.btnImage).setBackgroundDrawable(d);
if(files[i].substring(0,1).contains("a") || files[i].substring(0,1).contains("i")){
startFileSound = langFolder + audioFolder + "system/GAME_thisisan.mp3";
}
else if (files[i].substring(files[i].length()-1,files[i].length()).contains("s")){
startFileSound = langFolder + audioFolder + "system/GAME_theseare.mp3";
}
else{
startFileSound = noAudioFileSound;
}
}
When i'll try to comment line below, mp3 is playing with no problem.
Drawable d = Drawable.createFromStream(getAssets().open(categoryPath+"/"+files[i]+".png"), null);
you are loading drawable from asset folder, that is correct. But Don't do in the main thread. Try to do this in different thread.
Don't ever create MediaPlayer like this
MediaPlayer mpMusic = new MediaPlayer();
Use
MediaPlayer.create(context,R.raw.music);
try close descriptor
mpMusic.setDataSource(descriptor.getFileDescriptor(), start, end);
descriptor.close();
I have been trying to do it in different ways, but all the "lists" want int or string and I have Assetfiledescriptor stuff to work with.
How can I create a list of certain mp3 files, which are located in the activity expansionfile, that I can use for oncompletionlisteners, etc?
They all work fine individually, but I want to play them after each other.
Here are a few files that I would use:
AssetFileDescriptor dfFn01, dfFn02, dfFn03 = null;
dfFn01 = expansionFile.getAssetFileDescriptor("l1r_df_l6.mp3");
dfFn02 = expansionFile.getAssetFileDescriptor("l1r_df_l7.mp3");
dfFn03 = expansionFile.getAssetFileDescriptor("df_jp_l1r03.mp3");
Here is the player that starts from a buttonclick:
public void setWholeEngAudio(AssetFileDescriptor fd) throws IllegalStateException {
try {
stopPlayers();
wholeTextPlayer = new MediaPlayer();
wholeTextPlayer.reset();
wholeTextPlayer.setDataSource(fd.getFileDescriptor(), fd.getStartOffset(), fd.getLength());
try {
wholeTextPlayer.prepare();
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
wholeTextPlayer.start();
seekBar.setProgress(0);
seekBar.setMax(100);
updateSeekBar();
setThumbState();
} catch (Exception e) {
e.printStackTrace();
}
}
Thanks
My app Doesn't work in playing video and audio files. I use internal storage for saving the audio video files.here is my code video and audio doesn't play the error showing my code is :Attempt to invoke virtual method 'void android.media.MediaPlayer.setDataSource(java.lang.String)' on a null object reference
public class VideoFileActivity extends Activity {
String filePath, fileName;
MediaPlayer mp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_LEFT_ICON);
setContentView(R.layout.activity_video_dialogue);
getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.ic_launcher);
SharedPreferences pref = getSharedPreferences("import_pkid", MODE_PRIVATE);
filePath = pref.getString("file_path", "");
fileName = pref.getString("file_name", "");
Bundle extras = getIntent().getExtras();
if(extras != null){
filePath = extras.getString("file_path");
fileName = extras.getString("file_name");
}
DownloadFiles();
MusicPlay();
}
public void DownloadFiles() {
//Downloads Files
}
public void MusicPlay()
{
try {
mp.setDataSource(filePath+"/"+fileName);
} catch (IllegalArgumentException e1) {
e1.printStackTrace();
} catch (IllegalStateException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
try {
mp.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
mp.setLooping(true);
mp.start();
}
}
use log statement to get value of filePath and fileName
i use mp.setDataSource(Environment.getExternalStorageDirectory().getAbsolutePath() + "/......./" + name.mp3) for playing internally saved audios
You forgot to initialize MediaPlayer.
Add the following before calling methods on 'mp' .
MediaPlayer mp=new MediaPlayer();
I have hundreds of buttons, all with the same listener and action, but different names. Here is one with the name goes1:
public void goes1(){
final MediaPlayer mp = MediaPlayer.create(this, R.raw.goes1);
play_text_view = (TextView) this.findViewById(R.id.goes1);
play_text_view.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
try {
lireXML("goes1");
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
mp.start();
}
});
}
I would like to have only one method where the name of the button would be passed to the method as a parameter. Something like this, with the String parameter mot:
public void playMot(String mot){
final MediaPlayer mp = MediaPlayer.create(this, R.raw.mot);
play_text_view = (TextView) this.findViewById(R.id.mot);
play_text_view.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
try {
lireXML(mot);
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
mp.start();
}
});
}
but R.raw.mot and R.id.mot don't like this!
Should I really have hundreds of buttons, or is there a way around my problem?
Thanks
You are looking for Resources.getIdentifier. It looks up a resource id from a string name and type.
public void playMot(String mot) {
Resources res = getResources();
// Should error check this, returns 0 on error.
final rawId = res.getIdentifier(mot, "raw", getPackageName());
final MediaPlayer mp = MediaPlayer.create(this, rawId);
// Should error check this, returns 0 on error.
final resId = res.getIdentifier(mot, "id", getPackageName());
play_text_view = (TextView) this.findViewById(resId);
...
You can story the relation between name and raw id,the relation between name and view id,in hashMap,like this:
HashMap<String, Long> nameToRawId = new HashMap<String, Long>();
HashMap<String, Long> nameToViewId = new HashMap<String, Long>();
then get id from that hashMap int method playMot(String mot):
public void playMot(String mot){
final MediaPlayer mp = MediaPlayer.create(this, nameToRawId.get(mot));
play_text_view = (TextView) this.findViewById(nameToViewId.get(mot));
play_text_view.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
try {
lireXML(mot);
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
mp.start();
}
});
}
hope this can help you.
I have an activity which has a series of buttons which when pressed should play an audio file. I have been trying to implement this using MediaPlayer however I cant get it to work.
Here is the code I have been trying:
final MediaPlayer mp = new MediaPlayer();
Button ger1play = (Button) findViewById(R.id.ger1play);ger1play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mp.setDataSource(this, R.raw.greet_1);
mp.prepare();
mp.start();
}
});
The setDateSource method doesnt seem to work, can anyone tell me where I am going wrong?
I would like to then set the mediaPlayer to the relevant audio file based on which button is pressed, is this possible?
Updated
final MediaPlayer mp = new MediaPlayer();
Button ger1play = (Button) findViewById(R.id.ger1play);ger1play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
Uri myUri = Uri.parse(R.raw.greet_1);
mp.setDataSource(GreetingsLesson.this, R.raw.greet_1);
mp.prepare();
mp.start();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
try this:
final MediaPlayer mp = new MediaPlayer();
Button ger1play = (Button) findViewById(R.id.ger1play);ger1play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
mp.setDataSource(CurrentActivity.this, R.raw.greet_1);
mp.prepare();
mp.start();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
Ifyou want to send the media player object with one of the files fromapplication raw resources or from application assets files you can dothat as follows:
try {
AssetFileDescriptor fd = getResources().openRawResouceFd(R.raw.greet_1);
mp.setDataSource(fd.getFileDescriptor(), fd.getStartOffset(), fd.getLength());
mp.start();
fd.close();
} catch (IllegalArgumentException e) {
// handle exception
} catch (IllegalStateException e) {
// handle exception
} catch (IOException e) {
// handle exception
}
Why not just use
mp = MediaPlayer.create(this, R.raw.greet_1);
Then you don't need the prepare or start.
Are you running this in an emulator? If so check your AVD manager has under hardware, the property "Audio playback support | yes" added