繁体   English   中英

在Android中与MediaPlayer.create()一起使用哪个上下文?

[英]Which Context to use with MediaPlayer.create() in android?

我应该在MediaPlayer.create()使用哪个上下文?

我正在对所有MediaPlayer对象使用Activity.this 但是我认为这给了我空指针异常。

上下文是否可以成为在Android或其他工具中强制关闭的原因?

此处的“ com.bhavin.panara.kbc ”是程序包名称。 第108行是mediaplayer.start()

这是代码:

MediaPlayer mp;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    .
    .
    .
    mp = MediaPlayer.create(PlayScreen.this, R.raw.sound);
    .
    .
    mp.start(); // 108th line.

    }

这是我的日志猫报告。

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bhavin.panara.kbc/com.bhavin.panara.kbc.PlayScreen}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1999)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2026)
at android.app.ActivityThread.access$600(ActivityThread.java:127)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1174)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4506)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.bhavin.panara.kbc.PlayScreen.onCreate(PlayScreen.java:108)
at android.app.Activity.performCreate(Activity.java:4479)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1050)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1963)
... 11 more

尝试这个-

  MediaPlayer media=new MediaPlayer();
  media.setDataSource(songname);
  media.prepare();
  media.start();

不,它表示一个空指针异常,因此可能确实得到了文件的所述路径。 如果要使用媒体播放器,一种方法是:

MediaPlayer player = MediaPlayer.create(context, uri);



player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            public void onCompletion(MediaPlayer mp) {
                Log.d(TAG, "play Completed ...Starting Again ");
                player.start();
            }
        });

    player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

        public void onPrepared(MediaPlayer mp) {
            Log.d(TAG, "preparation Completed ...playing ");
            player.start();
        }
    });
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //set the layout of the Activity
    setContentView(R.layout.activity_songs);

    //initialize views
    initializeViews();
}

public void initializeViews(){
    songName = (TextView) findViewById(R.id.songName);
    songName.setText("khmosia.mp3");
    mediaPlayer = MediaPlayer.create(this, Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.khmosia));
    mediaPlayer.start();
    timeElapsed = mediaPlayer.getCurrentPosition();


    finalTime = mediaPlayer.getDuration();
    duration = (TextView) findViewById(R.id.songDuration);
    timeElapsed = mediaPlayer.getCurrentPosition();

    seekbar = (SeekBar) findViewById(R.id.seekBar);
    seekbar.setProgress((int) timeElapsed);

    durationHandler.postDelayed(updateSeekBarTime, 100);
    seekbar.setMax((int) finalTime);
    seekbar.setClickable(false);
    Runnable updateSeekBarTime = new Runnable() {
        public void run() {
            //get current position
            timeElapsed = mediaPlayer.getCurrentPosition();
            //set seekbar progress
            seekbar.setProgress((int) timeElapsed);
            //set time remaing
            double timeRemaining = finalTime - timeElapsed;
            duration.setText(String.format("%d min, %d sec", TimeUnit.MILLISECONDS.toMinutes((long) timeRemaining), TimeUnit.MILLISECONDS.toSeconds((long) timeRemaining) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes((long) timeRemaining))));

            //repeat yourself that again in 100 miliseconds
            durationHandler.postDelayed(this, 100);
            mediaPlayer.pause();

        }
    };

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM