2014-11-14 136 views
0

我想在後臺創建一個帶有音樂的閃屏,但音樂不播放。mediaPlayer不播放

這裏是我的代碼:

package com.example.thebasicseries; 

import android.support.v7.app.ActionBarActivity; 
import android.content.Intent; 
import android.media.MediaPlayer; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 

public class MainActivity extends ActionBarActivity { 
    MediaPlayer logoMusic; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.splash); 

     logoMusic = MediaPlayer.create(MainActivity.this,R.raw.techno); 
     Thread logoTimer = new Thread(){ 
      public void run(){ 
       try{ 
        sleep(5000); 
        Intent menuIntent = new Intent("com.example.thebasicseries.MENU"); 
        startActivity(menuIntent); 
       } catch (InterruptedException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
       finally{ 
        finish(); 
       } 
      } 

     }; 
     logoTimer.start(); 
     logoMusic.release(); 
    } 

沒有之前或在運行該應用程序後顯示的錯誤信息。

+0

需要logoMusic.play()才能播放音樂。我只看到創建和發佈。 – Wildroid 2014-11-14 13:59:24

+0

沒有.play(),因爲我試過。它在工作之前,但我不明白爲什麼它停止工作,也許是因爲我一直在玩代碼。 – jass 2014-11-14 14:04:55

+0

我建議你使用AsyncTask在自己的線程中播放背景音樂 - 請參閱此示例[鏈接](http://stackoverflow.com/a/16338215/4123144) – Wildroid 2014-11-14 14:15:18

回答

0

LOL,你創建的MP:

logoMusic = MediaPlayer.create(MainActivity.this,R.raw.techno);

但從來沒有發揮它:

logoMusic.play(); 
0

更好地從視頻削減特定部分的閃屏,並使用它像GIF圖片,將工作

+0

好主意。將嘗試它! – jass 2014-11-14 14:22:59

+0

@jass投票回答 – 2014-11-15 04:07:38

0

我有它的工作!

package com.example.thebasicseries; 


import android.support.v7.app.ActionBarActivity; 
import android.content.Intent; 
import android.media.MediaPlayer; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 


public class MainActivity extends ActionBarActivity { 
    MediaPlayer logoMusic; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.splash); 

     logoMusic = MediaPlayer.create(MainActivity.this,R.raw.techno); 
     logoMusic.start(); 
     Thread logoTimer = new Thread(){ 
      public void run(){ 
       try{ 
        sleep(5000); 
        Intent menuIntent = new Intent("com.example.thebasicseries.MENU"); 
        startActivity(menuIntent); 
       } catch (InterruptedException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
       finally{ 
        finish(); 
       } 
      } 

     }; 
     logoTimer.start(); 

    } 
    @Override //so i just create an onPause method and release my song 

    protected void onPause() { 
     // TODO Auto-generated method stub 
     super.onPause(); 
     logoMusic.release(); //while it is onPause i release the song 
    } 


}