如何在android中播放媒体并控制它

时间:2014-08-06 15:40:16

标签: android

我需要在后台播放媒体并获取CorrentPosition()媒体播放器并实时显示活动!我遵循了许多示例和教程,但无法实现我的目标!有什么建议吗?

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)

1 个答案:

答案 0 :(得分:1)

查看文档中的Media Playback文章!我在这里总结一下。

播放资源中的音频:

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

获取当前位置:

int position = mediaPlayer.getCurrentPosition();

修改:现在您已经发布了代码,我可以看到这不是问题。试试这个:

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

并设置Runnable,如下所示:

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);
}

seekUpdate()中添加对onCreate()的调用以启动循环。您可以下载类似的示例here

基本思想是,当你调用seekUpdate()时,它会等待一秒钟,然后再次执行,从而保持对SeekBar的持续更新周期。

希望这有帮助!