I have written an Android application ( in eclipse) that plays a music file as follows:
MediaPlayer mediaPlayer = new MediaPlayer();
try
{
mediaPlayer.setDataSource(fileName);
mediaPlayer.prepare();
mediaPlayer.start();
// mediaPlayer.stop();
}
catch (Exception e)
{
Log.d("Exception---", e.getMessage());
}
When I run it, it does not play the file ( and does not show any exception ), but when I switch to 'debug mode', and trace it line by line, it plays the file. I'm really confused. Would you please help me find out what's wrong?
Thanks.
Put a log before "mediaPlayer.setDataSource(fileName)" and check if you are getting a valid fileName in release mode.
I had similar problem and in my case it didn't like the player.PrepareAsync() command. It wasn't giving any errors in debugger except some miniscule info in the output window which wasn't of any help. Realizing that something was tripping the player I started to change a few things and changed the player.prepareAsync to player.Prepare() and thats all it took in Xamarin.
Related
So, I've searched around quite a bit, and haven't found anything that resolves this. (found someone asking in 2016, with no answers and did it somewhat differently, also I've found some that didn't care if the file was deleted on exit, or on next start up, which I do)
I have an application where I have one button for letting a user pick an mp3 file on the computer, and one button that let's the user play that sound back, when the user is done, he/she can click a save button that is supposed to delete the mp3 file that is stored in the user's folder, and then replace it with the new one the user picked, they are supposed to do this continuously, so deleting the previous file on exit isn't very good, as it could lead to a big heap of temp files that needs to be deleted on exit, and checked for on launch.
so, from what I can gather it seems to be a Windows-centric problem where a file opened in mediaplayer isn't released properly(or something like that, was a bit hard to follow..), so I was wondering if there is a way to force the MediaPlayer object to release the Media object/file, or maybe a way to find out and have a listener for when the dispose() method is done doing it's thing, so that I can delete the file while the java program continues to run afterwards?
here is a code snippet to illustrate the problem.
import javafx.application.Application;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.stage.Stage;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class Main extends Application {
public static void main(String[] args){launch(args);}
#Override
public void start(Stage primaryStage) throws Exception {
Path path = Paths.get("some/file/path.mp3"); //making a path for the Media object and for deleting later
Media media = new Media(path.toUri().toString()); //Making a Media object for the Mediaplayer
MediaPlayer mediaPlayer = new MediaPlayer(media); //Making a MediaPlayer
mediaPlayer.play(); //playing just to make sure it has been used
mediaPlayer.stop(); //stopping the player
mediaPlayer.dispose(); //disposing of the MediaPlayer
//checking to see that the MediaPlayer has been disposed of at least
try {
if (mediaPlayer.getStatus().equals(MediaPlayer.Status.DISPOSED)) {
Files.delete(path); //trying to delete, and subsequently crashing..
}
} catch (Exception e) {
e.printStackTrace();
}
System.exit(0); //terminating program
}
}
edit(added the exception thrown(replacing my actual path with the dummy one)):
java.nio.file.FileSystemException: some/file/path.mp3: The process cannot access the file because it is being used by another process.
my current thought on a solution is to have a file where I mark files for deletion when the program starts next, and keeping track of what files should be copied over to the user folder then.
or maybe even not actually saving the files in the users folder, and rather keeping a reference to the users files in a text file or something or other, while I just have a general "sounds folder" that I can get things from..
or something like that, but I thought I'd at least ask you people if you had any ideas on how to solve it as I initially wanted to do it.
Really sorry if this should end up as a duplicate, I haven't really found any answers that have helped when searching for this, but if you do, then please send me on my way over to wherever that is :)
Thank you for any Ideas you might have, and hope the English is understandable, also, if you want any more info or anything, please let me know :)
Edit 2:
So, I've found a "bad" workaround, where I use the AudioInputStream to play a sound with a Clip, as here I can control the stream, and close it before deleting the file, this seems to work quite well, only problem with this is that I am now limited to wav files(afaik), but I can work with that for now at least, but not leaving it as an answer, as a MediaPlayer solution would be far better imo.
Edit 3(forgot to add the code)(got the audio part from here: https://stackoverflow.com/a/11025384/10044355):
Path path = Paths.get("some/file/path.wav");
File yourFile = new File(path.toString());
AudioInputStream stream = null;
AudioFormat format;
DataLine.Info info;
Clip clip;
try {
stream = AudioSystem.getAudioInputStream(yourFile);
}catch (Exception e){
e.printStackTrace();
}
format = stream.getFormat();
info = new DataLine.Info(Clip.class, format);
clip = (Clip) AudioSystem.getLine(info);
clip.open(stream);
clip.start();
clip.stop();
stream.close();
Files.delete(path);
I tried to create a video player on android device, which could stream videos from local server. I found an this example. I followed to the guideline and at the end I was able to play local video on my device. Then I started to modify this project. I set VideoActivity as launcher activity and commented out all data received from previous activity. I modified initialization of media from
Media m = new Media(libvlc, media);
mMediaPlayer.setMedia(m);
to
Uri uri = Uri.parse("http://samples.mplayerhq.hu/MPEG-4/embedded_subs/1Video_2Audio_2SUBs_timed_text_streams_.mp4");
Media m = new Media(libvlc, uri);
mMediaPlayer.setMedia(m);
Added 2 new methods (as you see, those methods are dummy methods simply to see the result)
private void setSubtitles(){
MediaPlayer.TrackDescription[] tds = mMediaPlayer.getSpuTracks();
mMediaPlayer.setSpuTrack(tds[tds.length - 1].id);
}
private void seekTo(long time) {
mMediaPlayer.setTime(time);
}
And called them on play event
#Override
public void onEvent(MediaPlayer.Event event) {
VideoActivity player = mOwner.get();
switch(event.type) {
case MediaPlayer.Event.EndReached:
Log.d(TAG, "MediaPlayerEndReached");
player.releasePlayer();
break;
case MediaPlayer.Event.Playing:
---> player.seekTo(1000);
---> player.setSubtitles();
case MediaPlayer.Event.Paused:
case MediaPlayer.Event.Stopped:
default:
break;
}
}
But when ever I tried to play the video, I got 12401-12591/? A/libc﹕ ### ABORTING: INVALID HEAP ADDRESS IN dlfree. I get this error some seconds after subtitles ar shown (Also subtitles are too small). This error is shown only when I try to show subtitles. Without subtitles video is playing without any error.
I tried to play this video on VLC application and it was able to show subtitles. I tried to create new project - got the same error. I analysed VLC android source code and it looks that they are using almost the same logic for loading subtitles (Only setting different surfaceViews for video and subtitles, but it gave me the same error). So what am I missing? What does cause player to brake and how to fix it? Maybe there is already working example of code for showing subtitles.
I've a situation where I'm creating an instance of javax.Media.player (to play audio in wav foramt) using javax.Media.Manager and I've code which looks like:-
Player player = null;
MediaLocator locator = new MediaLocator("file path to wav file");
Manager.setHint(Manager.LIGHTWEIGHT_RENDERER, new Boolean(true));
try {
player = Manager.createRealizedPlayer(locator);
}
catch (CannotRealizeException e) {
e.printStackTrace();
}
player.addControllerListener(this);
player.start();
player.setMediaTime();
And after having an instance of player I'm invoking setMediaTime on it, the problem is that sometimes the player is updated with the time provided and sometime not.
Can anyone please suggest me that what mistake I'm making.
Finally, I got this working by tweaking my code, I obtained the player instance by
Manager.createPlayer(localtor)
and got my player realized by using busy waiting method, as soon as the player gets realized I invoke the setMediaTime method to set the audio start off set.
The reason why I was facing this problem was that, BasicPlayer from JMF API spwan a new thread if the fresh player instance is created and once the player is started and realized calling setMediaTime won't have any effect on that, basically this was more a threading issue which I overlooked.
In my application, I am trying to play a sound file in ogg format, stored in raw folder in res directory of my application. When I press the button that calls below function, it just freezes with the button pressed and does not respond. In the end, I have to terminate the application from Eclipse. Nothing about an error or exception in Logcat.
In debugging mode, it enters create function and never comes back. What am I doing wrong?
private void playbeep()
{
mPlayer = MediaPlayer.create(this, R.raw.beep);
mPlayer.start();
mPlayer.release();
}
You start and release the MediaPlayer at the same time. Try taking this out and see if it works.
mPlayer.release();
Also, check my post here to make sure you have the MediaPlayer set up correctly. If all else fails, try your audio file in a different format.
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.