android设备无法通过android studio播放音乐

时间:2015-08-11 17:09:42

标签: java android

package com.example.darshanms.myapplication;

import android.app.Activity;

import android.content.Intent;

import android.media.MediaPlayer;

import android.os.Bundle;


import static com.example.darshanms.myapplication.R.raw.splashsound;



public class splash extends Activity {

    MediaPlayer mp;

    @Override

    protected void onCreate(Bundle TravisloveBacon){

        super.onCreate(TravisloveBacon);

        setContentView(R.layout.splash);

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

        mp.reset();

        mp.start();


        Thread timer=new Thread(){

            public void run(){

                try {

                    sleep(5000);

                } catch (InterruptedException e) {

                    e.printStackTrace();

                } finally {

    Intent openMainActivity=new Intent("com.example.darshanms.MAINACTIVITY");

                    startActivity(openMainActivity);

                }

            }

        };

        timer.start();


    }

    @Override

    protected void onPause() {

        super.onPause();

        mp.release();

        finish();

    }

}

我试着在小应用程序中播放音乐。我的Android设备可以产生输出,但不能播放音乐。

1 个答案:

答案 0 :(得分:0)

尝试切换这一行:

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

mp.reset();

mp.start();

使用:

mp=MediaPlayer.create(this, R.raw.splashsound);
mp.prepare();
mp.start();