2011-03-29 96 views
19

嗨,大家好,我想播放某個MP3文件時單擊文本。例如,我點擊單詞「尼古拉斯」號,應用程序必須發揮nicholas.mp3 ...Android:從原始資源播放MP3文件點擊TextView

對不起,我的亂碼,我是新來的Android開發人員:

package com.example.playword; 

import java.io.IOException; 

import android.app.Activity; 
import android.media.MediaPlayer; 
import android.os.Bundle; 
//import android.os.Handler; 
import android.view.View; 
//import android.view.View.OnClickListener; 
//import android.widget.Button; 
import android.widget.TextView; 

public class PlayWord extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     //final Handler mHandler = new Handler(); 

     final TextView nicholas = (TextView) findViewById(R.id.nicholas); 
     final TextView was = (TextView) findViewById(R.id.was); 

     nicholas.setText("Nicholas "); 
     was.setText("was ");   

     /* 
     Button btn = (Button) (findViewById(R.id.nicholasBtn)); 

     btn.setOnClickListener(new OnClickListener(){ 
      @Override 
      public void onClick(View v) { 
       nicholas.setText("Nicholas (Clicked!) "); 
      } 

      }); 
     */ 

     View.OnClickListener handler = new View.OnClickListener(){ 
      public void onClick(View v) { 
       switch (v.getId()) { 
        case R.id.nicholas: // doStuff 

         MediaPlayer mPlayer = MediaPlayer.create(null, R.raw.aaanicholas); 

         try { 
          mPlayer.prepare(); 
         } catch (IllegalStateException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } catch (IOException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } 

         mPlayer.start(); 

         nicholas.setText("Nicholas (Clicked!) "); 
         break; 

        case R.id.was: // doStuff 

         MediaPlayer mPlayer1 = MediaPlayer.create(null, R.raw.aaawas); 

         try { 
          mPlayer1.prepare(); 
         } catch (IllegalStateException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } catch (IOException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } 

         mPlayer1.start(); 

         was.setText("was (Clicked!) "); 
         break; 
       } 
      } 
     }; 

     findViewById(R.id.nicholas).setOnClickListener(handler); 
     findViewById(R.id.was).setOnClickListener(handler); 

    } 
} 

當我運行這個,我得到一個關閉力的錯誤..你對這個有更好的想法嗎?提前謝謝了!

回答

44

你必須在一個上下文實例傳遞到MediaPlayer.create方法:

MediaPlayer mPlayer = MediaPlayer.create(PlayWorld.this, R.raw.aaanicholas); 

此外,create()電話後,準備已經執行,所以你並不需要顯式執行它,只需調用start()權之後create()

4

當你創建mPlayer對象時,你應該把它傳遞給Context,這在你的情況下是PlayWord.this

0
MediaPlayer mPlayer = MediaPlayer.create(FakeCallScreen.this, R.raw.mysoundfile); 
    mPlayer.start(); 
+0

請避免給出只有代碼的答案。嘗試解釋您的代碼如何解決OP的問題。如果你能提供一些背景和說明,那對每個人都更有幫助,特別是那些經驗不如你的人。 – 2016-12-31 19:39:19