mffff
mffff

Reputation: 15

How to play media and control it in android

I need to play media in background and get CorrentPosition() of media player and show it in activity real time! I follow many examples and tutorial but was not able to reach my goal! Any suggestions?

final Uri myUri1 = Uri.parse("file:///sdcard/quran/"+number+".mp3");
                    mp_qiraat=MediaPlayer.create(SuraSurface.this, myUri1);
                mp_qiraat.start();
                new Thread(new Runnable() {

                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        while(mp_qiraat.getCurrentPosition()<mp_qiraat.getDuration()){
                        tv_song_time.setText((mp_qiraat.getCurrentPosition()/1000)+"");
                        sb_song.setProgress(mp_qiraat.getCurrentPosition()/mp_qiraat.getDuration());
                        }
                    }
                }).start();

LogCat:

08-06 20:27:13.489: E/AndroidRuntime(11930): FATAL EXCEPTION: Thread-4012
08-06 20:27:13.489: E/AndroidRuntime(11930): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
08-06 20:27:13.489: E/AndroidRuntime(11930):    at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4056)
08-06 20:27:13.489: E/AndroidRuntime(11930):    at android.view.ViewRootImpl.invalidateChild(ViewRootImpl.java:722)
08-06 20:27:13.489: E/AndroidRuntime(11930):    at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:771)
08-06 20:27:13.489: E/AndroidRuntime(11930):    at android.view.ViewGroup.invalidateChild(ViewGroup.java:4109)
08-06 20:27:13.489: E/AndroidRuntime(11930):    at android.view.View.invalidate(View.java:8621)
08-06 20:27:13.489: E/AndroidRuntime(11930):    at android.view.View.invalidate(View.java:8572)
08-06 20:27:13.489: E/AndroidRuntime(11930):    at android.widget.TextView.checkForRelayout(TextView.java:6959)
08-06 20:27:13.489: E/AndroidRuntime(11930):    at android.widget.TextView.setText(TextView.java:3328)
08-06 20:27:13.489: E/AndroidRuntime(11930):    at android.widget.TextView.setText(TextView.java:3184)
08-06 20:27:13.489: E/AndroidRuntime(11930):    at android.widget.TextView.setText(TextView.java:3159)
08-06 20:27:13.489: E/AndroidRuntime(11930):    at ir.aiga.apps.quran.SuraSurface$17$1.run(SuraSurface.java:522)
08-06 20:27:13.489: E/AndroidRuntime(11930):    at java.lang.Thread.run(Thread.java:856)

Upvotes: 0

Views: 65

Answers (1)

Eoin
Eoin

Reputation: 833

Have a look at the Media Playback article in the docs! I'll summarize here.

To play audio from a resource:

MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.sound_file);
mediaPlayer.start();

To get the current position:

int position = mediaPlayer.getCurrentPosition();

Edit: Now you've posted your code, I can see that isn't the problem. Try this:

MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.sound_file);
mediaPlayer.start();

And set up your Runnable like this:

Handler handler = new Handler();
Runnable run = new Runnable() { @Override public void run() { seekUpdate(); } };
public void seekUpdate() {
    sb_song.setProgress(mediaPlayer.getCurrentPosition()/mediaPlayer.getDuration());
    seekHandler.postDelayed(run, 1000);
}

And add a call to seekUpdate() in onCreate() to start the loop. There's a similar example here that you can download.

The basic idea is that when you call seekUpdate(), it waits for one second, then executes again, thus maintaining a constant cycle of updates to the SeekBar.

Hope this helps!

Upvotes: 1

Related Questions