0
public void getNewStartingPoint() {

    path = "/mnt/sdcard/downloads/bluetooth/aabbccdd.mp3"; 

    try {
        mp.setDataSource(path);
        mp.prepare();
        mp.start();
    } catch (Exception e) {
        Log.e(TAG, "error: " + e.getMessage(), e);
    }

}

It seems that all of the tutorials show you how to upload a song from your sdcard, but they're all locations defined by the programmer. I was wondering how could i use songs off of the user's phone that they can decide to preview. Also say for instance I wanted to play a song for only 30 seconds. What method do I use to stop the song from playing after a certain amount of time. i tried using a timer but it doesnt work.

so

question 1. how can the user choose a song from their own sdcard,

question 2. how do you set a limit to the amount of time is playing.

losethequit
  • 203
  • 3
  • 17

2 Answers2

0

You can achieve the "30 seconds" using a

    Timer timer = new Timer();
    timer.schedule(new TimerTask() {
        @Override
        public void run() {
           //Stop the MediaPlayer here
        }
}, 30*1000);

To make your user select the file you can build your own FileManager, you can find some help at this other question: Android Programming: Where To Start For Creating A Simple File Browser?

UPDATE

Try this way to change your preview song

mp.stop();
mp.reset();
Uri url = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.1);
File songFile = new File(url.toString());
mp.setDataSource(songFile.getPath());
mp.prepare();
Community
  • 1
  • 1
GhostDerfel
  • 1,533
  • 1
  • 11
  • 18
  • IT WORKED!! Now all I have to do it figure out how to make it operate faster.. i think i need to put it on a runnable running on a separate thread. I already having a countdowntimer in place. the problem was calling the mp.pause(); followed by mp.reset(); i wanted to preview different parts of a song using buttons. i mp.seekTo(10000) into the song and then 20000 with another.. i just couldnt get it to reset. but now that it is, because i have so much running on the clock. i may need another thread – losethequit Feb 12 '14 at 22:07
  • So now you need to play only 30 seconds and if the user click in a button you will skip to another part of the file? – GhostDerfel Feb 13 '14 at 11:30
  • yeah.. the computer has to do some math. it may be because of the two timers running at the same time. or android buffering the song as it skips and plays through the song. @GhostDerfel. i dont know whats slowing it down. i added the other timer. the one you wrote. because my timer would not stop it. i had to rewrite how i create the media player... what is annoying is when i use the music player on my phone and start a song,. i can skip through the song easily. but in my app the sample sometimes clips if i press a lot of buttons. skipping around the track slows it down – losethequit Feb 13 '14 at 16:25
  • In my app I load an 80 MB mp3 files and had some issues on that also, what I can suggest you is to manage the skip button to skip to the time when the button stay like 1.5 second's without a click, so you acumlate the changes and then just one time and not a lot of times in a few milliseconds – GhostDerfel Feb 13 '14 at 16:30
  • but it shouldnt, and say for instance i want to start a new song to preview. that would mean i would have to instantiate a mediaplayer for every song. I tried switching the uri with a string. but it wants the r.raw.songfile); kinda argument. and you cant make that a string. so... ill have to release the object when another song picker button is pressed, just expressing logic out loud cause no one else around me cares.. – losethequit Feb 13 '14 at 16:31
  • The problem with using my code as example is that I was in need to control the pitch and BPM of the songs, so I forgot about MediaPlayer and start to use an NDK lib (MPG123) , will be hard to implement but you have more control in the hardware side – GhostDerfel Feb 13 '14 at 16:35
  • @user2989518 take a look at the "update" on my answer – GhostDerfel Feb 13 '14 at 16:44
  • the 30*1000 - in my app is a long. so i just put that there and it worked. let me google this ndk. i figured out how to adjust the bpm it just skips. it lags when it loads the song like 100 - 200 milliseconds to late,, and then when it starts playing it sort of right. if you move faster, some other samples will start and not finish, and then you have to press it like 3 times before the whole sample will play. i just dont get how android phones media players can skip seamlessly through the song. but i cant – losethequit Feb 13 '14 at 16:54
  • public void getNewStartingPoint() { mp = MediaPlayer.create(this, R.raw.aabbccdd); try { mp.seekTo(newStartingPoint); mp.start(); theOtherStopWatch++; } catch (Exception e) { Log.e(TAG, "error: " + e.getMessage(), e); } Timer timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { mp.pause(); } }, hTMill); } – losethequit Feb 13 '14 at 16:58
0
public void getNewStartingPoint() {

    mp = MediaPlayer.create(this, R.raw.aabbccdd);

    try {

        mp.seekTo(newStartingPoint);
        mp.start();
        theOtherStopWatch++;
    } catch (Exception e) {
        Log.e(TAG, "error: " + e.getMessage(), e);
    }
    Timer timer = new Timer();
    timer.schedule(new TimerTask() {
        @Override
        public void run() {
            mp.pause();
        }
    }, hTMill);

}

there may be some stuff missing.. but you can see that if the method is called. the song will only stop or pause when the timer. there is no way to stop the track immediately when another button is pressed. its as if i want the timer to stop when the next button is pressed to stop and not allow the track of the previous button pressed to be played. but i want the action of the button press to start the new track and stop the last track. instead of just starting a new track and relying on the timer to stop the track.

Hope you understand..

with your above

         anotherString = "R.raw.aabbccdd";

         fileName = "android.resource://" + getPackageName() + "/"
            + anotherString;

         mp.setDataSource(path);
         mp.setDataSource(this, Uri.parse(fileName));
         mp.prepare();

works .. but when you stop the track, reset, and prepare it all over again, it slows down the process.. it seems to work faster with..

         mp = MediaPlayer.create(this, R.raw.aabbccdd);
losethequit
  • 203
  • 3
  • 17